Hi All,
I'm struggling with an image-operation. I'm trying to scale, flip, move and rotate an image. However the imagerotate function is bugging me.
Because imagerotate() also moves the image down in relation to it's container we have to cancel that movement out.
the implicit movement equals the width of the original image, multiplied by the sine of the rotation angle so I subtract that amount from my delta_y. Here's the code:
<?php
$scale = isset($_GET['scl']) ? floatval($_GET['scl']) : 0.8;
$delta_x = isset($_GET['dx']) ? intval($_GET['dx']) : 10;
$delta_y = isset($_GET['dy']) ? intval($_GET['dy']) : 20;
$rotation = isset($_GET['rot']) ? intval($_GET['rot']) : 30;
$flip = isset($_GET['flip']) && $_GET['flip']=="true" ? true : false;
define("OUTPUTIMAGE", true);
// manipulate original image
// get the name
$userpic_path = "../userpics/waldo.jpg";
// convert to image resource
$userpic_image = imagecreatefromjpeg($userpic_path);
// prepare the container
$userpic_size_array = getimagesize($userpic_path);
$old_width = $userpic_size_array[0];
$old_height = $userpic_size_array[1];
$im = imagecreatetruecolor($old_width, $old_height);
// move, scale & flip
$new_width = $old_width * $scale;
$new_height = $old_height * $scale;
if($rotation != 0)
{
//imagerotate() performs an implicit move so we have to cancel that out
$rotation_radians = $rotation * (M_PI / 180);
$delta_y -= $new_width * sin($rotation_radians);
}
imagecopyresampled($im, $userpic_image, $delta_x, $delta_y, 0, 0, $new_width, $new_height, $old_width, $old_height);
// prepare the transparent color
$transp = imagecolorallocatealpha($im, 0, 0, 0, 0);
// rotate
$im = imagerotate($im, $rotation, $transp, false);
if(OUTPUTIMAGE)
{
header("Content-type: image/png");
imagepng($im, "", 0);
imagedestroy($im);
}
?>
The result can be found here:
http://www.marvelade.com/projects/devoslemmens/imgcrop/pages/png_generator.php?scl=1&dx=0&dy=0&rot=45&flip=false
feel free to play around with the GET variables as you please.
the original image is here:
http://www.marvelade.com/projects/devoslemmens/imgcrop/userpics/waldo.jpg
All help, remarks, comments are very much appreciated.
Best regards and thanks in advance,
Marvelade