图像处理和 GD
- 简介
- 安装/配置
- 预定义常量
- 示例
- GD 和图像处理 函数
- gd_info — 取得当前安装的 GD 库的信息
- getimagesize — 取得图像大小
- getimagesizefromstring — 从字符串中获取图像尺寸信息
- image_type_to_extension — 取得图像类型的文件后缀
- image_type_to_mime_type — 取得 getimagesize、exif_read_data、exif_thumbnail、exif_imagetype 所返回的图像类型的 MIME 类型
- image2wbmp — 输出图象到浏览器或文件。
- imageaffine — 返回经过仿射变换后的图像,剪切区域可选
- imageaffinematrixconcat — Concatenate two affine transformation matrices
- imageaffinematrixget — Get an affine transformation matrix
- imagealphablending — 设定图像的混色模式
- imageantialias — 是否使用抗锯齿(antialias)功能
- imagearc — 画弧线
- imageavif — 输出图象到浏览器或文件。
- imagebmp — Output a BMP image to browser or file
- imagechar — 水平地绘制一个字符
- imagecharup — 垂直地绘制一个字符
- imagecolorallocate — 为图像分配颜色
- imagecolorallocatealpha — 为图像分配颜色
- imagecolorat — 取得某像素的颜色索引值
- imagecolorclosest — 取得与指定的颜色最接近的颜色索引值
- imagecolorclosestalpha — 获取最接近指定颜色 + alpha 的颜色索引
- imagecolorclosesthwb — 取得与给定颜色最接近的色度的黑白色的索引
- imagecolordeallocate — 取消图像颜色的分配
- imagecolorexact — 取得指定颜色的索引值
- imagecolorexactalpha — 取得指定的颜色加透明度的索引值
- imagecolormatch — 使一个图像中调色板版本的颜色与真彩色版本更能匹配
- imagecolorresolve — 取得指定颜色的索引值或有可能得到的最接近的替代值
- imagecolorresolvealpha — 取得指定颜色 + alpha 或其最接近的替代值
- imagecolorset — 给指定调色板索引设定颜色
- imagecolorsforindex — 获取索引的颜色
- imagecolorstotal — 取得图像调色板中的颜色数量
- imagecolortransparent — 将颜色定义为透明
- imageconvolution — 用系数 div 和 offset 申请一个 3x3 的卷积矩阵
- imagecopy — 拷贝图像的一部分
- imagecopymerge — 拷贝并合并图像的一部分
- imagecopymergegray — 用灰度复制并合并图像的一部分
- imagecopyresampled — 重采样拷贝部分图像并调整大小
- imagecopyresized — 拷贝部分图像并调整大小
- imagecreate — 创建新的基于调色板的图像
- imagecreatefromavif — 由文件或 URL 创建一个新图象。
- imagecreatefrombmp — 由文件或 URL 创建一个新图象。
- imagecreatefromgd — 从 GD 文件或 URL 新建一图像
- imagecreatefromgd2 — 从 GD2 文件或 URL 新建一图像
- imagecreatefromgd2part — 从指定的 GD2 文件或 URL 的部分创建新图像
- imagecreatefromgif — 由文件或 URL 创建一个新图象。
- imagecreatefromjpeg — 由文件或 URL 创建一个新图象。
- imagecreatefrompng — 由文件或 URL 创建一个新图象。
- imagecreatefromstring — 从字符串的图像流中新建图像
- imagecreatefromtga — 由文件或 URL 创建一个新图象。
- imagecreatefromwbmp — 由文件或 URL 创建一个新图象。
- imagecreatefromwebp — 由文件或 URL 创建一个新图象。
- imagecreatefromxbm — 由文件或 URL 创建一个新图象。
- imagecreatefromxpm — 由文件或 URL 创建一个新图象。
- imagecreatetruecolor — 新建真彩色图像
- imagecrop — Crop an image to the given rectangle
- imagecropauto — Crop an image automatically using one of the available modes
- imagedashedline — 绘制虚线
- imagedestroy — 销毁图像
- imageellipse — 画椭圆
- imagefill — 漫水填充
- imagefilledarc — 绘制部分弧形并填充
- imagefilledellipse — 绘制椭圆并填充
- imagefilledpolygon — 绘制多边形并填充
- imagefilledrectangle — 绘制矩形并填充
- imagefilltoborder — 漫水填充特定颜色
- imagefilter — 对图像使用过滤器
- imageflip — Flips an image using a given mode
- imagefontheight — 获取字体高度
- imagefontwidth — 获取字体宽度
- imageftbbox — 通过 freetype2 使用字体给出文本的边界框
- imagefttext — 使用 FreeType 2 字体将文本写入图像
- imagegammacorrect — 对 GD 图像应用伽玛校正
- imagegd — 将 GD 图像输出到浏览器或文件
- imagegd2 — 将 GD2 图像输出到浏览器或文件
- imagegetclip — Get the clipping rectangle
- imagegetinterpolation — Get the interpolation method
- imagegif — 输出图象到浏览器或文件。
- imagegrabscreen — Captures the whole screen
- imagegrabwindow — Captures a window
- imageinterlace — 启用或禁用隔行扫描
- imageistruecolor — 检查图像是否为真彩色图像
- imagejpeg — 输出图象到浏览器或文件。
- imagelayereffect — 设定 alpha 混合标志以使用分层效果
- imageline — 绘制直线
- imageloadfont — 载入新字体
- imageopenpolygon — Draws an open polygon
- imagepalettecopy — 将调色板从一个图像复制到另一个
- imagepalettetotruecolor — Converts a palette based image to true color
- imagepng — 将 PNG 图像输出到浏览器或文件
- imagepolygon — 绘制多边形
- imagerectangle — 绘制矩形
- imageresolution — Get or set the resolution of the image
- imagerotate — 用给定角度旋转图像
- imagesavealpha — 保存图像时是否保留完整的 alpha 通道信息
- imagescale — Scale an image using the given new width and height
- imagesetbrush — 为线条设置笔刷图像
- imagesetclip — Set the clipping rectangle
- imagesetinterpolation — Set the interpolation method
- imagesetpixel — 设置单个像素
- imagesetstyle — 设定线条的样式
- imagesetthickness — 设定画线的粗细
- imagesettile — 设置要填充的平铺图像
- imagestring — 水平绘制字符串
- imagestringup — 垂直绘制字符串
- imagesx — 取得图像宽度
- imagesy — 取得图像高度
- imagetruecolortopalette — 将真彩色图像转换为调色板图像
- imagettfbbox — 取得使用 TrueType 字体的文本的边界框
- imagettftext — 用 TrueType 字体向图像写入文本
- imagetypes — 返回 PHP 内置支持的图像类型
- imagewbmp — 输出图象到浏览器或文件。
- imagewebp — 将 WebP 格式的图像输出到浏览器或文件
- imagexbm — 输出 XBM 图像到浏览器或文件
- iptcembed — 嵌入二进制 IPTC 数据到 JPEG 图像中
- iptcparse — 将二进制 IPTC 块解析为单个标签
- jpeg2wbmp — 将 JPEG 图像文件转换为 WBMP 图像文件
- png2wbmp — 将 PNG 图像文件转换为 WBMP 图像文件
- GdImage — GdImage 类
- GdFont — The GdFont class
+添加备注
用户贡献的备注 5 notes
mail at ecross dot nl ¶
14 years ago
hello there,
i made a function to create a gradient image.
description:
gradient(int image_width, int image_height,
int start_red, int start_green, int start_blue,
int end_red, int end_green, int end_blue,
bool vertical)
function:
<?php
function gradient($image_width, $image_height,$c1_r, $c1_g, $c1_b, $c2_r, $c2_g, $c2_b, $vertical=false)
{
// first: lets type cast;
$image_width = (integer)$image_width;
$image_height = (integer)$image_height;
$c1_r = (integer)$c1_r;
$c1_g = (integer)$c1_g;
$c1_b = (integer)$c1_b;
$c2_r = (integer)$c2_r;
$c2_g = (integer)$c2_g;
$c2_b = (integer)$c2_b;
$vertical = (bool)$vertical;
// create a image
$image = imagecreatetruecolor($image_width, $image_height);
// make the gradient
for($i=0; $i<$image_height; $i++)
{
$color_r = floor($i * ($c2_r-$c1_r) / $image_height)+$c1_r;
$color_g = floor($i * ($c2_g-$c1_g) / $image_height)+$c1_g;
$color_b = floor($i * ($c2_b-$c1_b) / $image_height)+$c1_b;
$color = ImageColorAllocate($image, $color_r, $color_g, $color_b);
imageline($image, 0, $i, $image_width, $i, $color);
}
# Prints out all the figures and picture and frees memory
header('Content-type: image/png');
if($vertical){$image = imagerotate($image, 90, 0);}
ImagePNG($image);
imagedestroy($image);
}
?>
kurdtpage at gmail dot com ¶
13 years ago
When using GD, please make sure of the following things:
1. The file that is used to manipulate images is saved as ANSI format and not UTF-8
2. There is no space in front of the opening tag <?php
Thomas ¶
16 years ago
You know, maybe this goes without saying, but I thought I would drop a note in here. When developing code to resize images, it is best not to use GD. When using the current GD methodologies, you are reading content from an image and manipulating it. By then writing that content to a brand new file, you are losing the EXIF data.
For purposes when you want to retain EXIF data, it is recommended that you compile in and use the PECL Imagick extension. It has great resizing methods built right in and the EXIF data is retained.
herbert dot walde at googlemail dot com ¶
13 years ago
In case your script is using output-buffering-functions somewhere, then you have to clear the buffer first ( with ob_clear() ), before outputting an image with a function like imagepng().
And you should make sure that no buffer will get send after outputing an image by using the ob_end_flush()-function.
Furthermore you should check if a buffer has already been flushed somewhere before. This can be done using the headers_sent()-function.
Here is the full solution:
<?php
if(headers_sent()){
die('Headers have been send somewhere within my script');
}
ob_clean(); //Clears the buffer
header('Content-type: image/png');
imagepng($img, NULL, 0, NULL);
ob_end_flush(); //Now we send the header and image plus we make sure that nothing will get send from now on (including possible shutdown-functions and __destruct()-methods) till the end of page-execution
?>
code at ashleyhunt dot co dot uk ¶
16 years ago
I have been looking to send the output from GD to a text string without proxying via a file or to a browser.
I have come up with a solution.
This code buffers the output between the ob_start() and ob_end() functions into ob_get_contents()
See the example below
<?php
// Create a test source image for this example
$im = imagecreatetruecolor(300, 50);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// start buffering
ob_start();
// output jpeg (or any other chosen) format & quality
imagejpeg($im, NULL, 85);
// capture output to string
$contents = ob_get_contents();
// end capture
ob_end_clean();
// be tidy; free up memory
imagedestroy($im);
// lastly (for the example) we are writing the string to a file
$fh = fopen("./temp/img.jpg", "a+" );
fwrite( $fh, $contents );
fclose( $fh );
?>
Enjoy!
Ashley