溫馨提示×

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

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

PHP如何實(shí)現(xiàn)縮略圖生成和圖片水印

發(fā)布時(shí)間:2021-06-18 10:23:55 來源:億速云 閱讀:213 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)PHP如何實(shí)現(xiàn)縮略圖生成和圖片水印,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

1.開始

在網(wǎng)站上傳圖片過程,經(jīng)常用到縮略圖功能。這里我自己寫了一個(gè)圖片處理的Image類,能生成縮略圖,并且可以添加水印圖。

2.如何生成縮略圖

     生成縮略圖,關(guān)鍵的是如何計(jì)算縮放比率。

     這里,我根據(jù)圖片等比縮放,寬高的幾種常見變化,得出一個(gè)算縮放比率算法是,使用新圖(即縮略圖)的寬高,分別除以原圖的寬高,看哪個(gè)值大,就取它作為縮放比率:

     縮放比率  = Max( { 新圖高度  / 原圖高度 ,  新圖寬度  / 原圖寬度 } )

     也就是:

      If ( (新圖高度  / 原圖高度)  >  (新圖寬度  / 原圖寬度 ) )  {

              縮放比率 =  新圖高度  / 原圖高度;

      }ELSE {

             縮放比率 =  新圖寬度 / 原圖寬度;

     }

 這里列出場(chǎng)景的圖片縮放場(chǎng)景,及處理方法:

 e.g

場(chǎng)景1,原圖比新圖大的情況, 縮放比率 =  新圖寬度 / 原圖寬度 :

PHP如何實(shí)現(xiàn)縮略圖生成和圖片水印

場(chǎng)景2,原圖比新圖大的情況,b. 縮放比率 =  新圖高度 / 原圖高度 :

PHP如何實(shí)現(xiàn)縮略圖生成和圖片水印

場(chǎng)景3,原圖比新圖大的情況,而且新圖寬高相等,即新圖形狀是正方形,那么上面的縮放算法也是適用的。

場(chǎng)景4,如果 “新圖寬度 >= 原圖寬度”  ,同時(shí)  “新圖高度 >= 原圖高度”,那么不縮放圖片,也不放大圖片,保持原圖。

PHP如何實(shí)現(xiàn)縮略圖生成和圖片水印

場(chǎng)景5,如果 “新圖寬度 < 原圖寬度”,同時(shí)  “新圖高度 >= 原圖高度”  ,那么先設(shè)置  “新圖高度= 原圖高度”,再剪切。

PHP如何實(shí)現(xiàn)縮略圖生成和圖片水印

場(chǎng)景6,如果 “新圖高度 < 原圖高度”,同時(shí)  “新圖寬度 >= 原圖寬度”  ,那么先設(shè)置  “新圖寬度= 原圖寬度”,再剪切。

PHP如何實(shí)現(xiàn)縮略圖生成和圖片水印

3.如何添加水印圖片
   添加水印很容易,我這里沒考慮那么復(fù)雜,主要是控制水印位置在圖片的右下角,和控制水印在圖片中的大小。如,當(dāng)目標(biāo)圖片與水印圖大小接近,那么需要先等比縮放水印圖片,再添加水印圖片。

PHP如何實(shí)現(xiàn)縮略圖生成和圖片水印

左邊兩幅圖,上面是原圖,下面是水印圖,右邊的縮放后加水印的新圖。

 4.類圖

PHP如何實(shí)現(xiàn)縮略圖生成和圖片水印

5.PHP代碼

