function.imagecopyresized.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 291 行
HTML
291 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Copy and resize part of an image</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.imagecopyresampled.html">imagecopyresampled</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.imagecreate.html">imagecreate</a></div> <div class="up"><a href="ref.image.html">GD Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.imagecopyresized" class="refentry"> <div class="refnamediv"> <h1 class="refname">imagecopyresized</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">imagecopyresized</span> — <span class="dc-title">Copy and resize part of an image</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>imagecopyresized</b></b></span> ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$dst_image</tt></span> , <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$src_image</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$dst_x</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$dst_y</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$src_x</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$src_y</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$dst_w</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$dst_h</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$src_w</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$src_h</tt></span> )</div> <p class="para rdfs-comment"> <b>imagecopyresized()</b> copies a rectangular portion of one image to another image. <i><tt class="parameter">dst_image</tt></i> is the destination image, <i><tt class="parameter">src_image</tt></i> is the source image identifier. </p> <p class="para"> In other words, <b>imagecopyresized()</b> will take an rectangular area from <i><tt class="parameter">src_image</tt></i> of width <i><tt class="parameter">src_w</tt></i> and height <i><tt class="parameter">src_h</tt></i> at position (<i><tt class="parameter">src_x</tt></i>,<i><tt class="parameter">src_y</tt></i>) and place it in a rectangular area of <i><tt class="parameter">dst_image</tt></i> of width <i><tt class="parameter">dst_w</tt></i> and height <i><tt class="parameter">dst_h</tt></i> at position (<i><tt class="parameter">dst_x</tt></i>,<i><tt class="parameter">dst_y</tt></i>). </p> <p class="para"> If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed. The coordinates refer to the upper left corner. This function can be used to copy regions within the same image (if <i><tt class="parameter">dst_image</tt></i> is the same as <i><tt class="parameter">src_image</tt></i>) but if the regions overlap the results will be unpredictable. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">dst_im</tt></i></span> <dd> <p class="para"> Destination image link resource. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">src_im</tt></i></span> <dd> <p class="para"> Source image link resource. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">dst_x</tt></i></span> <dd> <p class="para"> x-coordinate of destination point. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">dst_y</tt></i></span> <dd> <p class="para"> y-coordinate of destination point. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">src_x</tt></i></span> <dd> <p class="para"> x-coordinate of source point. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">src_y</tt></i></span> <dd> <p class="para"> y-coordinate of source point. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">dst_w</tt></i></span> <dd> <p class="para"> Destination width. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">dst_h</tt></i></span> <dd> <p class="para"> Destination height. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">src_w</tt></i></span> <dd> <p class="para"> Source width. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">src_h</tt></i></span> <dd> <p class="para"> Source height. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 Resizing an image</b></p> <div class="example-contents"><p> This example will display the image at half size. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// File and new size<br /></span><span style="color: #0000BB">$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">'test.jpg'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$percent </span><span style="color: #007700">= </span><span style="color: #0000BB">0.5</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Content type<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-type: image/jpeg'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Get new sizes<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$width</span><span style="color: #007700">, </span><span style="color: #0000BB">$height</span><span style="color: #007700">) = </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$newwidth </span><span style="color: #007700">= </span><span style="color: #0000BB">$width </span><span style="color: #007700">* </span><span style="color: #0000BB">$percent</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$newheight </span><span style="color: #007700">= </span><span style="color: #0000BB">$height </span><span style="color: #007700">* </span><span style="color: #0000BB">$percent</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Load<br /></span><span style="color: #0000BB">$thumb </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecreatetruecolor</span><span style="color: #007700">(</span><span style="color: #0000BB">$newwidth</span><span style="color: #007700">, </span><span style="color: #0000BB">$newheight</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$source </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecreatefromjpeg</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Resize<br /></span><span style="color: #0000BB">imagecopyresized</span><span style="color: #007700">(</span><span style="color: #0000BB">$thumb</span><span style="color: #007700">, </span><span style="color: #0000BB">$source</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$newwidth</span><span style="color: #007700">, </span><span style="color: #0000BB">$newheight</span><span style="color: #007700">, </span><span style="color: #0000BB">$width</span><span style="color: #007700">, </span><span style="color: #0000BB">$height</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Output<br /></span><span style="color: #0000BB">imagejpeg</span><span style="color: #007700">(</span><span style="color: #0000BB">$thumb</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="mediaobject"> <div class="imageobject"> <img src="figures/image.imagecopyresized.jpg" /> </div> </div> <div class="example-contents"><p> The image will be output at half size, though better quality could be obtained using <a href="function.imagecopyresampled.html" class="function">imagecopyresampled()</a>. </p></div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: There is a problem due to palette image limitations (255+1 colors). Resampling or filtering an image commonly needs more colors than 255, a kind of approximation is used to calculate the new resampled pixel and its color. With a palette image we try to allocate a new color, if that failed, we choose the closest (in theory) computed color. This is not always the closest visual color. That may produce a weird result, like blank (or visually blank) images. To skip this problem, please use a truecolor image as a destination image, such as one created by <a href="function.imagecreatetruecolor.html" class="function">imagecreatetruecolor()</a>. <br /> </p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <a href="function.imagecopyresampled.html" class="function" rel="rdfs-seeAlso">imagecopyresampled()</a> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.imagecopyresampled.html">imagecopyresampled</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.imagecreate.html">imagecreate</a></div> <div class="up"><a href="ref.image.html">GD Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?