溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用PHP怎么實(shí)現(xiàn)圖像處理技術(shù)

發(fā)布時(shí)間:2021-06-09 16:46:46 來源:億速云 閱讀:144 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)使用PHP怎么實(shí)現(xiàn)圖像處理技術(shù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1、繪圖

場景: 驗(yàn)證碼、圖像水印、圖像壓縮處理

php繪圖坐標(biāo)體系是從0,0點(diǎn)越向右值越大,越向下值越大

需要開啟php的gd2擴(kuò)展 php.ini 中

參數(shù)1:圖像資源(畫布)
參數(shù)2:開始的x軸坐標(biāo)
參數(shù)3:開始的y軸坐標(biāo)
參數(shù)4:結(jié)束的x軸坐標(biāo)
參數(shù)5:結(jié)束的y軸坐標(biāo)
參數(shù)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)

參數(shù)1:目標(biāo)圖像
參數(shù)2:原始圖像
參數(shù)3:目標(biāo)圖像坐標(biāo)x
參數(shù)4:目標(biāo)圖像坐標(biāo)y
參數(shù)5:原始圖像開始坐標(biāo)x
參數(shù)6:原始圖像開始坐標(biāo)y
參數(shù)7:原始圖像寬度
參數(shù)8:原始圖像高度

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

參數(shù)1:目標(biāo)圖像
參數(shù)2:字體 1,2,3,4 或 5,則使用內(nèi)置字體
參數(shù)3:目標(biāo)圖像坐標(biāo)x
參數(shù)4:目標(biāo)圖像坐標(biāo)y
參數(shù)5:字符,文字
參數(shù)6:顏色

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

參數(shù)1:圖像資源
參數(shù)2:字體大小
參數(shù)3:傾斜角度
參數(shù)4:x軸坐標(biāo)
參數(shù)5:y軸坐標(biāo)
參數(shù)6:字體顏色
參數(shù)7:字體文件
參數(shù)8:文字

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

參數(shù)1:圖像資源
參數(shù)2:弧形開始x坐標(biāo)
參數(shù)3:弧形開始y坐標(biāo)
參數(shù)4:弧形寬度
參數(shù)5:弧形高度
參數(shù)6:弧形開始角度
參數(shù)7:弧形結(jié)束角度
參數(shù)8:繪圖顏色

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

實(shí)例 - 弧形

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

參數(shù)1:圖像資源
參數(shù)2:弧形開始x坐標(biāo)
參數(shù)3:弧形開始y坐標(biāo)
參數(shù)4:弧形寬度
參數(shù)5:弧形高度
參數(shù)6:弧形開始角度
參數(shù)7:弧形結(jié)束角度
參數(shù)8:繪圖顏色
參數(shù)9:填充樣式

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

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

實(shí)例 - 弧形填充

// 創(chuàng)建圖像
$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);
// 創(chuàng)建 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);

效果

使用PHP怎么實(shí)現(xiàn)圖像處理技術(shù)

2、水印

使用 imagestring() 或者 imagettftext()

實(shí)例 - 圖片加字

// 建立一幅 100X30 的圖像
$im = imagecreate(100, 30);
// 白色背景和藍(lán)色文本
$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、驗(yàn)證碼

封裝的驗(yàn)證碼類

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

實(shí)例 - 驗(yàn)證碼驗(yàn)證(結(jié)合上面的驗(yàn)證類)

html頁面

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

驗(yàn)證碼檢測 captcha.php 頁面

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

4、圖像壓縮

對圖像進(jìn)行壓

縮處理非常簡單,因?yàn)榫鸵粋€(gè)函數(shù)

參數(shù)1:目標(biāo)圖像資源(畫布)
參數(shù)2:等待壓縮圖像資源
參數(shù)3:目標(biāo)點(diǎn)的x坐標(biāo)
參數(shù)4:目標(biāo)點(diǎn)的y坐標(biāo)
參數(shù)5:原圖的x坐標(biāo)
參數(shù)6:原圖的y坐標(biāo)
參數(shù)7:目的地寬度(畫布寬)
參數(shù)8:目的地高度(畫布高)
參數(shù)9:原圖寬度
參數(shù)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;
    }
  }
  //構(gòu)造方法初始化需要壓縮的圖像
  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);
    //將白色設(shè)置為透明色
    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').'/';
    //規(guī)范:上傳的圖像保存到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;
    //也可以另存為一個(gè)新的圖像
    imagepng($dst_image,$filename);
    return $filename;
  }
}
$thumb = new Thumb('upload.jpg');
$thumb -> _thumb_path = 'static/thumb/';
$file = $thumb -> makeThumb(100,50);
// var_dump($file);

看完上述內(nèi)容,你們對使用PHP怎么實(shí)現(xiàn)圖像處理技術(shù)有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI