Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Images Wish List | Report Bug | About Me ]

11.2.11     Basic image copying: imagecopy() and imagecopymerge()

This is NOT the latest copy of this book; click here for the latest version.

int imagecopy ( resource dest_image, resource source_image, int dest_x, int dest_y, int source_x, int source_y, int source_width, int source_height)

int imagecopymerge ( resource dest_image, resource source_image, int dest_x, int dest_y, int source_x, int source_y, int source_width, int source_height, int merge_percentage)

These two functions are both quite similar in that they copy one picture into another. Both of their first eight parameters are identical:

  • The destination image you're copying to

  • The source image you're copying from

  • The X co-ordinate you want to copy to

  • The Y co-ordinate you want to copy to

  • The X co-ordinate you want to copy from

  • The y co-ordinate you want to copy from

  • The width in pixels of the source image you want to copy

  • The height in pixels of the source image you want to copy

Parameters three and four allow you to position the source image where you want it on the destination image, and parameters five, six, seven, and eight allow you to define the rectangular area of the source image that you want to copy. Most of the time you will want to leave parameters five and six at 0 (copy from the top-left hand corner of the image), and parameters seven and eight at the width of the source image (the bottom-right corner of it) so that it copies the entire source image.

The way these functions differ is in the last parameter: imagecopy() always overwrites all the pixels in the destination with those of the source, whereas imagecopymerge() merges the destination pixels with the source pixels by the amount specified in the extra parameter: 0 means "keep the source picture fully", 100 means "overwrite with the source picture fully", and 50 means "mix the source and destination pixel colours equally". The imagecopy() function is therefore equivalent to calling imagecopymerge() and passing in 100 as the last parameter.

Take a look at these two example pictures:





Now, to get those two to merge we need a script like this one:

<?php
  $stars
= imagecreatefrompng("stars.png");
  
$gradient = imagecreatefrompng("gradient.png");
  
imagecopymerge($stars, $gradient, 0, 0, 0, 0, 256, 256, 60);
  
header('Content-type: image/png');
  
imagepng($stars);
  
imagedestroy($stars);
  
imagedestroy($gradient);
?>

That merges the two at 60%, which gives slightly more prominence to the gradient. The result is shown below: subtly gradiated stars.







<< 11.2.10 Using brushes: imagesetbrush()   11.2.12 Scaling and rotating: imagecopyresized(), imagecopyresampled(), and imagerotate() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Gosoe - 30 Aug 2008

Thanks for the great tutorial, busy trying to use to copyright my images using a copyright.png to display on top. Just need to figure out how to implement these codes with mine, cause images get stored on the database, using Mysql and PHP - Field Type - BLOB
-- Cheerio

A PHP User - 30 Aug 2008

This is really very useful and also well explained

Roswell - 30 Aug 2008

Thanks a lot for your turorial !

Vishwa - 30 Aug 2008

Fantastic Tutorial...Thanks !



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow