PHP操作圖像
創建、合并、添加水印等
PHP確實是一個簡單的語言,PHP能夠那么流行也是因為該語言能夠從簡單出發,用最簡單的語句就實現功能,摒棄了過多的繁瑣配置,寥寥幾句就可以實現JAVA需要寫一堆才能實現的功能;這個既是有點也是缺點,不過無論如何,PHP引領了一種潮流,就是簡單,simple is all;
php操作圖像最簡單的,就是給圖像加水印和加文字;
1,加水印:
<?php $one_path = 'be.jpg'; $two_path = 's.png'; //創建圖片的實例 $one = imagecreatefromstring(file_get_contents($one_path)); $two = imagecreatefromstring(file_get_contents($two_path)); //獲取水印圖片的寬高 list($two_w, $two_h) = getimagesize($two_path); // 將水印圖片復制到目標圖片上,最后個參數50是設置透明度,這里實現半透明效果 // imagecopymerge($one, $two, 800, 300, 0, 0, $two_w, $two_h, 100); // 如果水印圖片本身帶透明色,則使用imagecopy方法 imagecopy($one, $two, 800, 300, 0, 0, $two_w, $two_h); //輸出圖片 list($one_w, $one_h, $one_type) = getimagesize($one_path); switch ($one_type) { case 1://GIF header('Content-Type: image/gif'); imagegif($one); break; case 2://JPG header('Content-Type: image/jpeg'); imagejpeg($one); //$filename='mess.jpg'; //imagejpeg($one,$filename); break; case 3://PNG header('Content-Type: image/png'); imagepng($one); break; default: break; } imagedestroy($one); imagedestroy($two);
imagejpeg($img,$filename,qulity):這個關鍵方法有三個參數,第一個是圖像對象,第二個是圖像保存路徑,第三個是圖像質量;
如果直接顯示,只填第一個參數;如果保存成文件,填上第二個參數,比如D:/img/i.jpg,就會保存下來;
2,加文字:
<?php $dst_path = 'be.jpg'; //創建圖片的實例 $dst = imagecreatefromstring(file_get_contents($dst_path)); //打上文字 $font = './tt.ttf';//字體 $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字體顏色 imagefttext($dst, 100, 0, 100, 500, $black, $font, '快樂編程'); //輸出圖片 list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path); switch ($dst_type) { case 1://GIF header('Content-Type: image/gif'); imagegif($dst); break; case 2://JPG header('Content-Type: image/jpeg'); imagejpeg($dst); break; case 3://PNG header('Content-Type: image/png'); imagepng($dst); break; default: break; } imagedestroy($dst);
imagefttext ( resource $image , float $size , float $angle , int $x , int $y ,int $col , string $font_file , string $text [, array $extrainfo ] ):這個函數是關鍵
3,生成注冊碼:
<?php //echo PHP_VERSION; 輸出php版本 /**imagecreatetruecolor -- 新建一個真彩色圖像 imagecreatetruecolor() 返回一個圖像標識符,代表了一幅大小為 x_size 和 y_size 的黑色圖像。 * imagecolorallocate -- 為一幅圖像分配顏色 imagecolorallocate() 返回一個標識符,代表了由給定的 RGB 成分組成的顏色。 * imagefilledrectangle -- 畫一矩形并填充 imagefilledrectangle() 在 image 圖像中畫一個用 color 顏色填充了的矩形,其左上角坐標為 x1,y1,右下角坐標為 x2,y2。0, 0 是圖像的最左上角。 * imagepng -- 以 PNG 格式將圖像輸出到瀏覽器或文件 imagepng ( resource image [, string filename] ) * imagedestroy -- 銷毀一圖像 imagedestroy ( resource image )釋放與 image 關聯的內存。image 是由圖像創建函數返回的圖像標識符. * header 1.更改網頁頭部信息2.跳轉3.改成字符串 * imagerectangle -- 畫一個矩形 imagerectangle ( resource image, int x1, int y1, int x2, int y2, int col ) imagerectangle() 用 col 顏色在 image 圖像中畫一個矩形,其左上角坐標為 x1, y1,右下角坐標為 x2, y2。圖像的左上角坐標為 0, 0。 * imagesetpixel -- 畫一個單一像素 imagesetpixel ( resource image, int x, int y, int color ) imagesetpixel() 在 image 圖像中用 color 顏色在 x,y 坐標(圖像左上角為 0,0)上畫一個點。 * imagearc -- 畫橢圓弧 imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color ) imagearc() 以 cx,cy(圖像左上角為 0, 0)為中心在 image 所代表的圖像中畫一個橢圓弧。w 和 h 分別指定了橢圓的寬度和高度,起始和結束點以 s 和 e 參數以角度指定。0°位于三點鐘位置,以順時針方向繪畫。 * imagefttext -- 使用 FreeType 2 字體將文本寫入圖像 imagefttext ( resource image, float size尺寸, float angle角度, int x, int y, int col顏色, string font_file字體文件, string text [, array extrainfo]字符 ) * strlen -- Get string length strlen ( string string ) 獲取字符串長度 * * **/ //imagecreatetruecolor -- 新建一個真彩色圖像 $im = imagecreatetruecolor(300,200); //定義背景色imagecolorallocate(圖像資源,紅,綠,藍)0——255 。255.255.255 $back_color =imagecolorallocate($im,100,150,200); //imagefilledrectangle -- 畫一矩形并填充 imagefilledrectangle($im,0,0,300,200,$back_color); //$back_colors = imagecolorallocate($im,255,255,255); //imagerectangle -- 畫一個矩形 作為邊框 //imagerectangle($im, 2,2,298,198,$back_colors); for ($i=0;$i<800;$i++){ $point_color = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //imagesetpixel -- 畫一個單一像素 畫點 $point = imagesetpixel($im,mt_rand(0,300),mt_rand(0,200),$point_color); } //imagearc-- 畫橢圓弧 畫線 for($i=0;$i<300;$i++){ $line_color = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imagearc($im, mt_rand(0,500),mt_rand(0,500), mt_rand(0,500),mt_rand(0,500) ,mt_rand(0,360), mt_rand(0,360), $line_color); } //imagefttext -- 使用 FreeType 2 字體將文本寫入圖像 $str ="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW"; $code = ''; for($i=1;$i<5;$i++){ $code.=$str{mt_rand(0, strlen($str)-1)}; } $text_color = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //$str ='eng'; imagefttext($im,50, mt_rand(0,45),120,120, $text_color,'c:\\WINDOWS\\Fonts\\simsun.ttc',$code); //輸出圖像必須更改頭部信息 header('Cache-Control:max-age=1,s-maxage=1,no-cache,must-revalidate'); header('Content-type:image/png;charset=utf8'); //imagepng -- 以 PNG 格式將圖像輸出到瀏覽器或文件 imagepng($im); //imagedestroy -- 銷毀一圖像 imagedestroy($im);
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!