5.1. 構(gòu)造函數(shù) __construct()

     在Image類中,除了構(gòu)造函數(shù)__construct()是public,其它函數(shù)都為private.也就是在函數(shù)__construct()中,直接完成了生成縮略圖和添加水印圖的功能。如果,只生成縮略圖而不需要添加水印,那么直接在__construct()的參數(shù)$markPath,設(shè)置為null即可。

     其中,“$this->quality = $quality ? $quality : 75;” 控制輸出為JPG圖片時(shí),控制圖片質(zhì)量(0-100),默認(rèn)值為75;

  /**
   * Image constructor.
   * @param string $imagePath 圖片路徑
   * @param string $markPath 水印圖片路徑
   * @param int $new_width 縮略圖寬度
   * @param int $new_height 縮略圖高度
   * @param int $quality JPG圖片格輸出質(zhì)量
   */
  public function __construct(string $imagePath,
                string $markPath = null,
                int $new_width = null,
                int $new_height = null,
                int $quality = 75)
  {
    $this->imgPath = $_SERVER['DOCUMENT_ROOT'] . $imagePath;
    $this->waterMarkPath = $markPath;
    $this->newWidth = $new_width ? $new_width : $this->width;
    $this->newHeight = $new_height ? $new_height : $this->height;
    $this->quality = $quality ? $quality : 75;

    list($this->width, $this->height, $this->type) = getimagesize($this->imgPath);
    $this->img = $this->_loadImg($this->imgPath, $this->type);


    //生成縮略圖
    $this->_thumb();
    //添加水印圖片
    if (!empty($this->waterMarkPath)) $this->_addWaterMark();
    //輸出圖片
    $this->_outputImg();
  }

 Note: 先生成縮略圖,再在新圖上添加水印 圖片。 

5.2. 生成縮略圖函數(shù)_thumb()

   /**
   * 縮略圖(按等比例,根據(jù)設(shè)置的寬度和高度進(jìn)行裁剪)
   */
  private function _thumb()
  {

    //如果原圖本身小于縮略圖,按原圖長(zhǎng)高
    if ($this->newWidth > $this->width) $this->newWidth = $this->width;
    if ($this->newHeight > $this->height) $this->newHeight = $this->height;

    //背景圖長(zhǎng)高
    $gd_width = $this->newWidth;
    $gd_height = $this->newHeight;

    //如果縮略圖寬高,其中有一邊等于原圖的寬高,就直接裁剪
    if ($gd_width == $this->width || $gd_height == $this->height) {
      $this->newWidth = $this->width;
      $this->newHeight = $this->height;
    } else {

      //計(jì)算縮放比率
      $per = 1;

      if (($this->newHeight / $this->height) > ($this->newWidth / $this->width)) {
        $per = $this->newHeight / $this->height;
      } else {
        $per = $this->newWidth / $this->width;
      }

      if ($per < 1) {
        $this->newWidth = $this->width * $per;
        $this->newHeight = $this->height * $per;
      }
    }

    $this->newImg = $this->_CreateImg($gd_width, $gd_height, $this->type);
    imagecopyresampled($this->newImg, $this->img, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height);
  }

生成縮略圖函數(shù)_thumb() ,是按照前面的分析來進(jìn)行編碼。 

5.3. 添加水印圖片函數(shù) _addWaterMark()    

   /**
   * 添加水印
   */
  private function _addWaterMark()
  {
    $ratio = 1 / 5; //水印縮放比率

    $Width = imagesx($this->newImg);
    $Height = imagesy($this->newImg);

    $n_width = $Width * $ratio;
    $n_height = $Width * $ratio;

    list($markWidth, $markHeight, $markType) = getimagesize($this->waterMarkPath);

    if ($n_width > $markWidth) $n_width = $markWidth;
    if ($n_height > $markHeight) $n_height = $markHeight;

    $Img = $this->_loadImg($this->waterMarkPath, $markType);
    $Img = $this->_thumb1($Img, $markWidth, $markHeight, $markType, $n_width, $n_height);
    $markWidth = imagesx($Img);
    $markHeight = imagesy($Img);
    imagecopyresampled($this->newImg, $Img, $Width - $markWidth - 10, $Height - $markHeight - 10, 0, 0, $markWidth, $markHeight, $markWidth, $markHeight);
    imagedestroy($Img);
  }

