【PHP的圖像處理】文字水印、圖片水印、壓縮圖像等實例


創建圖片資源

imagecreatetruecolor(width,height);
imagecreatefromgif(圖片名稱);
imagecreatefrompng(圖片名稱);
imagecreatefromjpeg(圖片名稱);畫出各種圖像 imagegif(圖片資源,保存路徑);
imagepng()
imagejpeg();

獲取圖片屬性

imagesx(res//寬度
imagesy(res//高度
getimagesize(文件路徑)
返回一個具有四個單元的數組。索引 0 包含圖像寬度的像素值,索引 1 包含圖像高度的像素值。索引 2 是圖像類型的標記: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。這些標記與 PHP 4.3.0 新加的 IMAGETYPE 常量對應。索引 3 是文本字符串,內容為“height="yyy" width="xxx"”,可直接用於 IMG 標記。
銷毀圖像資源
imagedestroy(圖片資源);

透明處理

PNG、jpeg透明色都正常,只有gif不正常
imagecolortransparent(resource image [,int color])//將某個顏色設置成透明色
imagecolorstotal()
imagecolorforindex();

圖片的裁剪

imagecopyresized()
imagecopyresampled();

加水印(文字、圖片)

字符串編碼轉換string iconv ( string $in_charset , string $out_charset , string $str )

圖片旋轉

imagerotate();//制定角度的圖片翻轉

圖片的翻轉

沿X軸 沿Y軸翻轉

銳化

imagecolorsforindex()
imagecolorat()
在圖片上畫圖形 $img=imagecreatefromgif("./images/map.gif");

以下PHP的圖像處理實例分享給大家供大家參考:

1、添加文字水印

//1、打開圖片資源
  $src="./images/logo.png";
  $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="./images/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="./images/logo.png";
$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);

原文來源:https://www.newbii.cn/20200428100000.htm

 


免責聲明!

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



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