PHP圖像處理技術實例總結【繪圖、水印、驗證碼、圖像壓縮】

這篇文章主要介紹了PHP圖像處理技術,結合實例形式總結分析了php繪圖、水印、驗證碼、圖像壓縮等相關函數、功能與圖形繪製實現技巧,需要的朋友可以參考下

本文實例總結了PHP圖像處理技術。分享給大家供大家參考,具體如下:

1、繪圖

場景: 驗證碼、圖像水印、圖像壓縮處理

php繪圖座標體系是從0,0點越向右值越大,越向下值越大

需要開啓php的gd2擴展 php.ini 中

參數1:圖像資源(畫布)
參數2:開始的x軸座標
參數3:開始的y軸座標
參數4:結束的x軸座標
參數5:結束的y軸座標
參數6:線條的顏色

(1)繪製線條: imageline($p1, $p2, $p3, $p4, $p5, $6)
(2)繪製三角形:imageline($p1, $p2, $p3, $p4, $p5, $6) // 需要3次
(3)繪製矩形:imagerectangle($p1, $p2, $p3, $p4, $p5, $6)
(3.1)繪製並填充矩形:imagefilledrectangle($p1, $p2, $p3, $p4, $p5, $6)
(4)繪製橢圓:imageellipse($p1, $p2, $p3, $p4, $p5, $6)
(4.1)繪製並填充橢圓:imagefilledellipse($p1, $p2, $p3, $p4, $p5, $6)

參數1:目標圖像
參數2:原始圖像
參數3:目標圖像座標x
參數4:目標圖像座標y
參數5:原始圖像開始座標x
參數6:原始圖像開始座標y
參數7:原始圖像寬度
參數8:原始圖像高度

(5)將圖片繪製到畫布上:imagecopy ( $p1, $p2, $p3, $p4, $p5, $6, $7, $8)

參數1:目標圖像
參數2:字體 1,2,3,4 或 5,則使用內置字體
參數3:目標圖像座標x
參數4:目標圖像座標y
參數5:字符,文字
參數6:顏色

(6)繪製字符串:imagestring( $p1, $p2, $p3, $p4, $p5, $6)// 向畫布寫入字符,文字

參數1:圖像資源
參數2:字體大小
參數3:傾斜角度
參數4:x軸座標
參數5:y軸座標
參數6:字體顏色
參數7:字體文件
參數8:文字

(7)繪製中文:imagettftext($p1, $p2, $p3, $p4, $p5, $6, $7, $8)

參數1:圖像資源
參數2:弧形開始x座標
參數3:弧形開始y座標
參數4:弧形寬度
參數5:弧形高度
參數6:弧形開始角度
參數7:弧形結束角度
參數8:繪圖顏色

(8)繪製弧形:imagearc($p1, $p2, $p3, $p4, $p5, $6, $7, $8) // 三點鐘的位置是起點(0度), 順時針方向繪畫

實例 - 弧形

// 創建一個 200X200 的圖像
$img = imagecreatetruecolor(200, 200);
// 分配顏色
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
// 畫一個黑色的圓
imagearc($img, 100, 100, 150, 150, 0, 360, $black);
// 將圖像輸出到瀏覽器
header("Content-type: image/png");
imagepng($img);
// 釋放內存
imagedestroy($img);

參數1:圖像資源
參數2:弧形開始x座標
參數3:弧形開始y座標
參數4:弧形寬度
參數5:弧形高度
參數6:弧形開始角度
參數7:弧形結束角度
參數8:繪圖顏色
參數9:填充樣式

IMG_ARC_PIE : 用直線連接產生圓形邊界
IMG_ARC_CHORD : 用直線連接了起始和結束點
IMG_ARC_NOFILL : 明弧或弦只有輪廓,不填充
IMG_ARC_EDGED :用直線將起始和結束點與中心點相連,和 IMG_ARC_NOFILL 一起使用是畫餅狀圖輪廓的好方法(而不用填充)

(9)繪製弧形並填充:imagefilledarc($p1, $p2, $p3, $p4, $p5, $6, $7, $8, $9) // 三點鐘的位置是起點(0度), 順時針方向繪畫

實例 - 弧形填充