在添加水印圖片中,用到一個(gè)_thumb1()函數(shù)來縮放水印圖片:

  /**
   * 縮略圖(按等比例)
   * @param resource $img 圖像流
   * @param int $width
   * @param int $height
   * @param int $type
   * @param int $new_width
   * @param int $new_height
   * @return resource
   */
  private function _thumb1($img, $width, $height, $type, $new_width, $new_height)
  {

    if ($width < $height) {
      $new_width = ($new_height / $height) * $width;
    } else {
      $new_height = ($new_width / $width) * $height;
    }

    $newImg = $this->_CreateImg($new_width, $new_height, $type);
    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    return $newImg;
  }

5.4. 完整代碼:

<?php

/**
 * 圖片處理,生成縮略圖和添加水印圖片
 * Created by PhpStorm.
 * User: andy
 * Date: 17-1-3
 * Time: 上午11:55
 */
class Image
{
 //原圖
 private $imgPath; //圖片地址
 private $width;  //圖片寬度
 private $height; //圖片高度
 private $type;  //圖片類型
 private $img;  //圖片(圖像流)

 //縮略圖
 private $newImg; //縮略圖(圖像流)
 private $newWidth;
 private $newHeight;

 //水印圖路徑
 private $waterMarkPath;

 //輸出圖像質(zhì)量,jpg有效
 private $quality;

 /**
  * Image constructor.
  * @param string $imagePath 圖片路徑
  * @param string $markPath 水印圖片路徑
  * @param int $new_width 縮略圖寬度
  * @param int $new_height 縮略圖高度
  * @param int $quality JPG圖片格輸出質(zhì)量
  */
 public function __construct(string $imagePath,
        string $markPath = null,
        int $new_width = null,
        int $new_height = null,
        int $quality = 75)
 {
  $this->imgPath = $_SERVER['DOCUMENT_ROOT'] . $imagePath;
  $this->waterMarkPath = $markPath;
  $this->newWidth = $new_width ? $new_width : $this->width;
  $this->newHeight = $new_height ? $new_height : $this->height;
  $this->quality = $quality ? $quality : 75;

  list($this->width, $this->height, $this->type) = getimagesize($this->imgPath);
  $this->img = $this->_loadImg($this->imgPath, $this->type);


  //生成縮略圖
  $this->_thumb();
  //添加水印圖片
  if (!empty($this->waterMarkPath)) $this->_addWaterMark();
  //輸出圖片
  $this->_outputImg();
 }

 /**
  *圖片輸出
  */
 private function _outputImg()
 {
  switch ($this->type) {
   case 1: // GIF
    imagegif($this->newImg, $this->imgPath);
    break;
   case 2: // JPG
    if (intval($this->quality) < 0 || intval($this->quality) > 100) $this->quality = 75;
    imagejpeg($this->newImg, $this->imgPath, $this->quality);
    break;
   case 3: // PNG
    imagepng($this->newImg, $this->imgPath);
    break;
  }
  imagedestroy($this->newImg);
  imagedestroy($this->img);
 }

 /**
  * 添加水印
  */
 private function _addWaterMark()
 {
  $ratio = 1 / 5; //水印縮放比率

  $Width = imagesx($this->newImg);
  $Height = imagesy($this->newImg);

  $n_width = $Width * $ratio;
  $n_height = $Width * $ratio;

  list($markWidth, $markHeight, $markType) = getimagesize($this->waterMarkPath);

  if ($n_width > $markWidth) $n_width = $markWidth;
  if ($n_height > $markHeight) $n_height = $markHeight;

  $Img = $this->_loadImg($this->waterMarkPath, $markType);
  $Img = $this->_thumb1($Img, $markWidth, $markHeight, $markType, $n_width, $n_height);
  $markWidth = imagesx($Img);
  $markHeight = imagesy($Img);
  imagecopyresampled($this->newImg, $Img, $Width - $markWidth - 10, $Height - $markHeight - 10, 0, 0, $markWidth, $markHeight, $markWidth, $markHeight);
  imagedestroy($Img);
 }

