<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/refs.utilspec.image.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'book.image.php',
    1 => 'GD',
    2 => '图像处理和 GD',
  ),
  'up' => 
  array (
    0 => 'refs.utilspec.image.php',
    1 => '图像生成和处理',
  ),
  'prev' => 
  array (
    0 => 'function.read-exif-data.php',
    1 => 'read_exif_data',
  ),
  'next' => 
  array (
    0 => 'image.setup.php',
    1 => '安装/配置',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/image/book.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/book.image.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="book.image" class="book">
 
 <h1 class="title">图像处理和 GD</h1>
 
 
 
 <div id="intro.image" class="preface">
  <h1 class="title">简介</h1>
  <p class="para">
   PHP 并不仅限于创建 HTML 输出，
   它也可以创建和处理包括 <abbr title="Graphic Interchange Format">GIF</abbr>， <abbr title="Portable Network Graphics">PNG</abbr>，
   <abbr title="Joint Photographic Experts Group">JPEG</abbr>， <abbr title="Wireless Bitmap">WBMP</abbr> 以及 <abbr>XPM</abbr>
   在内的多种格式的图像。
   更加方便的是，PHP 可以直接将图像数据流输出到浏览器。
   要想在 PHP 中使用图像处理功能，你需要连带 <abbr title="Graphics Draw">GD</abbr> 库一起来编译 PHP。
   <abbr title="Graphics Draw">GD</abbr> 库和 PHP 可能需要其他的库，
   这取决于你要处理的图像格式。
  </p>
  <p class="para">
   你可以使用 PHP 中的图像函数来获取下列格式图像的大小：
   <abbr title="Joint Photographic Experts Group">JPEG</abbr>， <abbr title="Graphic Interchange Format">GIF</abbr>，
   <abbr title="Portable Network Graphics">PNG</abbr>， <abbr title="Shockwave Flash">SWF</abbr>，
   <abbr title="Tagged Image File Format">TIFF</abbr> 和 <abbr>JPEG2000</abbr>。
  </p>
  <p class="para">
   如果联合 <a href="ref.exif.php" class="link">exif 扩展</a> 一起使用，
   你可以操作存储在 <abbr title="Joint Photographic Experts Group">JPEG</abbr> 和 <abbr title="Tagged Image File Format">TIFF</abbr> 
   图像文件头部的信息，
   这样就就可以获取数码相机所产生的元数据。
   exif 相关的函数不需要 <abbr title="Graphics Draw">GD</abbr> 库亦可使用。
  </p>
  
  <p class="para">
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <span class="simpara">
     关于如何扩展图像处理能力，例如读取、写入以及修改，
     请参考“需求”一节。
     要想读取数码相机拍摄的图片的元数据，
     你需要上面提到的 <a href="ref.exif.php" class="link">exif 扩展</a>。
    </span>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <span class="simpara">
      <span class="function"><a href="function.getimagesize.php" class="function">getimagesize()</a></span> 函数不需要 GD 扩展库。
    </span>
   </p></blockquote>
   <div class="caution"><strong class="caution">警告</strong>
    <p class="simpara">
     由于绑定的 GD 库使用 Zend 内存管理机制来分配内存，
     所以所使用的内存大小不受  <a href="ini.core.php#ini.memory-limit" class="link">memory_limit</a>
     配置参数限制。
    </p>
   </div>
  </p>

  <p class="para">
   GD 库支持多种图像格式，
   下标所列的是 GD 所支持的格式，请注意备图像格式对应的读取/写入支持的可用性。
  </p>
  <p class="para">
   <table class="doctable table">
    <caption><strong>GD 所支持的格式</strong></caption>
    
     <thead>
      <tr>
       <th>格式</th>
       <th>支持读取</th>
       <th>支持写入</th>
       <th>备注</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>JPEG</td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td>PNG</td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td>GIF</td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td>XBM</td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td>XPM</td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td><strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td>WBMP</td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td>WebP</td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td>BMP</td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td>从 PHP 7.2.0 开始支持</td>
      </tr>

     </tbody>
    
   </table>

  </p>
  <p class="para">
   很遗憾的是，虽然上表中显示大部分图像格式都是支持读取和写入的，
   但是不代表你的 PHP 环境在编译的时候是支持这些操作的。
   要想检测 GD 库所支持的格式，请使用 <span class="function"><a href="function.gd-info.php" class="function">gd_info()</a></span> 函数，
   更多信息请参考 “安装” 一章。
  </p>
 </div>

 
 
 








 






 








 








 




 

 




<ul class="chunklist chunklist_book"><li><a href="image.setup.php">安装/配置</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="image.requirements.php">需求</a></li><li><a href="image.installation.php">安装</a></li><li><a href="image.configuration.php">运行时配置</a></li><li><a href="image.resources.php">资源类型</a></li></ul></li><li><a href="image.constants.php">预定义常量</a></li><li><a href="image.examples.php">示例</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="image.examples-png.php">使用 PHP 创建 PNG 图像</a></li><li><a href="image.examples-watermark.php">使用 Alpha 通道为图像加水印</a></li><li><a href="image.examples.merged-watermark.php">使用 imagecopymerge 函数创建半透明水印</a></li></ul></li><li><a href="ref.image.php">GD 和图像处理 函数</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="function.gd-info.php">gd_info</a> — 取得当前安装的 GD 库的信息</li><li><a href="function.getimagesize.php">getimagesize</a> — 取得图像大小</li><li><a href="function.getimagesizefromstring.php">getimagesizefromstring</a> — 从字符串中获取图像尺寸信息</li><li><a href="function.image-type-to-extension.php">image_type_to_extension</a> — 取得图像类型的文件后缀</li><li><a href="function.image-type-to-mime-type.php">image_type_to_mime_type</a> — 取得 getimagesize、exif_read_data、exif_thumbnail、exif_imagetype 所返回的图像类型的 MIME 类型</li><li><a href="function.image2wbmp.php">image2wbmp</a> — 输出图象到浏览器或文件。</li><li><a href="function.imageaffine.php">imageaffine</a> — 返回经过仿射变换后的图像，剪切区域可选</li><li><a href="function.imageaffinematrixconcat.php">imageaffinematrixconcat</a> — Concatenate two affine transformation matrices</li><li><a href="function.imageaffinematrixget.php">imageaffinematrixget</a> — Get an affine transformation matrix</li><li><a href="function.imagealphablending.php">imagealphablending</a> — 设定图像的混色模式</li><li><a href="function.imageantialias.php">imageantialias</a> — 是否使用抗锯齿（antialias）功能</li><li><a href="function.imagearc.php">imagearc</a> — 画弧线</li><li><a href="function.imageavif.php">imageavif</a> — 输出图象到浏览器或文件。</li><li><a href="function.imagebmp.php">imagebmp</a> — Output a BMP image to browser or file</li><li><a href="function.imagechar.php">imagechar</a> — 水平地绘制一个字符</li><li><a href="function.imagecharup.php">imagecharup</a> — 垂直地绘制一个字符</li><li><a href="function.imagecolorallocate.php">imagecolorallocate</a> — 为图像分配颜色</li><li><a href="function.imagecolorallocatealpha.php">imagecolorallocatealpha</a> — 为图像分配颜色</li><li><a href="function.imagecolorat.php">imagecolorat</a> — 取得某像素的颜色索引值</li><li><a href="function.imagecolorclosest.php">imagecolorclosest</a> — 取得与指定的颜色最接近的颜色索引值</li><li><a href="function.imagecolorclosestalpha.php">imagecolorclosestalpha</a> — 获取最接近指定颜色 + alpha 的颜色索引</li><li><a href="function.imagecolorclosesthwb.php">imagecolorclosesthwb</a> — 取得与给定颜色最接近的色度的黑白色的索引</li><li><a href="function.imagecolordeallocate.php">imagecolordeallocate</a> — 取消图像颜色的分配</li><li><a href="function.imagecolorexact.php">imagecolorexact</a> — 取得指定颜色的索引值</li><li><a href="function.imagecolorexactalpha.php">imagecolorexactalpha</a> — 取得指定的颜色加透明度的索引值</li><li><a href="function.imagecolormatch.php">imagecolormatch</a> — 使一个图像中调色板版本的颜色与真彩色版本更能匹配</li><li><a href="function.imagecolorresolve.php">imagecolorresolve</a> — 取得指定颜色的索引值或有可能得到的最接近的替代值</li><li><a href="function.imagecolorresolvealpha.php">imagecolorresolvealpha</a> — 取得指定颜色 + alpha 或其最接近的替代值</li><li><a href="function.imagecolorset.php">imagecolorset</a> — 给指定调色板索引设定颜色</li><li><a href="function.imagecolorsforindex.php">imagecolorsforindex</a> — 获取索引的颜色</li><li><a href="function.imagecolorstotal.php">imagecolorstotal</a> — 取得图像调色板中的颜色数量</li><li><a href="function.imagecolortransparent.php">imagecolortransparent</a> — 将颜色定义为透明</li><li><a href="function.imageconvolution.php">imageconvolution</a> — 用系数 div 和 offset 申请一个 3x3 的卷积矩阵</li><li><a href="function.imagecopy.php">imagecopy</a> — 拷贝图像的一部分</li><li><a href="function.imagecopymerge.php">imagecopymerge</a> — 拷贝并合并图像的一部分</li><li><a href="function.imagecopymergegray.php">imagecopymergegray</a> — 用灰度复制并合并图像的一部分</li><li><a href="function.imagecopyresampled.php">imagecopyresampled</a> — 重采样拷贝部分图像并调整大小</li><li><a href="function.imagecopyresized.php">imagecopyresized</a> — 拷贝部分图像并调整大小</li><li><a href="function.imagecreate.php">imagecreate</a> — 创建新的基于调色板的图像</li><li><a href="function.imagecreatefromavif.php">imagecreatefromavif</a> — 由文件或 URL 创建一个新图象。</li><li><a href="function.imagecreatefrombmp.php">imagecreatefrombmp</a> — 由文件或 URL 创建一个新图象。</li><li><a href="function.imagecreatefromgd.php">imagecreatefromgd</a> — 从 GD 文件或 URL 新建一图像</li><li><a href="function.imagecreatefromgd2.php">imagecreatefromgd2</a> — 从 GD2 文件或 URL 新建一图像</li><li><a href="function.imagecreatefromgd2part.php">imagecreatefromgd2part</a> — 从指定的 GD2 文件或 URL 的部分创建新图像</li><li><a href="function.imagecreatefromgif.php">imagecreatefromgif</a> — 由文件或 URL 创建一个新图象。</li><li><a href="function.imagecreatefromjpeg.php">imagecreatefromjpeg</a> — 由文件或 URL 创建一个新图象。</li><li><a href="function.imagecreatefrompng.php">imagecreatefrompng</a> — 由文件或 URL 创建一个新图象。</li><li><a href="function.imagecreatefromstring.php">imagecreatefromstring</a> — 从字符串的图像流中新建图像</li><li><a href="function.imagecreatefromtga.php">imagecreatefromtga</a> — 由文件或 URL 创建一个新图象。</li><li><a href="function.imagecreatefromwbmp.php">imagecreatefromwbmp</a> — 由文件或 URL 创建一个新图象。</li><li><a href="function.imagecreatefromwebp.php">imagecreatefromwebp</a> — 由文件或 URL 创建一个新图象。</li><li><a href="function.imagecreatefromxbm.php">imagecreatefromxbm</a> — 由文件或 URL 创建一个新图象。</li><li><a href="function.imagecreatefromxpm.php">imagecreatefromxpm</a> — 由文件或 URL 创建一个新图象。</li><li><a href="function.imagecreatetruecolor.php">imagecreatetruecolor</a> — 新建真彩色图像</li><li><a href="function.imagecrop.php">imagecrop</a> — Crop an image to the given rectangle</li><li><a href="function.imagecropauto.php">imagecropauto</a> — Crop an image automatically using one of the available modes</li><li><a href="function.imagedashedline.php">imagedashedline</a> — 绘制虚线</li><li><a href="function.imagedestroy.php">imagedestroy</a> — 销毁图像</li><li><a href="function.imageellipse.php">imageellipse</a> — 画椭圆</li><li><a href="function.imagefill.php">imagefill</a> — 漫水填充</li><li><a href="function.imagefilledarc.php">imagefilledarc</a> — 绘制部分弧形并填充</li><li><a href="function.imagefilledellipse.php">imagefilledellipse</a> — 绘制椭圆并填充</li><li><a href="function.imagefilledpolygon.php">imagefilledpolygon</a> — 绘制多边形并填充</li><li><a href="function.imagefilledrectangle.php">imagefilledrectangle</a> — 绘制矩形并填充</li><li><a href="function.imagefilltoborder.php">imagefilltoborder</a> — 漫水填充特定颜色</li><li><a href="function.imagefilter.php">imagefilter</a> — 对图像使用过滤器</li><li><a href="function.imageflip.php">imageflip</a> — Flips an image using a given mode</li><li><a href="function.imagefontheight.php">imagefontheight</a> — 获取字体高度</li><li><a href="function.imagefontwidth.php">imagefontwidth</a> — 获取字体宽度</li><li><a href="function.imageftbbox.php">imageftbbox</a> — 通过 freetype2 使用字体给出文本的边界框</li><li><a href="function.imagefttext.php">imagefttext</a> — 使用 FreeType 2 字体将文本写入图像</li><li><a href="function.imagegammacorrect.php">imagegammacorrect</a> — 对 GD 图像应用伽玛校正</li><li><a href="function.imagegd.php">imagegd</a> — 将 GD 图像输出到浏览器或文件</li><li><a href="function.imagegd2.php">imagegd2</a> — 将 GD2 图像输出到浏览器或文件</li><li><a href="function.imagegetclip.php">imagegetclip</a> — Get the clipping rectangle</li><li><a href="function.imagegetinterpolation.php">imagegetinterpolation</a> — Get the interpolation method</li><li><a href="function.imagegif.php">imagegif</a> — 输出图象到浏览器或文件。</li><li><a href="function.imagegrabscreen.php">imagegrabscreen</a> — Captures the whole screen</li><li><a href="function.imagegrabwindow.php">imagegrabwindow</a> — Captures a window</li><li><a href="function.imageinterlace.php">imageinterlace</a> — 启用或禁用隔行扫描</li><li><a href="function.imageistruecolor.php">imageistruecolor</a> — 检查图像是否为真彩色图像</li><li><a href="function.imagejpeg.php">imagejpeg</a> — 输出图象到浏览器或文件。</li><li><a href="function.imagelayereffect.php">imagelayereffect</a> — 设定 alpha 混合标志以使用分层效果</li><li><a href="function.imageline.php">imageline</a> — 绘制直线</li><li><a href="function.imageloadfont.php">imageloadfont</a> — 载入新字体</li><li><a href="function.imageopenpolygon.php">imageopenpolygon</a> — Draws an open polygon</li><li><a href="function.imagepalettecopy.php">imagepalettecopy</a> — 将调色板从一个图像复制到另一个</li><li><a href="function.imagepalettetotruecolor.php">imagepalettetotruecolor</a> — Converts a palette based image to true color</li><li><a href="function.imagepng.php">imagepng</a> — 将 PNG 图像输出到浏览器或文件</li><li><a href="function.imagepolygon.php">imagepolygon</a> — 绘制多边形</li><li><a href="function.imagerectangle.php">imagerectangle</a> — 绘制矩形</li><li><a href="function.imageresolution.php">imageresolution</a> — Get or set the resolution of the image</li><li><a href="function.imagerotate.php">imagerotate</a> — 用给定角度旋转图像</li><li><a href="function.imagesavealpha.php">imagesavealpha</a> — 保存图像时是否保留完整的 alpha 通道信息</li><li><a href="function.imagescale.php">imagescale</a> — Scale an image using the given new width and height</li><li><a href="function.imagesetbrush.php">imagesetbrush</a> — 为线条设置笔刷图像</li><li><a href="function.imagesetclip.php">imagesetclip</a> — Set the clipping rectangle</li><li><a href="function.imagesetinterpolation.php">imagesetinterpolation</a> — Set the interpolation method</li><li><a href="function.imagesetpixel.php">imagesetpixel</a> — 设置单个像素</li><li><a href="function.imagesetstyle.php">imagesetstyle</a> — 设定线条的样式</li><li><a href="function.imagesetthickness.php">imagesetthickness</a> — 设定画线的粗细</li><li><a href="function.imagesettile.php">imagesettile</a> — 设置要填充的平铺图像</li><li><a href="function.imagestring.php">imagestring</a> — 水平绘制字符串</li><li><a href="function.imagestringup.php">imagestringup</a> — 垂直绘制字符串</li><li><a href="function.imagesx.php">imagesx</a> — 取得图像宽度</li><li><a href="function.imagesy.php">imagesy</a> — 取得图像高度</li><li><a href="function.imagetruecolortopalette.php">imagetruecolortopalette</a> — 将真彩色图像转换为调色板图像</li><li><a href="function.imagettfbbox.php">imagettfbbox</a> — 取得使用 TrueType 字体的文本的边界框</li><li><a href="function.imagettftext.php">imagettftext</a> — 用 TrueType 字体向图像写入文本</li><li><a href="function.imagetypes.php">imagetypes</a> — 返回 PHP 内置支持的图像类型</li><li><a href="function.imagewbmp.php">imagewbmp</a> — 输出图象到浏览器或文件。</li><li><a href="function.imagewebp.php">imagewebp</a> — 将 WebP 格式的图像输出到浏览器或文件</li><li><a href="function.imagexbm.php">imagexbm</a> — 输出 XBM 图像到浏览器或文件</li><li><a href="function.iptcembed.php">iptcembed</a> — 嵌入二进制 IPTC 数据到 JPEG 图像中</li><li><a href="function.iptcparse.php">iptcparse</a> — 将二进制 IPTC 块解析为单个标签</li><li><a href="function.jpeg2wbmp.php">jpeg2wbmp</a> — 将 JPEG 图像文件转换为 WBMP 图像文件</li><li><a href="function.png2wbmp.php">png2wbmp</a> — 将 PNG 图像文件转换为 WBMP 图像文件</li></ul></li><li><a href="class.gdimage.php">GdImage</a> — GdImage 类</li><li><a href="class.gdfont.php">GdFont</a> — The GdFont class</li></ul></div><?php manual_footer($setup); ?>