// 創建圖像
$image = imagecreatetruecolor(100, 100);
// 分配一些顏色
$white  = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray   = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy   = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red   = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
// 創建 3D 效果
for ($i = 60; $i > 50; $i--) {
  imagefilledarc($image, 50, $i, 100, 50, 0, 45, $darknavy, IMG_ARC_PIE);
  imagefilledarc($image, 50, $i, 100, 50, 45, 75 , $darkgray, IMG_ARC_PIE);
  imagefilledarc($image, 50, $i, 100, 50, 75, 360 , $darkred, IMG_ARC_PIE);
}
imagefilledarc($image, 50, 50, 100, 50, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 75, 360 , $red, IMG_ARC_PIE);
// 輸出圖像
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

效果

2、水印

使用 imagestring() 或者 imagettftext()

實例 - 圖片加字

// 建立一幅 100X30 的圖像
$im = imagecreate(100, 30);
// 白色背景和藍色文本
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// 把字符串寫在圖像左上角
imagestring($im, 5, 0, 0, "Hello world!", $textcolor);
// 輸出圖像
header("Content-type: image/png");
imagepng($im);

3、驗證碼

封裝的驗證碼類

<?php
/*
 * 生成驗證碼
 */
class Captcha
{
  private $_width = 100;
  private $_height = 25;
  private $_number = 4; //顯示的驗證碼的字符個數
  private $_font  = 15; //驗證碼字體大小
  private $_fontfile = 'STXINWEI.TTF';
  //創建驗證碼圖像
  public function makeImage()
  {
    # 1. 創建圖像資源(畫布)
    $image = imagecreatetruecolor($this->_width,$this->_height);
    //隨機填充顏色
    //mt_rand(0,255)  生成一個更具有唯一性的隨機數 #000  255
    $color = imagecolorallocate($image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
    imagefill($image,0,0,$color);
    # 2.繪製文字
    $code = $this -> makeCode();  //隨機生成驗證碼文字 ab3g
    $color = imagecolorallocate($image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
    for($i=0;$i<$this->_number;$i++){
      imagettftext($image,$this->_font,mt_rand(-30,30),$i*($this->_width/$this->_number)+5,20,$color,$this->_fontfile,$code[$i]);
    }
    # 3.繪製15條幹擾線條
    for($i=0;$i<10;$i++){
      $color = imagecolorallocate($image,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
      imageline($image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color);
    }
    # 4.設置100個干擾像素點
    for($i=0;$i<100;$i++){
      imagesetpixel($image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color);
    }
    # 5.將驗證碼保存起來嗎,便於後面再其他地方使用
    //只能使用session來存儲,session明天就會講到
    session_start();
    $_SESSION['captcha'] = $code;
    //在瀏覽器輸出、顯示一下
    header("Content-Type:image/png");
    imagepng($image);
    imagedestroy($image);
  }
  /**
   * 隨機產生隨機數
   */
  public function makeCode()
  {
    # 獲得字母的範圍(大寫字母、小寫字母)
    $lower = range('a','z'); //創建從小a到小z字符範圍的數組
    $upper = range('A','Z'); //創建從大A到大Z範圍的數組
    $number = range(3,9);   //創建從3到9之間的數字
    //將上面的三個數組合併成一個數組
    $code  = array_merge($lower,$upper,$number);
    # 打亂數組元素的順序
    shuffle($code);
    //隨機從上面的數組中篩選出n個字符,需要通過下標來取數組的元素
    $str = '';
    for($i=0;$i<$this->_number;$i++){
      $str .= $code[$i];
    }
    return $str;
  }
  /**
   * 驗證用戶輸入的驗證碼和我們生產的驗證碼是否一致
   * @param [str] $input [輸入驗證碼值]
   * @return
   */
  public function checkCode($input)
  {
    session_start();
    if(strtolower($code) == strtolower($_SESSION['captcha'])){
      //說明驗證碼正確
      //echo '驗證碼正確';
      return true;
    }else{
      //echo '驗證碼錯誤';
      return false;
    }
  }
}
?>

實例 - 驗證碼驗證(結合上面的驗證類)

html頁面

<form action="captcha.php?act=verify" method="post">
  驗證碼:<input type="text" name="captcha">
  <img src="captcha.php?act=show">
  <br>
  <input type="submit" value="提交">
</form>

驗證碼檢測 captcha.php 頁面

  //接收地址欄上面的參數
  if($_GET['act']=='verify'){
    //說明是提交的表單
    //接收表單中用戶輸入的內容
    $code = $_POST['captcha'];
    //和創建的驗證碼進行比較
    session_start();
    //將用戶輸入的驗證碼 和 我們創建的統一小寫之後再進行比較
    if(strtolower($code) == strtolower($_SESSION['captcha'])){
      //說明驗證碼正確
      echo '驗證碼正確';
    }else{
      echo '驗證碼錯誤';
    }
  }else if($_GET['act']=='show'){
    //說明需要顯示一個圖片
    require 'Captcha.class.php';
    $captcha = new Captcha();
    $captcha -> makeImage();
  }

4、圖像壓縮

對圖像進行壓

縮處理非常簡單,因爲就一個函數

參數1:目標圖像資源(畫布)
參數2:等待壓縮圖像資源
參數3:目標點的x座標
參數4:目標點的y座標
參數5:原圖的x座標
參數6:原圖的y座標
參數7:目的地寬度(畫布寬)
參數8:目的地高度(畫布高)
參數9:原圖寬度
參數10:原圖高度

imagecopyresampled($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)

封裝的圖像壓縮類

<?php
/*
 * 圖像壓縮處理類
 */
class Thumb
{
  private $_filename;    //等待壓縮的圖像
  private $_thumb_path = 'thumb/';  //壓縮圖像的保存目錄
  public function __set($p,$v)
  {
    if(property_exists($this,$p)){
      $this -> $p = $v;
    }
  }
  //構造方法初始化需要壓縮的圖像
  public function __construct($file)
  {
    if(!file_exists($file)){
      echo '文件有誤,不能壓縮';
      return;
    }
    $this -> _filename = $file;
  }
  //圖像壓縮處理
  function makeThumb($area_w,$area_h)
  {
    $src_image = imagecreatefrompng($this->_filename);
    $res = getimagesize($this->_filename);
    echo '<pre>';
    var_dump($res);
    die;
    $dst_x = 0;
    $dst_y = 0;
    $src_x = 0;
    $src_y = 0;
    //原圖的寬度、高度
    $src_w = imagesx($src_image);  //獲得圖像資源的寬度
    $src_h = imagesy($src_image);  //獲得圖像資源的高度
    if($src_w / $area_w < $src_h/$area_h){
      $scale = $src_h/$area_h;
    }
    if($src_w / $area_w >= $src_h/$area_h){
      $scale = $src_w / $area_w;
    }
    $dst_w = (int)($src_w / $scale);
    $dst_h = (int)($src_h / $scale);
    $dst_image = imagecreatetruecolor($dst_w,$dst_h);
    $color = imagecolorallocate($dst_image,255,255,255);
    //將白色設置爲透明色
    imagecolortransparent($dst_image,$color);
    imagefill($dst_image,0,0,$color);
    imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);
    //可以在瀏覽器直接顯示
    //header("Content-Type:image/png");
    //imagepng($dst_image);
    //分目錄保存壓縮的圖像
    $sub_path = date('Ymd').'/';
    //規範:上傳的圖像保存到upload目錄,壓縮的圖像保存到thumb目錄
    if(!is_dir($this -> _thumb_path . $sub_path)){
      mkdir($this -> _thumb_path . $sub_path,0777,true);
    }
    $filename = $this -> _thumb_path . $sub_path.'thumb_'.$this->_filename;
    //也可以另存爲一個新的圖像
    imagepng($dst_image,$filename);
    return $filename;
  }
}
$thumb = new Thumb('upload.jpg');
$thumb -> _thumb_path = 'static/thumb/';
$file = $thumb -> makeThumb(100,50);
// var_dump($file);

更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧彙總》、《PHP數組(Array)操作技巧大全》、《PHP數據結構與算法教程》、《php程序設計算法總結》、《PHP數學運算技巧總結》、《php字符串(string)用法總結》及《php常見數據庫操作技巧彙總

希望本文所述對大家PHP程序設計有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章