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
$resizePercentage = 0.25;
$iSource = ImageCreateFromJpeg($source_file);
$iBackground = ImageCreateFromJpeg($background_file);
...
$cBackground = ImageColorClosest($iSource, 255, 0, 255);
ImageColorTransparent($iSource, $cBackground);
$iBackground = ImageResize($iTemplate, ImageSX($iBackground ) / $resizePercentage, ImageSY($iBackground ) / $resizePercentage);
ImageCopyMerge($iBackground , $iSource,
((ImageSX($iBackground ) - ImageSX($iSource)) / 2),
((ImageSY($iBackground ) - ImageSY($iSource)) / 2) - 25, 0, 0, ImageSX($iWorking), ImageSY($iSource), 100);
$iBackground = ImageResize($iTemplate, ImageSX($iBackground ) * $resizePercentage, ImageSY($iBackground ) * $resizePercentage);
header("Content-Type: image/png");
ImagePng($iBackground);
exit();
function ImageResize($pImage, $t_width, $t_height) {
$iCanvas = @ImageCreateTrueColor($t_width, $t_height);
$s_width = ImageSX($pImage);
$s_height = ImageSY($pImage);
ImageCopyResampled($iCanvas, $pImage, 0, 0, 0, 0, $t_width, $t_height, $s_width, $s_height);
return $iCanvas;
}
?>