您的位置:首页技术文章
文章详情页

PHP扩展之图像处理1——GD库使用及相关函数

【字号: 日期:2022-09-15 17:01:27浏览:4作者:猪猪
范例

Example #1 使用PHP创建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);?>

PHP扩展之图像处理1——GD库使用及相关函数

Example #3 使用 imagecopymerge() 创建透明水印

<?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);?>

PHP扩展之图像处理1——GD库使用及相关函数

相关函数gd_info — 取得当前安装的 GD 库的信息getimagesize — 取得图像大小getimagesizefromstring — Get the size of an image from a stringimage_type_to_extension — 取得图像类型的文件后缀image_type_to_mime_type — 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的图像类型的 MIME 类型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 — 设定图像的混色模式imageantialias — 是否使用抗锯齿(antialias)功能imagearc — 画椭圆弧imagechar — 水平地画一个字符imagecharup — 垂直地画一个字符imagecolorallocate — 为一幅图像分配颜色imagecolorallocatealpha — 为一幅图像分配颜色 + alphaimagecolorat — 取得某像素的颜色索引值imagecolorclosest — 取得与指定的颜色最接近的颜色的索引值imagecolorclosestalpha — 取得与指定的颜色加透明度最接近的颜色imagecolorclosesthwb — 取得与给定颜色最接近的色度的黑白色的索引imagecolordeallocate — 取消图像颜色的分配imagecolorexact — 取得指定颜色的索引值imagecolorexactalpha — 取得指定的颜色加透明度的索引值imagecolormatch — 使一个图像中调色板版本的颜色与真彩色版本更能匹配imagecolorresolve — 取得指定颜色的索引值或有可能得到的最接近的替代值imagecolorresolvealpha — 取得指定颜色 + alpha 的索引值或有可能得到的最接近的替代值imagecolorset — 给指定调色板索引设定颜色imagecolorsforindex — 取得某索引的颜色imagecolorstotal — 取得一幅图像的调色板中颜色的数目imagecolortransparent — 将某个颜色定义为透明色imageconvolution — 用系数 div 和 offset 申请一个 3x3 的卷积矩阵imagecopy — 拷贝图像的一部分imagecopymerge — 拷贝并合并图像的一部分imagecopymergegray — 用灰度拷贝并合并图像的一部分imagecopyresampled — 重采样拷贝部分图像并调整大小imagecopyresized — 拷贝部分图像并调整大小imagecreate — 新建一个基于调色板的图像imagecreatefromgd2 — 从 GD2 文件或 URL 新建一图像imagecreatefromgd2part — 从给定的 GD2 文件或 URL 中的部分新建一图像imagecreatefromgd — 从 GD 文件或 URL 新建一图像imagecreatefromgif — 由文件或 URL 创建一个新图象。imagecreatefromjpeg — 由文件或 URL 创建一个新图象。imagecreatefrompng — 由文件或 URL 创建一个新图象。imagecreatefromstring — 从字符串中的图像流新建一图像imagecreatefromwbmp — 由文件或 URL 创建一个新图象。imagecreatefromwebp — 由文件或 URL 创建一个新图象。imagecreatefromxbm — 由文件或 URL 创建一个新图象。imagecreatefromxpm — 由文件或 URL 创建一个新图象。imagecreatetruecolor — 新建一个真彩色图像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 — 画一虚线imagedestroy — 销毁一图像imageellipse — 画一个椭圆imagefill — 区域填充imagefilledarc — 画一椭圆弧且填充imagefilledellipse — 画一椭圆并填充imagefilledpolygon — 画一多边形并填充imagefilledrectangle — 画一矩形并填充imagefilltoborder — 区域填充到指定颜色的边界为止imagefilter — 对图像使用过滤器imageflip — Flips an image using a given modeimagefontheight — 取得字体高度imagefontwidth — 取得字体宽度imageftbbox — 给出一个使用 FreeType 2 字体的文本框imagefttext — 使用 FreeType 2 字体将文本写入图像imagegammacorrect — 对 GD 图像应用 gamma 修正imagegd2 — 将 GD2 图像输出到浏览器或文件imagegd — 将 GD 图像输出到浏览器或文件imagegif — 输出图象到浏览器或文件。imagegrabscreen — Captures the whole screenimagegrabwindow — Captures a windowimageinterlace — 激活或禁止隔行扫描imageistruecolor — 检查图像是否为真彩色图像imagejpeg — 输出图象到浏览器或文件。imagelayereffect — 设定 alpha 混色标志以使用绑定的 libgd 分层效果imageline — 画一条线段imageloadfont — 载入一新字体imagepalettecopy — 将调色板从一幅图像拷贝到另一幅imagepalettetotruecolor — Converts a palette based image to true colorimagepng — 以 PNG 格式将图像输出到浏览器或文件imagepolygon — 画一个多边形imagepsbbox — 给出一个使用 PostScript Type1 字体的文本方框imagepsencodefont — 改变字体中的字符编码矢量imagepsextendfont — 扩充或精简字体imagepsfreefont — 释放一个 PostScript Type 1 字体所占用的内存imagepsloadfont — 从文件中加载一个 PostScript Type 1 字体imagepsslantfont — 倾斜某字体imagepstext — 用 PostScript Type1 字体把文本字符串画在图像上imagerectangle — 画一个矩形imagerotate — 用给定角度旋转图像imagesavealpha — 设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息(与单一透明色相反)imagescale — Scale an image using the given new width and heightimagesetbrush — 设定画线用的画笔图像imagesetinterpolation — Set the interpolation methodimagesetpixel — 画一个单一像素imagesetstyle — 设定画线的风格imagesetthickness — 设定画线的宽度imagesettile — 设定用于填充的贴图imagestring — 水平地画一行字符串imagestringup — 垂直地画一行字符串imagesx — 取得图像宽度imagesy — 取得图像高度imagetruecolortopalette — 将真彩色图像转换为调色板图像imagettfbbox — 取得使用 TrueType 字体的文本的范围imagettftext — 用 TrueType 字体向图像写入文本imagetypes — 返回当前 PHP 版本所支持的图像类型imagewbmp — 以 WBMP 格式将图像输出到浏览器或文件imagewebp — Output an WebP image to browser or fileimagexbm — 将 XBM 图像输出到浏览器或文件iptcembed — 将二进制 IPTC 数据嵌入到一幅 JPEG 图像中iptcparse — 将二进制 IPTC 块解析为单个标记jpeg2wbmp — 将 JPEG 图像文件转换为 WBMP 图像文件png2wbmp — 将 PNG 图像文件转换为 WBMP 图像文件
标签: PHP
相关文章: