<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc'; $TOC = array(); $TOC_DEPRECATED = array(); $PARENTS = array(); include_once dirname(__FILE__) ."/toc/ref.image.inc"; $setup = array ( 'home' => array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.imagecropauto.php', 1 => 'imagecropauto', 2 => 'Crop an image automatically using one of the available modes', ), 'up' => array ( 0 => 'ref.image.php', 1 => 'GD 和图像处理 函数', ), 'prev' => array ( 0 => 'function.imagecrop.php', 1 => 'imagecrop', ), 'next' => array ( 0 => 'function.imagedashedline.php', 1 => 'imagedashedline', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/image/functions/imagecropauto.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?> <div id="function.imagecropauto" class="refentry"> <div class="refnamediv"> <h1 class="refname">imagecropauto</h1> <p class="verinfo">(PHP 5 >= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">imagecropauto</span> — <span class="dc-title">Crop an image automatically using one of the available modes</span></p> </div> <div class="refsect1 description" id="refsect1-function.imagecropauto-description"> <h3 class="title">说明</h3> <div class="methodsynopsis dc-description"> <span class="methodname"><strong>imagecropauto</strong></span>(<br> <span class="methodparam"><span class="type"><a href="class.gdimage.php" class="type GdImage">GdImage</a></span> <code class="parameter">$image</code></span>,<br> <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$mode</code><span class="initializer"> = <strong><code><a href="image.constants.php#constant.img-crop-default">IMG_CROP_DEFAULT</a></code></strong></span></span>,<br> <span class="methodparam"><span class="type"><a href="language.types.float.php" class="type float">float</a></span> <code class="parameter">$threshold</code><span class="initializer"> = 0.5</span></span>,<br> <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$color</code><span class="initializer"> = -1</span></span><br>): <span class="type"><span class="type"><a href="class.gdimage.php" class="type GdImage">GdImage</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div> <p class="para rdfs-comment"> Automatically crops an image according to the given <code class="parameter">mode</code>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.imagecropauto-parameters"> <h3 class="title">参数</h3> <dl> <dt> <code class="parameter">image</code></dt><dd><p class="para">由图象创建函数(例如<span class="function"><a href="function.imagecreatetruecolor.php" class="function">imagecreatetruecolor()</a></span>)返回的 <span class="classname"><a href="class.gdimage.php" class="classname">GdImage</a></span> 对象。</p></dd> <dt><code class="parameter">mode</code></dt> <dd> <p class="para"> One of the following constants: </p> <dl> <dt><strong><code><a href="image.constants.php#constant.img-crop-default">IMG_CROP_DEFAULT</a></code></strong></dt> <dd> <span class="simpara"> Same as <strong><code><a href="image.constants.php#constant.img-crop-transparent">IMG_CROP_TRANSPARENT</a></code></strong>. Before PHP 7.4.0, the bundled libgd fell back to <strong><code><a href="image.constants.php#constant.img-crop-sides">IMG_CROP_SIDES</a></code></strong>, if the image had no transparent color. </span> </dd> <dt><strong><code><a href="image.constants.php#constant.img-crop-transparent">IMG_CROP_TRANSPARENT</a></code></strong></dt> <dd> <span class="simpara"> Crops out a transparent background. </span> </dd> <dt><strong><code><a href="image.constants.php#constant.img-crop-black">IMG_CROP_BLACK</a></code></strong></dt> <dd> <span class="simpara"> Crops out a black background. </span> </dd> <dt><strong><code><a href="image.constants.php#constant.img-crop-white">IMG_CROP_WHITE</a></code></strong></dt> <dd> <span class="simpara"> Crops out a white background. </span> </dd> <dt><strong><code><a href="image.constants.php#constant.img-crop-sides">IMG_CROP_SIDES</a></code></strong></dt> <dd> <span class="simpara"> Uses the 4 corners of the image to attempt to detect the background to crop. </span> </dd> <dt><strong><code><a href="image.constants.php#constant.img-crop-threshold">IMG_CROP_THRESHOLD</a></code></strong></dt> <dd> <span class="simpara"> Crops an image using the given <code class="parameter">threshold</code> and <code class="parameter">color</code>. </span> </dd> </dl> </dd> <dt><code class="parameter">threshold</code></dt> <dd> <p class="para"> Specifies the tolerance in percent to be used while comparing the image color and the color to crop. The method used to calculate the color difference is based on the color distance in the RGB(a) cube. </p> <p class="para"> Used only in <strong><code><a href="image.constants.php#constant.img-crop-threshold">IMG_CROP_THRESHOLD</a></code></strong> mode. </p> <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara"> Before PHP 7.4.0, the bundled libgd used a somewhat different algorithm, so the same <code class="parameter">threshold</code> yielded different results for system and bundled libgd. </span> </p></blockquote> </dd> <dt><code class="parameter">color</code></dt> <dd> <p class="para"> Either an RGB color value or a palette index. </p> <p class="para"> Used only in <strong><code><a href="image.constants.php#constant.img-crop-threshold">IMG_CROP_THRESHOLD</a></code></strong> mode. </p> </dd> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-function.imagecropauto-returnvalues"> <h3 class="title">返回值</h3> <p class="para"> Returns a cropped image object on success 或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>. <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> is also returned if the whole image was cropped. </p> </div> <div class="refsect1 changelog" id="refsect1-function.imagecropauto-changelog"> <h3 class="title">更新日志</h3> <table class="doctable informaltable"> <thead> <tr> <th>版本</th> <th>说明</th> </tr> </thead> <tbody class="tbody"> <tr> <td>8.0.0</td> <td> <code class="parameter">image</code> 现在需要 <span class="classname"><a href="class.gdimage.php" class="classname">GdImage</a></span> 实例;之前需要有效的 <code class="literal">gd</code> <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>。 </td> </tr> <tr> <td>8.0.0</td> <td> On success, this function returns a <span class="classname"><a href="class.gdimage.php" class="classname">GDImage</a></span> instance now; previously, a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> was returned. </td> </tr> <tr> <td>7.4.0</td> <td> The behavior of imagecropauto() in the bundled libgd has been synced with that of system libgd: <strong><code><a href="image.constants.php#constant.img-crop-default">IMG_CROP_DEFAULT</a></code></strong> no longer falls back to <strong><code><a href="image.constants.php#constant.img-crop-sides">IMG_CROP_SIDES</a></code></strong> and threshold-cropping now uses the same algorithm as system libgd. </td> </tr> <tr> <td>7.4.0</td> <td> The default value of <code class="parameter">mode</code> has been changed to <strong><code>IMG_CROP_AUTO</code></strong>. Formerly, the default value has been <code class="literal">-1</code> which corresponds to <strong><code><a href="image.constants.php#constant.img-crop-default">IMG_CROP_DEFAULT</a></code></strong>, but passing <code class="literal">-1</code> is now deprecated. </td> </tr> </tbody> </table> </div> <div class="refsect1 examples" id="refsect1-function.imagecropauto-examples"> <h3 class="title">示例</h3> <p class="para"> <div class="example" id="example-2853"> <p><strong>示例 #1 Proper handling of auto-cropping</strong></p> <div class="example-contents"><p> As noted in the return value section, <span class="function"><strong>imagecropauto()</strong></span> returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> if the whole image was cropped. In this example we have an image object <code class="literal">$im</code> which should be automatically cropped only if there is something to crop; otherwise we want to proceed with the original image. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$cropped </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecropauto</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">, </span><span style="color: #0000BB">IMG_CROP_DEFAULT</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$cropped </span><span style="color: #007700">!== </span><span style="color: #0000BB">false</span><span style="color: #007700">) { </span><span style="color: #FF8000">// in case a new image object was returned<br /> </span><span style="color: #0000BB">$im </span><span style="color: #007700">= </span><span style="color: #0000BB">$cropped</span><span style="color: #007700">; </span><span style="color: #FF8000">// assign the cropped image to $im<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.imagecropauto-seealso"> <h3 class="title">参见</h3> <ul class="simplelist"> <li><span class="function"><a href="function.imagecrop.php" class="function" rel="rdfs-seeAlso">imagecrop()</a> - Crop an image to the given rectangle</span></li> </ul> </div> </div><?php manual_footer($setup); ?>