本文實例講述了PHP的圖像處理。分享給大家供大家參考,具體如下:
1、添加文字水印
//1、打開圖片資源
$src="./material/sea.jpg";
$info=getimagesize($src);//獲取圖片信息
$type=image_type_to_extension($info[2],false);//轉化圖片類型
//var_dump($info);
$fun="imagecreatefrom{$type}";//拼接成為imagecreatefromjpeg()方法
$image=$fun($src);//新建GD圖片資源
//操作圖片
$font="./material/segoepr.ttf";
$content="@SuperTory";
$color=imagecolorallocate($image,255,255,255);
imagettftext($image,10,0,0,$info[1]-5,$color,$font,$content);//圖片上寫文字
//輸出圖片
header("content-type:".$info['mime']);//$imfo['mine']='image/jpeg'
$output="image{$type}";//拼接成為imagejpeg()方法
$output($image);//輸出到頁面
$output($image,'./material/watermarked.'.$type);//輸出到本地路徑
//銷毀圖片內存資源
imagedestroy($image);
2、壓縮圖像
//打開圖像
$src="./material/logo.png";
$info=getimagesize($src);
$type=image_type_to_extension($info[2],false);
$create="imagecreatefrom".$type;
$image=$create($src);
//壓縮
$tinyImg=imagecreatetruecolor(100,100); //新建壓縮后的圖像資源
//將原圖映射到壓縮后的圖像資源上
imagecopyresampled($tinyImg,$image,0,0,0,0,100,100,$info[0],$info[1]);
header("content-type:".$info['mime']);
$output="image{$type}";
//$output($image);
$output($tinyImg);
//銷毀
imagedestroy($image);
imagedestroy($tinyImg);
3、添加水印圖片
//獲取原圖片
$src="./material/sea.jpg";
$info=getimagesize($src);
$type=image_type_to_extension($info[2],false);
$create="imagecreatefrom".$type;
$image=$create($src);
//獲取水印圖片資源
$markSrc="./material/logo.png";
$markInfo=getimagesize($markSrc);
$markType=image_type_to_extension($markInfo[2],false);
$create="imagecreatefrom".$markType;
$markImage=$create($markSrc);
$tinyImg=imagecreatetruecolor(100,100);
imagecopyresampled($tinyImg,$markImage,0,0,0,0,
100,100,$markInfo[0],$markInfo[1]);
imagecopymerge($image,$tinyImg,$info[0]-100,$info[1]-100,
0,0,100,100,100);
//合并圖片:(原圖,水印圖,原圖x位置,原圖y位置,水印x起點,水印y起點,水印x終點,水印y終點,不透明度)
header("content-type:".$info['mime']);
$output="image{$type}";
$output($image);
imagedestroy($image);
imagedestroy($markImage);
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數組(Array)操作技巧大全》、《PHP數據結構與算法教程》、《php程序設計算法總結》、《PHP數學運算技巧總結》、《php字符串(string)用法總結》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:- Python基于pillow庫實現生成圖片水印
- vue中使用帶隱藏文本信息的圖片、圖片水印的方法
- Unity實現圖片水印生成
- PHP圖像處理 imagestring添加圖片水印與文字水印操作示例
- Java在Word中添加多行圖片水印