范例
Example #1 使用PHP創(chuàng)建PNG格式圖片
<?phpheader('Content-type: image/png');$string = $_GET[’text’];$im = imagecreatefrompng('images/button1.png');$orange = imagecolorallocate($im, 220, 210, 60);$px = (imagesx($im) - 7.5 * strlen($string)) / 2;imagestring($im, 3, $px, 9, $string, $orange);imagepng($im);imagedestroy($im);?>
Example #2 使用 alpha channels 添加水印到圖片
<?php// Load the stamp and the photo to apply the watermark to$stamp = imagecreatefrompng(’stamp.png’);$im = imagecreatefromjpeg(’photo.jpeg’);// Set the margins for the stamp and get the height/width of the stamp image$marge_right = 10;$marge_bottom = 10;$sx = imagesx($stamp);$sy = imagesy($stamp);// Copy the stamp image onto our photo using the margin offsets and the photo// width to calculate positioning of the stamp.imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));// Output and free memoryheader(’Content-type: image/png’);imagepng($im);imagedestroy($im);?>

Example #3 使用 imagecopymerge() 創(chuàng)建透明水印
<?php// Load the stamp and the photo to apply the watermark to$im = imagecreatefromjpeg(’photo.jpeg’);// First we create our stamp image manually from GD$stamp = imagecreatetruecolor(100, 70);imagefilledrectangle($stamp, 0, 0, 99, 69, 0x0000FF);imagefilledrectangle($stamp, 9, 9, 90, 60, 0xFFFFFF);$im = imagecreatefromjpeg(’photo.jpeg’);imagestring($stamp, 5, 20, 20, ’libGD’, 0x0000FF);imagestring($stamp, 3, 20, 40, ’(c) 2007-9’, 0x0000FF);// Set the margins for the stamp and get the height/width of the stamp image$marge_right = 10;$marge_bottom = 10;$sx = imagesx($stamp);$sy = imagesy($stamp);// Merge the stamp onto our photo with an opacity of 50%imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);// Save the image to file and free memoryimagepng($im, ’photo_stamp.png’);imagedestroy($im);?>

