note 58361 added to function.imagecopymerge

From: Date: Tue, 01 Nov 2005 22:15:25 +0000
Subject: note 58361 added to function.imagecopymerge
Groups: php.notes 
Request: Send a blank email to php-notes+get-97950@lists.php.net to get a copy of this message
Task: Rotate a large image, then reduce it in size and place it on a small background (i.e. as an Inset). Problem: If you resize the image first, the rotation becomes hugely aliased... So, it makes sense to rotate it first, then shrink it. Unfortunately, when you resample the image, you lose the background color (at least, some of it may change), so you can no longer set transparancy as you require. If instead you resize the image (rather than resample), again, the aliasing looks bad. Solution: Resize the background - make it bigger. Then add the original (large) inset, and resize the whole thing back to normal. <?php /* We will shrink the inset to 25% */ $resizePercentage = 0.25; /* Load a source image and a background */ $iSource = ImageCreateFromJpeg($source_file); $iBackground = ImageCreateFromJpeg($background_file); /* Do something here, such a rotate, skew etc */ ... /* Assume $iSource is still the image we want to insert onto the background */ /* Set the background color to be transparent */ $cBackground = ImageColorClosest($iSource, 255, 0, 255); ImageColorTransparent($iSource, $cBackground); /* Resize the background - make it huge */ $iBackground = ImageResize($iTemplate, ImageSX($iBackground ) / $resizePercentage, ImageSY($iBackground ) / $resizePercentage); /* Place the image on the background - all full size, so no aliasing issues */ ImageCopyMerge($iBackground , $iSource, ((ImageSX($iBackground ) - ImageSX($iSource)) / 2), ((ImageSY($iBackground ) - ImageSY($iSource)) / 2) - 25, 0, 0, ImageSX($iWorking), ImageSY($iSource), 100); /* Shrink the combined image... no issues with transparancy! */ $iBackground = ImageResize($iTemplate, ImageSX($iBackground ) * $resizePercentage, ImageSY($iBackground ) * $resizePercentage); /* Output the image as a PNG */ header("Content-Type: image/png"); ImagePng($iBackground); exit(); function ImageResize($pImage, $t_width, $t_height) { // Target image $iCanvas = @ImageCreateTrueColor($t_width, $t_height); // Source dimensions $s_width = ImageSX($pImage); $s_height = ImageSY($pImage); // Copy image ImageCopyResampled($iCanvas, $pImage, 0, 0, 0, 0, $t_width, $t_height, $s_width, $s_height); // Return image return $iCanvas; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.imagecopymerge.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58361 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58361&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58361&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58361&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58361&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58361&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58361&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58361&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58361&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97950) next »