一、圖片添加文字水印:
<?php $bigImgPath = 'background.png'; $img = imagecreatefromstring(file_get_contents($bigImgPath)); $font = 'msyh.ttf';//字體,字體文件需保存到相應文件夾下 $black = imagecolorallocate($img, 0, 0, 0);//字體顏色 RGB $fontSize = 12; //字體大小 $circleSize = 60; //旋轉角度 $left = 50; //左邊距 $top = 150; //頂邊距 imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font, 'zhix.net| 智昕網絡'); list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath); switch ($bgType) { case 1: //gif header('Content-Type:image/gif'); imagegif($img); break; case 2: //jpg header('Content-Type:image/jpg'); imagejpeg($img); break; case 3: //png header('Content-Type:image/png'); imagepng($img,"images/circle.png"); //在 images 目錄下就會生成一個 circle.png 文件,上面也可設置相應的保存目錄及文件名。 break; default: break; } imagedestroy($img);
效果如下:
二、合成二維碼
<?php
$bigImgPath = 'background.png';
$qCodePath = 'code.png';
$bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
$qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
imagesavealpha($bigImg,true);//假如是透明PNG圖片,這里很重要 意思是不要丟了圖像的透明
list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
// imagecopymerge使用注解
imagecopymerge($bigImg, $qCodeImg, 50, 50, 0, 0, $qCodeWidth, $qCodeHight, 100);
list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);
switch ($bigType) {
case 1: //gif
header('Content-Type:image/gif');
imagegif($bigImg);
break;
case 2: //jpg
header('Content-Type:image/jpg');
imagejpeg($bigImg);
break;
case 3: //jpg
header('Content-Type:image/png');
imagepng($bigImg,"images/circle.png"); //在 images 目錄下就會生成一個 circle.png 文件,上面也可設置相應的保存目錄及文件名。
break;
default:
# code...
break;
}
imagedestroy($bigImg);
imagedestroy($qcodeImg);
合成的效果如下:
如果感覺上面的二維碼太大 那么可以先生成一張小的二維碼圖片,下面是處理的方法
<?php $bigImgPath = 'background.png'; $qCodePath = 'code.png'; imagepress($qCodePath); $qCodePath = 'code1.png'; $bigImg = imagecreatefromstring(file_get_contents($bigImgPath)); $qCodeImg = imagecreatefromstring(file_get_contents($qCodePath)); //$qCodeImg = myImageResize($qCodeImg, 200, 200); //最大寬高 list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath); // imagecopymerge使用注解 imagecopymerge($bigImg, $qCodeImg, 50, 50, 0, 0, $qCodeWidth, $qCodeHight, 100); list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath); switch ($bigType) { case 1: //gif header('Content-Type:image/gif'); imagegif($bigImg); break; case 2: //jpg header('Content-Type:image/jpg'); imagejpeg($bigImg); break; case 3: //jpg header('Content-Type:image/png'); imagepng($bigImg,"images/circle.png"); //在 images 目錄下就會生成一個 circle.png 文件,上面也可設置相應的保存目錄及文件名。 break; default: # code... break; } imagedestroy($bigImg); imagedestroy($qcodeImg); //等比例生成圖片 //$filepath圖片路徑,$percent縮放百分比 function imagepress($filepath,$percent='0.5'){ // 圖片類型,這里的二維碼是PNG的 所以使用PNG類型 header('Content-Type: image/png'); // 獲得新的圖片大小 list($width, $height) = getimagesize($filepath); //取圖片信息 $new_width = $width * $percent; $new_height = $height * $percent; // 重新取樣 $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefrompng($filepath); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // 輸出 return imagejpeg($image_p, 'code1.png', 100); //這里的名命可根據需要設置 }
效果如下:
imagecopymerge語法:
imagecopymerge( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct )
imagecopymerge() 函數用於拷貝並合並圖像的一部分,成功返回 TRUE ,否則返回 FALSE
參數 | 說明 |
---|---|
dst_im | 目標圖像 |
src_im | 被拷貝的源圖像 |
dst_x | 目標圖像開始 x 坐標 |
dst_y | 目標圖像開始 y 坐標,x,y同為 0 則從左上角開始 |
src_x | 拷貝圖像開始 x 坐標 |
src_y | 拷貝圖像開始 y 坐標,x,y同為 0 則從左上角開始拷貝 |
src_w | (從 src_x 開始)拷貝的寬度 |
src_h | (從 src_y 開始)拷貝的高度 |
pct | 圖像合並程度,取值 0-100 ,當 pct=0 時,實際上什么也沒做,反之完全合並。 |
當為 pct = 100 時對於調色板圖像本函數和 imagecopy() 完全一樣,參考閱讀:
imagecopy():拷貝圖像或圖像的一部分。
該函數的一個典型應用就是將圖像打上水印標識:
<? header("Content-type: image/jpeg"); //原始圖像 $dst = "images/flower_1.jpg"; //得到原始圖片信息 $dst_im = imagecreatefromjpeg($dst); $dst_info = getimagesize($dst); //水印圖像 $src = "images/logo.gif"; $src_im = imagecreatefromgif($src); $src_info = getimagesize($src); //水印透明度 $alpha = 30; //合並水印圖片 imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0],$dst_info[1]-$src_info[1],0,0,$src_info[0], $src_info[1],$alpha); //輸出合並后水印圖片 imagejpeg($dst_im); imagedestroy($dst_im); imagedestroy($src_im); ?>