這篇文章主要為大家詳細介紹了PHP縮圖產生和圖片浮水印製作過程,php實現浮水印添加與縮圖產生的相關步驟,具有一定的參考價值,感興趣的小夥伴們可以參考一下
1.開始
在網站上傳圖片過程,經常用到縮圖功能。這裡我自己寫了一個圖片處理的Image類,能產生縮圖,並且可以添加浮水印圖。
2.如何產生縮圖
產生縮圖,關鍵的是如何計算縮放比率。
這裡,我根據圖片等比縮放,寬高的幾種常見變化,得出一個算縮放比率演算法是,使用新圖(即縮圖)的寬高,分別除以原圖的寬高,看哪個值大,就取它作為縮放比率:
縮放比率 = Max( { 新圖高度 / 原圖高度 , 新圖寬度 / 原圖寬度 } )
也就是:
If ( (新圖高度 / 原圖高度) > (新圖寬度 / 原圖寬度 ) ) {
縮放比率 = 新圖高度 / 原圖高度;
}ELSE {
縮放比率 = 新圖寬度 / 原圖寬度;
}
這裡列出情境的圖片縮放情境,及處理方法:
e.g
情境1,原圖比新圖大的情況, 縮放比率 = 新圖寬度 / 原圖寬度 :
情境2,原圖比新圖大的情況,b. 縮放比率 = 新圖高度 / 原圖高度 :
情境3,原圖比新圖大的情況,而且新圖寬高相等,即新圖形狀是正方形,那麼上面的縮放演算法也是適用的。
情境4,如果 “新圖寬度 >= 原圖寬度” ,同時 “新圖高度 >= 原圖高度”,那麼不縮放圖片,也不放大圖片,保持原圖。
情境5,如果 “新圖寬度 < 原圖寬度”,同時 “新圖高度 >= 原圖高度” ,那麼先設定 “新圖高度= 原圖高度”,再剪下。
情境6,如果 “新圖高度 < 原圖高度”,同時 “新圖寬度 >= 原圖寬度” ,那麼先設定 “新圖寬度= 原圖寬度”,再剪下。
3.如何添加浮水印圖片
添加浮水印很容易,我這裡沒考慮那麼複雜,主要是控制浮水印位置在圖片的右下角,和控制浮水印在圖片中的大小。如,當靶心圖表片與浮水印圖大小接近,那麼需要先等比縮放浮水印圖片,再添加浮水印圖片。
左邊兩幅圖,上面是原圖,下面是浮水印圖,右邊的縮放後加浮水印的新圖。
4.類圖
5.PHP代碼
5.1. 建構函式 __construct()
在Image類中,除了建構函式__construct()是public,其它函數都為private.也就是在函數__construct()中,直接完成了產生縮圖和添加浮水印圖的功能。如果,只產生縮圖而不需要添加浮水印,那麼直接在__construct()的參數$markPath,設定為null即可。
其中,“$this->quality = $quality ? $quality : 75;” 控制輸出為JPG圖片時,控製圖片品質(0-100),預設值為75;
/** * Image constructor. * @param string $imagePath 圖片路徑 * @param string $markPath 浮水印圖片路徑 * @param int $new_width 縮圖寬度 * @param int $new_height 縮圖高度 * @param int $quality JPG圖片格輸出品質 */ 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. 產生縮圖函數_thumb()
/** * 縮圖(按等比例,根據設定的寬度和高度進行裁剪) */ private function _thumb() { //如果原圖本身小於縮圖,按原圖長高 if ($this->newWidth > $this->width) $this->newWidth = $this->width; if ($this->newHeight > $this->height) $this->newHeight = $this->height; //背景圖長高 $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 { //計算縮放比率 $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); }
產生縮圖函數_thumb() ,是按照前面的分析來進行編碼。
5.3. 添加浮水印圖片函數 _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); }
在添加浮水印圖片中,用到一個_thumb1()函數來縮放浮水印圖片:
/** * 縮圖(按等比例) * @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; //輸出映像品質,jpg有效 private $quality; /** * Image constructor. * @param string $imagePath 圖片路徑 * @param string $markPath 浮水印圖片路徑 * @param int $new_width 縮圖寬度 * @param int $new_height 縮圖高度 * @param int $quality JPG圖片格輸出品質 */ 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); } /** * 縮圖(按等比例,根據設定的寬度和高度進行裁剪) */ private function _thumb() { //如果原圖本身小於縮圖,按原圖長高 if ($this->newWidth > $this->width) $this->newWidth = $this->width; if ($this->newHeight > $this->height) $this->newHeight = $this->height; //背景圖長高 $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 { //計算縮放比率 $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('不支援當前圖片類型.' . $type); break; } return $img; } /** * 建立一個背景映像 * @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); //設定背景為透明的 imagealphablending($img, false); imagesavealpha($img, true); break; case 4://gif imagecolortransparent($img, 0); break; } return $img; }}
6.調用
調用非常簡單,在引入類後,直接new 並輸入對應參數即可:
e.g.
new Image($_path, MARK, 400, 200, 100);
7.小結
這個Image 類能夠產生縮圖,不出現黑邊,添加浮水印圖,能根據圖片的大小縮放浮水印圖。當然有個缺點,就是不能縮放GIF的動畫,因為涉及到幀的處理,比較麻煩。
以上就是本文的全部內容,希望對大家的學習有所協助。