日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP GD库 生成图片水印

發布時間:2024/1/8 php 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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库 生成图片水印的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。