相關(guān)函數(shù)gd_info — 取得當(dāng)前安裝的 GD 庫(kù)的信息getimagesize — 取得圖像大小getimagesizefromstring — Get the size of an image from a stringimage_type_to_extension — 取得圖像類(lèi)型的文件后綴image_type_to_mime_type — 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的圖像類(lèi)型的 MIME 類(lèi)型image2wbmp — 以 WBMP 格式將圖像輸出到瀏覽器或文件imageaffine — Return an image containing the affine tramsformed src image, using an optional clipping areaimageaffinematrixconcat — Concat two matrices (as in doing many ops in one go)imageaffinematrixget — Return an image containing the affine tramsformed src image, using an optional clipping areaimagealphablending — 設(shè)定圖像的混色模式imageantialias — 是否使用抗鋸齒(antialias)功能imagearc — 畫(huà)橢圓弧imagechar — 水平地畫(huà)一個(gè)字符imagecharup — 垂直地畫(huà)一個(gè)字符imagecolorallocate — 為一幅圖像分配顏色imagecolorallocatealpha — 為一幅圖像分配顏色 + alphaimagecolorat — 取得某像素的顏色索引值imagecolorclosest — 取得與指定的顏色最接近的顏色的索引值imagecolorclosestalpha — 取得與指定的顏色加透明度最接近的顏色imagecolorclosesthwb — 取得與給定顏色最接近的色度的黑白色的索引imagecolordeallocate — 取消圖像顏色的分配imagecolorexact — 取得指定顏色的索引值imagecolorexactalpha — 取得指定的顏色加透明度的索引值imagecolormatch — 使一個(gè)圖像中調(diào)色板版本的顏色與真彩色版本更能匹配imagecolorresolve — 取得指定顏色的索引值或有可能得到的最接近的替代值imagecolorresolvealpha — 取得指定顏色 + alpha 的索引值或有可能得到的最接近的替代值imagecolorset — 給指定調(diào)色板索引設(shè)定顏色imagecolorsforindex — 取得某索引的顏色imagecolorstotal — 取得一幅圖像的調(diào)色板中顏色的數(shù)目imagecolortransparent — 將某個(gè)顏色定義為透明色imageconvolution — 用系數(shù) div 和 offset 申請(qǐng)一個(gè) 3x3 的卷積矩陣imagecopy — 拷貝圖像的一部分imagecopymerge — 拷貝并合并圖像的一部分imagecopymergegray — 用灰度拷貝并合并圖像的一部分imagecopyresampled — 重采樣拷貝部分圖像并調(diào)整大小imagecopyresized — 拷貝部分圖像并調(diào)整大小imagecreate — 新建一個(gè)基于調(diào)色板的圖像imagecreatefromgd2 — 從 GD2 文件或 URL 新建一圖像imagecreatefromgd2part — 從給定的 GD2 文件或 URL 中的部分新建一圖像imagecreatefromgd — 從 GD 文件或 URL 新建一圖像imagecreatefromgif — 由文件或 URL 創(chuàng)建一個(gè)新圖象。imagecreatefromjpeg — 由文件或 URL 創(chuàng)建一個(gè)新圖象。imagecreatefrompng — 由文件或 URL 創(chuàng)建一個(gè)新圖象。imagecreatefromstring — 從字符串中的圖像流新建一圖像imagecreatefromwbmp — 由文件或 URL 創(chuàng)建一個(gè)新圖象。imagecreatefromwebp — 由文件或 URL 創(chuàng)建一個(gè)新圖象。imagecreatefromxbm — 由文件或 URL 創(chuàng)建一個(gè)新圖象。imagecreatefromxpm — 由文件或 URL 創(chuàng)建一個(gè)新圖象。imagecreatetruecolor — 新建一個(gè)真彩色圖像imagecrop — Crop an image using the given coordinates and size, x, y, width and heightimagecropauto — Crop an image automatically using one of the available modesimagedashedline — 畫(huà)一虛線imagedestroy — 銷(xiāo)毀一圖像imageellipse — 畫(huà)一個(gè)橢圓imagefill — 區(qū)域填充imagefilledarc — 畫(huà)一橢圓弧且填充imagefilledellipse — 畫(huà)一橢圓并填充imagefilledpolygon — 畫(huà)一多邊形并填充imagefilledrectangle — 畫(huà)一矩形并填充imagefilltoborder — 區(qū)域填充到指定顏色的邊界為止imagefilter — 對(duì)圖像使用過(guò)濾器imageflip — Flips an image using a given modeimagefontheight — 取得字體高度imagefontwidth — 取得字體寬度imageftbbox — 給出一個(gè)使用 FreeType 2 字體的文本框imagefttext — 使用 FreeType 2 字體將文本寫(xiě)入圖像imagegammacorrect — 對(duì) GD 圖像應(yīng)用 gamma 修正imagegd2 — 將 GD2 圖像輸出到瀏覽器或文件imagegd — 將 GD 圖像輸出到瀏覽器或文件imagegif — 輸出圖象到瀏覽器或文件。imagegrabscreen — Captures the whole screenimagegrabwindow — Captures a windowimageinterlace — 激活或禁止隔行掃描imageistruecolor — 檢查圖像是否為真彩色圖像imagejpeg — 輸出圖象到瀏覽器或文件。imagelayereffect — 設(shè)定 alpha 混色標(biāo)志以使用綁定的 libgd 分層效果imageline — 畫(huà)一條線段imageloadfont — 載入一新字體imagepalettecopy — 將調(diào)色板從一幅圖像拷貝到另一幅imagepalettetotruecolor — Converts a palette based image to true colorimagepng — 以 PNG 格式將圖像輸出到瀏覽器或文件imagepolygon — 畫(huà)一個(gè)多邊形imagepsbbox — 給出一個(gè)使用 PostScript Type1 字體的文本方框imagepsencodefont — 改變字體中的字符編碼矢量imagepsextendfont — 擴(kuò)充或精簡(jiǎn)字體imagepsfreefont — 釋放一個(gè) PostScript Type 1 字體所占用的內(nèi)存imagepsloadfont — 從文件中加載一個(gè) PostScript Type 1 字體imagepsslantfont — 傾斜某字體imagepstext — 用 PostScript Type1 字體把文本字符串畫(huà)在圖像上imagerectangle — 畫(huà)一個(gè)矩形imagerotate — 用給定角度旋轉(zhuǎn)圖像imagesavealpha — 設(shè)置標(biāo)記以在保存 PNG 圖像時(shí)保存完整的 alpha 通道信息(與單一透明色相反)imagescale — Scale an image using the given new width and heightimagesetbrush — 設(shè)定畫(huà)線用的畫(huà)筆圖像imagesetinterpolation — Set the interpolation methodimagesetpixel — 畫(huà)一個(gè)單一像素imagesetstyle — 設(shè)定畫(huà)線的風(fēng)格imagesetthickness — 設(shè)定畫(huà)線的寬度imagesettile — 設(shè)定用于填充的貼圖imagestring — 水平地畫(huà)一行字符串imagestringup — 垂直地畫(huà)一行字符串imagesx — 取得圖像寬度imagesy — 取得圖像高度imagetruecolortopalette — 將真彩色圖像轉(zhuǎn)換為調(diào)色板圖像imagettfbbox — 取得使用 TrueType 字體的文本的范圍imagettftext — 用 TrueType 字體向圖像寫(xiě)入文本imagetypes — 返回當(dāng)前 PHP 版本所支持的圖像類(lèi)型imagewbmp — 以 WBMP 格式將圖像輸出到瀏覽器或文件imagewebp — Output an WebP image to browser or fileimagexbm — 將 XBM 圖像輸出到瀏覽器或文件iptcembed — 將二進(jìn)制 IPTC 數(shù)據(jù)嵌入到一幅 JPEG 圖像中iptcparse — 將二進(jìn)制 IPTC 塊解析為單個(gè)標(biāo)記jpeg2wbmp — 將 JPEG 圖像文件轉(zhuǎn)換為 WBMP 圖像文件png2wbmp — 將 PNG 圖像文件轉(zhuǎn)換為 WBMP 圖像文件