Jump to content

help needed with GD function imagerotate()


marvelade

Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.