在php教程如果想利用它圖片處理函數就必須在php.ini裡面的gd庫開啟哦,
*/
//發送標頭檔
header("content-type: image/png");
//建立映像,如果失敗輸出內容
$im=@imagecreate(150,50) or die("cannot initialize new gd image stream");
//定義背景顏色
$background_color=imagecolorallocate($im,255,255,255);
//定義文字顏色
$text_color=imagecolorallocate($im,233,14,91);
//在映像上畫出檔案
imagestring($im,3,5,5,"hello world",$text_color);
//輸出影像檔
imagepng($im);
//銷毀映像
imagedestroy($im);
/*
該代碼的執行結果如圖22.5所示:
*/
// 2圖片等比例縮小
//定義一個檔案
$filename='1.jpg';
//定義縮放百分比
$percent=0.5;
//輸出標頭檔
header('content-type: image/jpeg');
//擷取新的大小
list($width,$height)=getimagesize($filename);
$newwidth=$width * $percent;
$newheight=$height * $percent;
//建立繪圖區域,並載入映像
$thumb=imagecreatetruecolor($newwidth,$newheight);
$source=imagecreatefromjpeg($filename);
//重新調整大小
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//輸出映像
imagejpeg($thumb);
/*
執行該代碼,將把原映像縮放50%,並以新映像輸出
*/
//在圖片上寫文字
//定義內容
$data='ivborw0kggoaaaansuheugaaabwaaaascamaaab/2u7waaaabl'.
'bmveuaaad///+l2z/daaaasuleqvr4xqwquqoaiaxc2/0vxzdr'.
'ex4ijtrkb7lobnustxsb0jixiamssqnwlsv+wulf4avk9flq2r'.
'8a5hse35q3eo2xp1a1wqkzsgetvdtkdqaaaabjru5erkjggg==';
//對內容進行base64編碼
$data=base64_decode($data);
//根據字串建立映像
$im=imagecreatefromstring($data);
if($im!== false)
{
//如果成功建立,則輸出映像
header('content-type: image/png');
imagepng($im);
}
else
{
//如果建立失敗,則輸出內容
echo 'an error occured.';
}
/*
該代碼的執行結果如圖:22.4所示:
*/
//在圖片上寫文字
header("content-type: image/png");
//建立映像,如果失敗輸出內容
$im=@imagecreate(100,50) or die("cannot initialize new gd image stream");
//定義背景顏色
$background_color=imagecolorallocate($im,255,255,255);
//定義文字顏色
$text_color=imagecolorallocate($im,233,14,91);
//在映像上畫出檔案
imagestring($im,1,5,5,"a simple text string",$text_color);
//輸出影像檔
imagepng($im);
//銷毀映像
imagedestroy($im);
/*
執行該代碼將產生一個jpeg映像。
並輸出指定字串
*/