一、創建一個圖像應該完成如下所示的四個基本步驟:
1.創建圖像
imagecreatetruecolor()//新建一個真彩色圖像
打開服務器或網絡文件中已經存在的GIF,JPEG,PNG,WBMP格式圖像
imagecreatefromjpeg()imagecreatefrompng()imagecreatefromgif()imagecreatefromwbmp()
創建或者打開失敗的時候會返回空字符串,并且輸出一條錯誤信息。
imagesx()//輸出畫布寬度imagesy()//輸出畫布高度getimagesize()//取得圖像大小
2.繪制圖像
圖像創建完成以后,就可以通過這個圖像資源,使用各種畫像函數設置圖像的顏色、填充圖像、畫點、線段、以及向圖像的添加文本等
1.imagecolorallocate()//分配顏色2.imagefill()//區域填充3.imagesetpixel()//畫一個單一像素4.imageline()//畫一條線段5.imagerectangle()//畫一個矩形6.imagestring()//水平地畫一行字符串7.imagettftext()//用 TrueType 字體向圖像寫入文本8.imagettfbbox()//計算 TrueType 文字所占區域9.imagecopy()//拷貝圖像的一部分10.imagecopymerge()//拷貝并合并圖像的一部分11.imagecopyresampled()//重采樣拷貝部分圖像并調整大小
3.輸出圖像
header('Content-type:image/jpeg');//輸出圖像為jpeg時
header函數注意點
在該函數之前,不能輸出任何內容
在我們的PHP代碼 的函數里面,我們使用的/開頭的路徑 這個/不是指 web根目錄,而是操作系統的 文件的根目錄!
4.釋放資源
二、驗證碼
PS:驗證碼的目的是為了防止暴力注入
<?php
header('Content-type:image/jpeg');
$width=120;
$height=40;
$element=array('a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$string='';
for ($i=0;$i<5;$i++){$string.=$element[rand(0,count($element)-1)];
}
$img=imagecreatetruecolor($width, $height);
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorString=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
imagefill($img,0,0,$colorBg);
imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);
for($i=0;$i<100;$i++){imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
}
for($i=0;$i<3;$i++){imageline($img,rand(0,$width/2),rand(0,$height),rand($width/2,$width),rand(0,$height),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
}
//imagestring($img,5,0,0,'abcd',$colorString);
imagettftext($img,14,rand(-5,5),rand(5,15),rand(30,35),$colorString,'font/SketchyComic.ttf',$string);
imagejpeg($img);
imagedestroy($img);
三、水印
header('Content-type:text/html;charset:utf-8');//設置編碼
header('Content-type:image/jpeg');//輸出圖像為jpeg時$img=imagecreatefromjpeg('images/1.jpg');//打開images文件下的1.jpg圖片(也可以打開瀏覽器上的圖片,寫圖片地址)
$color=imagecolorallocate($img,255,255,255);//顏色$width=imagesx($img);//取得圖像寬度
$height=imagesy($img);//取得圖像高度
$position=imagettfbbox(20,0,'font/Inkfree.TTF','xzq');
$stringWidth=$position[2]-$position[0];//水印寬度imagettftext($img,20,0,$width-1-$stringWidth-($width/30),$height-1-($height/30), $color,'font/Inkfree.TTF','xzq');
//$width-1-$stringWidth-($width/30),$height-1-($height/30)把水印放在右下角
imagejpeg($img);//輸出圖像
imagedestroy($img);//釋放資源
四、縮放與剪裁
imagecopyresampled()函數
采樣某個圖像資源的 某一部分 到 另外一個圖像資源上面去
header('Content-type:image/jpeg');
$width=300;
$img=imagecreatefromjpeg('images/zcx.jpg');
$imgWidth=imagesx($img);
$imgHeight=imagesy($img);
$height=$width/($imgWidth/$imgHeight);
$img1=imagecreatetruecolor($width,$height);
imagecopyresampled($img1,$img,0,0,0,0,$width,$height,$imgWidth,$imgHeight);
if(imagejpeg($img1)){imagejpeg($img1,'images/zoom_zcx.jpg');
}
imagedestroy($img);
imagedestroy($img1);
總結
以上是生活随笔為你收集整理的PHP—图像处理的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。