<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Copy and resize part of an image with resampling</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.imagecopymergegray.html">imagecopymergegray</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.imagecopyresized.html">imagecopyresized</a></div> <div class="up"><a href="ref.image.html">GD and Image Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.imagecopyresampled" class="refentry"> <div class="refnamediv"> <h1 class="refname">imagecopyresampled</h1> <p class="verinfo">(PHP 4 >= 4.0.6, PHP 5)</p><p class="refpurpose"><span class="refname">imagecopyresampled</span> — <span class="dc-title">Copy and resize part of an image with resampling</span></p> </div> <div class="refsect1 description" id="refsect1-function.imagecopyresampled-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>imagecopyresampled</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$dst_image</code></span> , <span class="methodparam"><span class="type">resource</span> <code class="parameter">$src_image</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$dst_x</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$dst_y</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$src_x</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$src_y</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$dst_w</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$dst_h</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$src_w</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$src_h</code></span> )</div> <p class="para rdfs-comment"> <span class="function"><strong>imagecopyresampled()</strong></span> copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity. </p> <p class="para"> In other words, <span class="function"><strong>imagecopyresampled()</strong></span> will take a rectangular area from <em><code class="parameter">src_image</code></em> of width <em><code class="parameter">src_w</code></em> and height <em><code class="parameter">src_h</code></em> at position (<em><code class="parameter">src_x</code></em>,<em><code class="parameter">src_y</code></em>) and place it in a rectangular area of <em><code class="parameter">dst_image</code></em> of width <em><code class="parameter">dst_w</code></em> and height <em><code class="parameter">dst_h</code></em> at position (<em><code class="parameter">dst_x</code></em>,<em><code class="parameter">dst_y</code></em>). </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 <em><code class="parameter">dst_image</code></em> is the same as <em><code class="parameter">src_image</code></em>) but if the regions overlap the results will be unpredictable. </p> </div> <div class="refsect1 parameters" id="refsect1-function.imagecopyresampled-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">dst_image</code></em></span> <dd> <p class="para">Destination image link resource.</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">src_image</code></em></span> <dd> <p class="para">Source image link resource.</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">dst_x</code></em></span> <dd> <p class="para"> x-coordinate of destination point. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">dst_y</code></em></span> <dd> <p class="para"> y-coordinate of destination point. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">src_x</code></em></span> <dd> <p class="para"> x-coordinate of source point. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">src_y</code></em></span> <dd> <p class="para"> y-coordinate of source point. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">dst_w</code></em></span> <dd> <p class="para"> Destination width. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">dst_h</code></em></span> <dd> <p class="para"> Destination height. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">src_w</code></em></span> <dd> <p class="para">Source width.</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">src_h</code></em></span> <dd> <p class="para">Source height.</p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.imagecopyresampled-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-function.imagecopyresampled-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3205"> <p><strong>Example #1 Simple example</strong></p> <div class="example-contents"><p> This example will resample an image to half its original 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">// The file<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 dimensions<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">$new_width </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">$new_height </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">// Resample<br /></span><span style="color: #0000BB">$image_p </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecreatetruecolor</span><span style="color: #007700">(</span><span style="color: #0000BB">$new_width</span><span style="color: #007700">, </span><span style="color: #0000BB">$new_height</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$image </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 /></span><span style="color: #0000BB">imagecopyresampled</span><span style="color: #007700">(</span><span style="color: #0000BB">$image_p</span><span style="color: #007700">, </span><span style="color: #0000BB">$image</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">$new_width</span><span style="color: #007700">, </span><span style="color: #0000BB">$new_height</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">$image_p</span><span style="color: #007700">, </span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">100</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="images/21009b70229598c6a80eef8b45bf282b-imagecopyresampled.jpg" alt="Output of example : Simple example" width="47" height="25" /> </div> </div> </div> </p> <p class="para"> <div class="example" id="example-3206"> <p><strong>Example #2 Resampling an image proportionally</strong></p> <div class="example-contents"><p> This example will display an image with the maximum width, or height, of 200 pixels. </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">// The file<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 /><br /></span><span style="color: #FF8000">// Set a maximum height and width<br /></span><span style="color: #0000BB">$width </span><span style="color: #007700">= </span><span style="color: #0000BB">200</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$height </span><span style="color: #007700">= </span><span style="color: #0000BB">200</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 dimensions<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$width_orig</span><span style="color: #007700">, </span><span style="color: #0000BB">$height_orig</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 /><br /></span><span style="color: #0000BB">$ratio_orig </span><span style="color: #007700">= </span><span style="color: #0000BB">$width_orig</span><span style="color: #007700">/</span><span style="color: #0000BB">$height_orig</span><span style="color: #007700">;<br /><br />if (</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">$ratio_orig</span><span style="color: #007700">) {<br /> </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">$ratio_orig</span><span style="color: #007700">;<br />} else {<br /> </span><span style="color: #0000BB">$height </span><span style="color: #007700">= </span><span style="color: #0000BB">$width</span><span style="color: #007700">/</span><span style="color: #0000BB">$ratio_orig</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Resample<br /></span><span style="color: #0000BB">$image_p </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecreatetruecolor</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 /></span><span style="color: #0000BB">$image </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 /></span><span style="color: #0000BB">imagecopyresampled</span><span style="color: #007700">(</span><span style="color: #0000BB">$image_p</span><span style="color: #007700">, </span><span style="color: #0000BB">$image</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">$width</span><span style="color: #007700">, </span><span style="color: #0000BB">$height</span><span style="color: #007700">, </span><span style="color: #0000BB">$width_orig</span><span style="color: #007700">, </span><span style="color: #0000BB">$height_orig</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">$image_p</span><span style="color: #007700">, </span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">100</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="images/21009b70229598c6a80eef8b45bf282b-imagecopyresampled_2.jpg" alt="Output of example : Resampling an image proportionally" width="200" height="107" /> </div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.imagecopyresampled-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> 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 <span class="function"><a href="function.imagecreatetruecolor.html" class="function">imagecreatetruecolor()</a></span>. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.imagecopyresampled-seealso"> <h3 class="title">See Also</h3> <p class="para"> <span class="function"><a href="function.imagecopyresized.html" class="function" rel="rdfs-seeAlso">imagecopyresized()</a> - Copy and resize part of an image</span> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.imagecopymergegray.html">imagecopymergegray</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.imagecopyresized.html">imagecopyresized</a></div> <div class="up"><a href="ref.image.html">GD and Image Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>