php擴(kuò)展庫:C:\wamp\bin\php\php5.3.10\ext
<?php
/*
創(chuàng)建畫布
參數(shù)1:畫布寬度
參數(shù)2:畫布高度
返回值:圖像資源類型
*/
$image = imagecreatetruecolor(500, 500);
/*
創(chuàng)建顏色
參數(shù)1:圖像資源
參數(shù)234:紅綠藍(lán)三種顏色 0-255 0x00-0xff
*/
$red = imagecolorallocate($image, 255, 0, 0);
$green = imagecolorallocate($image, 0, 255, 0);
$blue = imagecolorallocate($image, 0, 0, 255);
$pink = imagecolorallocate($image, 0xff, 0xc0, 0xcb);
$yellow = imagecolorallocate($image, 255, 255, 0);
$black = imagecolorallocate($image, 0, 0, 0);
/*
給畫布填充顏色
*/
//imagefill($image, 0, 0, $red);
imagefilledrectangle($image, 0, 0, 500, 500, $pink);
/*
通過圖像處理函數(shù)畫圖形
*/
imageline($image, 0, 0, 500, 500, $yellow);
/*
畫矩形
*/
imagerectangle($image, 50, 50, 150, 150, $blue);
imagerectangle($image, 350, 50, 450, 150, $blue);
/*
畫多邊形
*/
imagepolygon($image, [250, 150, 150, 250, 350, 250], 3, $red);
/*
畫橢圓
*/
imageellipse($image, 250, 350, 200, 50, $green);
imagefilledellipse($image, 100, 100, 30, 30, $black);
/*
畫弧線
角度為度數(shù),方向為順時針,0度位于3點鐘方向
*/
imagearc($image, 250, 250, 200, 200, -90, 90, $red);
/*
畫字符串
*/
imagestring($image, 5, 50, 200, '我愛 baby', $black);
imagechar($image, 5, 100, 300, 'c', $black);
/*
畫中文字符
*/
imagettftext($image, 50, 10, 0, 100, $red, 'STXINGKA.TTF', '我愛你中國');
/*
告知MIME類型
*/
header('content-type:image/png');
/*
輸出或者保存
參數(shù)1:圖像資源
參數(shù)2:如果不傳遞,代表輸出到瀏覽器励幼,如果傳遞,要傳遞一個文件路徑口柳,將該圖像保存到該文件中
*/
imagepng($image);
/*
銷毀圖像資源
*/
imagedestroy($image);
1苹粟、圖像應(yīng)用
2、基本概念
MIME:多用途互聯(lián)網(wǎng)郵件擴(kuò)展類型跃闹,主要用來在郵件傳輸和http協(xié)議中指定文件類型
圖片類型
比如常見的MIME類型
html: .html text/html
png: .png image/png
jpg嵌削、jpeg image/jpeg
gif image/gif
3毛好、gd庫 php擴(kuò)展
配置文件 php.ini 中通過 extension = xxx.dll 打開或者關(guān)閉擴(kuò)展庫
在安裝目錄下 wamp64=>bin=>php7.0=>ext 存放的所有的擴(kuò)展庫
4、六脈神劍
1苛秕、創(chuàng)建畫布(就是一個圖像資源)
2肌访、創(chuàng)建顏色
3、通過圖像處理函數(shù)畫圖
4艇劫、告知瀏覽器文件MIME類型
5吼驶、輸出到瀏覽器或者保存到本地
6、銷毀圖像資源
5店煞、相關(guān)函數(shù)
創(chuàng)建圖像資源函數(shù):
imagecreate(推薦使用創(chuàng)建真彩色函數(shù))
imagecreatetruecolor
imagecreatefromjpeg
imagecreatefromgif
imagecreatefromwbmp
imagecreatefrompng
創(chuàng)建顏色函數(shù)
imagecolorallocate
畫圖形函數(shù)
imagefilledrectangle :畫一個矩形并且填充顏色
imagesetpixel :畫一個像素點
imageline
imagerectangle
imagepolygon
imageellipse
imagearc
下面的都是上面的填充版本
imagefilledrectangle
imagefilledpolygon
imagefilledellipse
imagefilledarc
imagerotate:旋轉(zhuǎn)圖片
imagestring :不能寫中文
imagechar:畫一個字符
imagettftext:
告知瀏覽器MIME類型
header('Content-Type:image/png');
輸出保存圖片
imagepng($img,['path'])
imagejpeg
imagegif
銷毀圖像資源
imagedestroy
獲取圖片的寬度和高度
list($width, $height) = getimagesize(filename);
/*
imagecopy
imagecopymerge 帶透明度的拷貝
imagecopyresampled 重采樣進(jìn)行拷貝
*/
//imagecopymerge($dst, $src, 370, 677, 112, 80, 200, 85, 50);
imagecopyresampled($dst, $src, 370, 677, 112, 80, 200, 100, 200, 85);