18.8 Copy One Part of an Image to Another


You want to copy part of one image to another image.

Technique

Use the ImageCopy() function to copy part of one image to another image:

 <?php header("Content-type: image/jpeg"); $im1 = ImageCreateFromJpeg('tst1.jpg'); $im2 = ImageCreateFromJpeg('tst2.jpg'); ImageCopy($im1, $im2, 50, 100, 50, 100, 35, 35); ImageJpeg($im2); ImageDestroy($im2); ImageDestroy($im1); ?> 

Comments

The ImageCopy() function has the following prototype:

 int ImageCopy(int src, int dest, int destX, int destY, int srcX, int srcY,               int srcW, int srcH); 

This means that the function takes an image stream src and appends the portion of the stream starting at srcX , srcY with a width of srcW and a height of srcH to the image stream dest , at an X location of destX and a Y location of destY . In the example, we copy a 35_35 portion of tst1.jpg starting at 50, 100 to tst2.jpg starting at 50, 100.

Here we use the ImageCopy() function to copy one portion of one image to another portion of another image. However, if you simply want to move around parts of a single image, specify the same source and destination image:

 ImageCopy($im1, $im1, 50, 100, 25, 50, 25, 50); 

But be careful: If the pixel areas overlap, unpredictable things will occur!

If you want to copy part of the image, but resize the part of the image you are copying, use the ImageCopyResized() function. Here is a quick example:

 <?php $im = ImageCreateFromJpeg("tst.jpg"); $im1 = ImageCreateFromJpeg("tst1.jpg"); ImageCopyResized($im, $im1, 50, 100, 50, 100, 70, 70, 35, 35); ImageJpeg($im1); ImageDestroy($im); ImageDestroy($im2); ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net