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

11.2.12     Scaling and rotating: imagecopyresized(), imagecopyresampled(), and imagerotate()

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

bool imagecopyresized ( resource dest_image, resource source_image, int dest_x, int dest_y, int source_x, int source_y, int dest_width, int dest_height, int source_width, int source_height)

bool imagecopyresampled ( resource dest_image, resource source_image, int dest_x, int dest_y, int source_x, int source_y, int dest_width, int dest_height, int source_width, int source_height)

resource imagerotate ( resource source_image, float angle, int background_color)

PHP offers you two different ways to resize an image, and you should choose the right one for your needs. The first option, imagecopyresized(), allows you to change the size of an image quickly and easily, but has the downside of producing fairly low-quality pictures. When an image with detail is resized, aliasing ("jaggies") is usually visible, which makes the resized version hard to read, particularly if the resizing was to an unusual size. The other option is imagecopyresampled(), which takes the same parameters as imagecopyresized() and works in the same way with the exception that the resized image is smoothed so that it is still visible. The downside here is that the smoothing takes more CPU effort, so the image takes longer to produce.

Here is an example of imagecopyresized() in action - save it as specialeffects.php:

<?php
    header
("content-type: image/png");
    
$src_img = imagecreatefrompng("complicated.png");
    
$srcsize = getimagesize("complicated.png");
    
$dest_x = $srcsize[0] / 1.5;
    
$dest_y = $srcsize[1] / 1.5;
    
$dst_img = imagecreatetruecolor($dest_x, $dest_y);

    
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $srcsize[0], $srcsize[1]);
    
imagepng($dst_img);
    
imagedestroy($src_img);
    
imagedestroy($dst_img);
?>

There are two images being used in there. The first one, $src_img, is created from an PNG screenshot of the online PHP manual - this contains lots of text, which highlights the aliasing problem with imagecopyresized() nicely. The variables $dest_x and $dest_y are set to be the width and height of complicated.png divided by 1.5, which will set the destination size to be 66% of the source size. Resizing "exact" values such as 10%, 50%, etc, usually looks better than resizing unusual values such as 66%, 79%, etc.

The second image is then created using imagecreatetruecolor() and our destination sizes, and stored in $dst_img. Now comes the key part: imagecopyresized() takes quite a few variables, and you really need not bother memorising them. They are, in order, the image to copy to, the image to copy from, the destination X co-ordinate, destination Y co-ordinate, source X co-ordinate, source Y co-ordinate, destination width, destination height, source width, and source height. Parameters three to six, the co-ordinates, allow you to copy regions of the pictures as opposed to the whole picture - PHP will copy from the specified co-ordinate to the end of the picture, so by passing in 0 we're using the entire picture. You probably will not ever want to copy regions using these parameters, so just leave them as 0.

Take a screenshot of a website of your choosing, and save it as complicated.png in the same directory as your PHP script, then load up specialeffects.php in your browser. All being well, you should see something similar to Figure 25 - the website picture has been resized down, but as a result all the text is hard if not impossible to read.



Now, to give you an idea why imagecopyresampled() is better, change the imagecopyresized() call to imagecopyresampled(). The parameter list is identical, so just change the function name. This time you should see a marked difference - the website is still smaller, but should be perfectly legible as the text should be nicely smoothed. This is demonstrated in the picture below:



The final special effect we're going to look at is imagerotate(), which, as you can guess, rotates an image. This is much easier to do than resizing and resampling, as it only has three parameters: the image to rotate, and the number of degrees anti-clockwise you wish to rotate it, and the colour to use wherever space is uncovered. The rotation is performed from the centre of the source image, and the destination image will automatically be sized to fit the whole of the rotated image.

The last parameter only really makes sense once you have seen it in action, so try out this script:

<?php
    $image
= imagecreatefrompng("button.png");
    
$hotpink = imagecolorallocate($image, 255, 110, 221);
    
$rotated_image = imagerotate($image, 50, $hotpink);

    
header("content-type: image/png");
    
imagepng($rotated_image);
    
imagedestroy($image);
    
imagedestroy($rotated_image);
?>

You'll need to put your own file in where I have used button.png, but otherwise you should see something like when you load the picture in your web browser.



The image has been rotated by 50 degrees, anti-aliased to avoid jagged lines, resized by the minimum amount so that the outputted picture has just enough space to hold the rotated image. Finally, note that the gaps in the image, effectively the "background", have been colour the hot pink we defined. White is usually preferable, but it would not have been quite so obvious in the screenshot!





<< 11.2.11 Basic image copying: imagecopy() and imagecopymerge()   11.2.13 Points and lines: imagesetpixel(), imageline(), and imagesetthickness() >>
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
RokKralj - 20 Aug 2008

mahe4us,
$clockwise=360-$anticlockwise;

this is the new generation of programmers without general knowledge :(

mahe4us/mahendran.vetriselvan@g-antssoft.com - 20 Aug 2008

This is the best tutorial about image functionalities. But I have one doubt in image rotation. Normally image rotate happening in anticlockwise direction. How can we rotate in clockwise direction. Please suggest your great ideas to me....

Thanks

A PHP User - 20 Aug 2008

Once you've rotated an image, how can you save it to the server overwriting the original file?

stu@wordpix.ca - 20 Aug 2008

This is very nice but what seems to be happening is the server must process the large image and make a smaller copy of it every time you look at it. So you're not only downloading a large image, you're processing it into a smaller image.

What's the point of all that extra work?

I want the script to create an image which can then be used over and over again so that it loads faster.

jscjignesh@yahoo.co.in - 20 Aug 2008

i have an image which contains barcode.
but it contains non required region like left side space , right side space.. etc

is it possibble to copy only that barcode image from whole image ??

please reply.

thanks,
Jignesh

voxel - 20 Aug 2008

Is the best explanation that I see in web. Very 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 one plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow