PHP GD库 生成图片水印
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                PHP GD库 生成图片水印
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                * index.php
<?php /*** Created by PhpStorm.* User: mingzhanghui* Date: 9/24/2019* Time: 12:47*/include "autoload.php";// 頭像圖片 $dstPath = dirname(__FILE__)."/image/avatar.jpg"; // 國旗圖片 $srcPath = dirname(__FILE__)."/image/flag.png";/** @var $dataDst string image content */ $dataDst = file_get_contents($dstPath); $dataSrc = file_get_contents($srcPath); $dst = imagecreatefromstring($dataDst); list($dstW, $dstH) = getimagesize($dstPath);list($srcW, $srcH) = getimagesize($srcPath); // 寬度是小圖的3倍 $thumbWidth = 3 * $srcW; $thumbHeight = $thumbWidth * ($dstH/$dstW); $data = \lib\Image::resize($dataDst, $thumbWidth, $thumbHeight, 0); if (!$data) {echo "resize failed\n";exit(1); } $dst = imagecreatefromstring($data); $info = getimagesize($dstPath); // 'mime' => 'image/jpeg' // 'mime' => 'image/png'$a = explode('/', $info['mime']); $type = array_pop($a); unset($a);$dstPath = "tmp.".$type; call_user_func("image".$type, $dst, $dstPath); // == 縮略圖生成完了 ==$dstW = $thumbWidth; $dstH = $thumbHeight; list($dstW, $dstH) = getimagesize($dstPath);$src = imagecreatefromstring($dataSrc);imagecopymerge($dst, $src,$dstW - $srcW, $dstH - $srcH,0, 0, $srcW, $srcH, 100);/* header("Content-Type: ".$info['mime']); // imagepng(); call_user_func("image".$type, $dst); */call_user_func("image".$type, $dst, "out.".$type);imagedestroy($src); imagedestroy($dst);* autoload.php
<?php /*** Created by PhpStorm.* User: mingzhanghui* Date: 9/24/2019* Time: 13:16*/$prefixList = ['lib'];$pwd = dirname(__FILE__);foreach ($prefixList as $prefix) {spl_autoload_register(function($class) use ($prefix, $pwd) {$base_dir = $pwd . DIRECTORY_SEPARATOR. str_replace('\\', '/', $prefix);// echo $base_dir.PHP_EOL;$len = strlen($prefix);if (strncmp($prefix, $class, $len) !== 0) {return;}$relative_class = substr($class, $len);$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';// echo $file.PHP_EOL;if (file_exists($file)) {require $file;}}); }* ./lib/Image.php
<?php /*** Created by PhpStorm.* User: mingzhanghui* Date: 9/24/2019* Time: 13:15*/namespace lib;class Image {/*** @param $imagedata string 圖像數據* @param $width int 縮放寬度* @param $height int 縮放高度* @param int $per 縮放比例,為0不縮放,>0忽略參數2、3的寬高* @return bool|string*/public static function resize($imagedata, $width, $height, $per = 0) {// 1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM// 獲取圖像信息list($bigWidth, $bigHight, $bigType) = getimagesizefromstring($imagedata);// 縮放比例if ($per > 0) {$width = $bigWidth * $per;$height = $bigHight * $per;}// 創建縮略圖畫板$block = imagecreatetruecolor($width, $height);// 啟用混色模式imagealphablending($block, false);// 保存PNG alpha通道信息imagesavealpha($block, true);// 創建原圖畫板$bigImg = imagecreatefromstring($imagedata);// 縮放imagecopyresampled($block, $bigImg, 0, 0, 0, 0,$width, $height, $bigWidth, $bigHight);// 生成臨時文件名$tmpFilename = tempnam(sys_get_temp_dir(), 'image_');// 保存switch ($bigType) {case 1: imagegif($block, $tmpFilename);break;case 2: imagejpeg($block, $tmpFilename);break;case 3: imagepng($block, $tmpFilename);break;}// 銷毀imagedestroy($block);$image = file_get_contents($tmpFilename);unlink($tmpFilename);return $image;} }?
把國旗圖片覆蓋到目的圖片右下角
?
總結
以上是生活随笔為你收集整理的PHP GD库 生成图片水印的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: java网络编程--URLEncode和
- 下一篇: 爬取图片,并按比例划分数据集