 /**
  * 縮略圖(按等比例,根據(jù)設(shè)置的寬度和高度進(jìn)行裁剪)
  */
 private function _thumb()
 {

  //如果原圖本身小于縮略圖,按原圖長(zhǎng)高
  if ($this->newWidth > $this->width) $this->newWidth = $this->width;
  if ($this->newHeight > $this->height) $this->newHeight = $this->height;

  //背景圖長(zhǎng)高
  $gd_width = $this->newWidth;
  $gd_height = $this->newHeight;

  //如果縮略圖寬高,其中有一邊等于原圖的寬高,就直接裁剪
  if ($gd_width == $this->width || $gd_height == $this->height) {
   $this->newWidth = $this->width;
   $this->newHeight = $this->height;
  } else {

   //計(jì)算縮放比率
   $per = 1;

   if (($this->newHeight / $this->height) > ($this->newWidth / $this->width)) {
    $per = $this->newHeight / $this->height;
   } else {
    $per = $this->newWidth / $this->width;
   }

   if ($per < 1) {
    $this->newWidth = $this->width * $per;
    $this->newHeight = $this->height * $per;
   }
  }

  $this->newImg = $this->_CreateImg($gd_width, $gd_height, $this->type);
  imagecopyresampled($this->newImg, $this->img, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height);
 }


 /**
  * 縮略圖(按等比例)
  * @param resource $img 圖像流
  * @param int $width
  * @param int $height
  * @param int $type
  * @param int $new_width
  * @param int $new_height
  * @return resource
  */
 private function _thumb1($img, $width, $height, $type, $new_width, $new_height)
 {

  if ($width < $height) {
   $new_width = ($new_height / $height) * $width;
  } else {
   $new_height = ($new_width / $width) * $height;
  }

  $newImg = $this->_CreateImg($new_width, $new_height, $type);
  imagecopyresampled($newImg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  return $newImg;
 }

 /**
  * 加載圖片
  * @param string $imgPath
  * @param int $type
  * @return resource
  */
 private function _loadImg($imgPath, $type)
 {
  switch ($type) {
   case 1: // GIF
    $img = imagecreatefromgif($imgPath);
    break;
   case 2: // JPG
    $img = imagecreatefromjpeg($imgPath);
    break;
   case 3: // PNG
    $img = imagecreatefrompng($imgPath);
    break;
   default: //其他類型
    Tool::alertBack('不支持當(dāng)前圖片類型.' . $type);
    break;
  }
  return $img;
 }

 /**
  * 創(chuàng)建一個(gè)背景圖像
  * @param int $width
  * @param int $height
  * @param int $type
  * @return resource
  */
 private function _CreateImg($width, $height, $type)
 {
  $img = imagecreatetruecolor($width, $height);
  switch ($type) {
   case 3: //png
    imagecolortransparent($img, 0); //設(shè)置背景為透明的
    imagealphablending($img, false);
    imagesavealpha($img, true);
    break;
   case 4://gif
    imagecolortransparent($img, 0);
    break;
  }

  return $img;
 }
}

6.調(diào)用

調(diào)用非常簡(jiǎn)單,在引入類后,直接new 并輸入對(duì)應(yīng)參數(shù)即可:

e.g.

new Image($_path, MARK, 400, 200, 100);

7.小結(jié)
這個(gè)Image 類能夠生成縮略圖,不出現(xiàn)黑邊,添加水印圖,能根據(jù)圖片的大小縮放水印圖。當(dāng)然有個(gè)缺點(diǎn),就是不能縮放GIF的動(dòng)畫,因?yàn)樯婕暗綆奶幚?,比較麻煩。

關(guān)于“PHP如何實(shí)現(xiàn)縮略圖生成和圖片水印”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

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

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

php
AI