PHP - 图像处理
第14章?處理圖像
?
學習要點:
1.創建圖像
2.簡單小案例
?
?
在PHP5中,動態圖象的處理要比以前容易得多。PHP5在php.ini文件中包含了GD擴展包,只需去掉GD擴展包的相應注釋就可以正常使用了。PHP5包含的GD庫正是升級的GD2庫,其中包含支持真彩圖像處理的一些有用的JPG功能。
一般生成的圖形,通過PHP的文檔格式存放,但可以通過HTML的圖片插入方式SRC來直接獲取動態圖形。比如,驗證碼、水印、微縮圖等。
?
一.創建圖像
?
創建圖像的一般流程:
1).設定標頭,告訴瀏覽器你要生成的MIME類型。
2).創建一個圖像區域,以后的操作都將基于此圖像區域。
3).在空白圖像區域繪制填充背景。
4).在背景上繪制圖形輪廓輸入文本。
5).輸出最終圖形。
6).清除所有資源。
7).其他頁面調用圖像。
?
設定標頭指定MIME輸出類型
<?php
?header('Content-Type:?image/png');
?>
?
創建一個空白的圖像區域
<?php
?$im?=?imagecreatetruecolor(200,200);
?>
?
在空白圖像區域繪制填充背景
<?php
?$blue?=?imagecolorallocate($im,0,102,255);
?imagefill($im,0,0,$blue);
?>
?
在背景上繪制圖形輪廓輸入文本
<?php
?$white?=?imagecolorallocate($im,255,255,255);
?imageline($im,0,0,200,200,$white);
?imageline($im,200,0,0,200,$white);
?imagestring($im,?5,?80,?20,?"Mr.Lee",?$white);
?>
?
輸出最終圖形
<?php
?imagepng($im);
?>
?
清除所有資源
<?php
?imagedestroy($im);
?>
?
其他頁面調用創建的圖形
<img?src="Demo4.php"?alt="PHP創建的圖片"?/>
?
?
二.簡單小案例
?
簡單驗證碼
<?php
header('Content-type:?image/png');
for($Tmpa=0;$Tmpa<4;$Tmpa++){
$nmsg.=dechex(rand(0,15));
}
$im?=?imagecreatetruecolor(75,25);
$blue?=?imagecolorallocate($im,0,102,255);
$white?=?imagecolorallocate($im,255,255,255);
imagefill($im,0,0,$blue);
imagestring($im,5,20,4,$nmsg,$white);
imagepng($im);
imagedestroy($im);
?>
?
加載已有的圖像
<?php
?header('Content-Type:image/png');
?define('__DIR__',dirname(__FILE__).'\\');
?$im?=?imagecreatefrompng(__DIR__.'222.png');
?$white?=?imagecolorallocate($im,255,255,255);
?imagestring($im,3,5,5,'http://www.yc60.com',$white);
?imagepng($im);
?imagedestroy($im);
?>
?
加載已有的系統字體
<?php
$text?=?iconv("gbk","utf-8","李炎恢");
$font?=?'C:\WINDOWS\Fonts\SIMHEI.TTF';
imagettftext($im,20,0,30,30,$white,$font,$text);
?>
?
圖像微縮
<?php
header('Content-type:?image/png');
define('__DIR__',dirname(__FILE__).'\\');
list($width,?$height)?=?getimagesize(__DIR__.'222.png');
$new_width?=?$width?*?0.7;
$new_height?=?$height?*?0.7;
$im2?=?imagecreatetruecolor($new_width,?$new_height);
$im?=?imagecreatefrompng(__DIR__.'222.png');
imagecopyresampled($im2,?$im,?0,?0,?0,?0,?
$new_width,?$new_height,?$width,?$height);
imagepng($im2);
imagedestroy($im);
Imagedestroy($im2);
?>
?
?
?
?
PS:掃一遍圖像函數手冊
?
?
?
===============================================================
?
?
<?php /* // * 創建圖像 *//第一步,設置輸出圖片的類型header('Content-Type:image/png');//第二步,創建一個空白的畫布,并返回畫布的句柄$img = imagecreatetruecolor(200, 200);//第三步,在空白的畫布上面填充背景顏色,并返回此時的句柄//創建背景顏色$background_color = imagecolorallocate($img,135,206,250 );//填充顏色imagefill($img, 0, 0, $background_color);//第四步,在背景上面,繪制文字,線條等其他圖形。//創建一個白顏色$white = imagecolorallocate($img, 255, 255, 255);//畫一條線imageline($img, 0, 0, 200, 200, $white);//也可以輸出字符串imagestring($img, 5, 40, 90, "HF_Ultrastrong", $white);//第五步,輸出創建的圖像,可以輸出png,jpeg,wbmp,gifimagepng($img);//第六步,銷毀所使用的資源imagedestroy($img); *//*// * 創建驗證碼 *//文件頭...header("Content-type: image/png");//創建真彩色白紙$im = @imagecreatetruecolor(50, 20) or die("建立圖像失敗");//獲取背景顏色$background_color = imagecolorallocate($im, 255, 255, 255);//填充背景顏色(這個東西類似油桶)imagefill($im,0,0,$background_color);//獲取邊框顏色$border_color = imagecolorallocate($im,200,200,200);//畫矩形,邊框顏色200,200,200imagerectangle($im,0,0,49,19,$border_color);//逐行炫耀背景,全屏用1或0for($i=2;$i<18;$i++){//獲取隨機淡色$line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));//畫線imageline($im,2,$i,47,$i,$line_color);}//設置字體大小$font_size=12;//設置印上去的文字$Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";$Str[1] = "abcdefghijklmnopqrstuvwxyz";$Str[2] = "01234567891234567890123456";//獲取第1個隨機文字$imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];$imstr[0]["x"] = rand(2,5);$imstr[0]["y"] = rand(1,4);//獲取第2個隨機文字$imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)];$imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);$imstr[1]["y"] = rand(1,3);//獲取第3個隨機文字$imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];$imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1);$imstr[2]["y"] = rand(1,4);//獲取第4個隨機文字$imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];$imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1);$imstr[3]["y"] = rand(1,3);//寫入隨機字串for($i=0;$i<4;$i++){//獲取隨機較深顏色$text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180));//畫文字imagechar($im,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"],$text_color);}//顯示圖片imagepng($im);//銷毀圖片imagedestroy($im); */ ?>?
轉載于:https://www.cnblogs.com/KTblog/p/4956403.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的PHP - 图像处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS 实现 jQuery的$(func
- 下一篇: grep 和 sed:linux经常使用