php 給圖片添加水印 實例


方法已經放在下面了:

/**
* 添加圖片水印
*
* @param $picPath
* @param $logoPath
*/
private function _addWaterMark($picPath, $logoPath)
{
//如果圖片不存在或者logo不存在則不處理
if (!file_exists($picPath) || !file_exists($logoPath)) {
return;
}

//創建圖片的實例
$im = imagecreatefromstring(file_get_contents($picPath));

//獲取水印源
$watermark = imagecreatefromstring(file_get_contents($logoPath));

//獲取圖、水印 寬高類型
list($bgWidth, $bgHight, $bgType) = getimagesize($picPath);
list($logoWidth, $logoHight, $logoType) = getimagesize($logoPath);

//定義平鋪數據
$xLength = $bgWidth - 10; //x軸總長度
$yLength = $bgHight - 10; //y軸總長度

//創建透明畫布 偽白色
$opacity = 15;
$w = imagesx($watermark);
$h = imagesy($watermark);
$cut = imagecreatetruecolor($w, $h);
$white = imagecolorallocatealpha($cut, 255, 255, 255, 0);
imagefill($cut, 0, 0, $white);

//整合水印
imagecopy($cut, $watermark, 0, 0, 0, 0, $w, $h);

//循環平鋪水印
for ($x = 0; $x < $xLength; $x++) {
for ($y = 0; $y < $yLength; $y++) {
imagecopymerge($im, $cut, $x, $y, 0, 0, $logoWidth, $logoHight, $opacity);
$y += $logoHight;
}
$x += $logoWidth;
}
imagejpeg($im, $picPath);
imagedestroy($im);
}

效果如下:

 
         
         
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM