View Full Version : image rotation

09-18-2009, 06:51 PM
hello, i want to take an image, rotate it 15 degrees then center it in a white canvas 220 width x 120 height.


09-18-2009, 06:55 PM
This sounds like it would be done with JavaScript, not PHP.

Edit: Seems I was wrong, sort of forgot about the GD library =/

09-18-2009, 09:35 PM
or flash.

09-18-2009, 10:51 PM
You can use php for this. I do.


should do it. For the list of GD functions that php uses go to http://us2.php.net/manual/en/book.image.php . Here is an amateur tutorial that I created on the subject. http://www.animeviews.com/article.php?ID=91&category=programming

09-18-2009, 10:52 PM


$filename = 'test.jpg';

header('Content-type: image/jpeg');

$source = imagecreatefromjpeg($filename);
$rotate = imagerotate($source, 15, 0);

$im = imagecreate(220, 120);
$img = imagecopy($im, $im, 0, 0, 0, 0, 0, 0);


I think I REALLY screwed that up, but that's the outline for you. HTH:)

09-18-2009, 11:54 PM
The GD library for PHP will rotate it and give you an image. Flash will let you do it on the page. Javascript won't be much help because it won't do well with non-90-degree angles (if you wanted to make an image sideways, there is probably a reasonably way in Javascript, but not 15 degrees).
Based on what you said, I think the GD library as suggested above is the right way to go.

However, it's probably better to just save these images in the first place as you want, using a PHP-GD script if you'd like, but not running it every time the page loads, because that will be slow and cause a strain on the server. If you are doing it with user uploads or something else you can't batch process before, then you could just process once with GD and save that output so you are still only running the script once.

09-19-2009, 12:13 AM
Sounds like he has a unique image every time, so I think he'll have no choice but to run the script each time. Maybe he can make a copy of the image if the image is repeated?

09-19-2009, 05:37 AM
Right. If it can't be pregenerated, then caching the image is the best option.

I have a gallery script I did where it is live-- it just grabs all the images from the folder and puts them on the page (as thumbnails generated by GD, with links to the actual files), but it does so by looking through all the images in the folder and generating the thumbnail only if the thumbnail is not already saved in a folder. I save the thumbnail based on the md5 value of the image itself, so that is an easy way to be sure the image hasn't been updated. The only downside to that is leaving extra thumbnails in the cached folder once an image is deleted, but it doesn't really cause much of a problem.

09-19-2009, 10:25 AM
thanks guys,

i'm just having a play with a few of the scripts..

If you look at the images i've attached, you can clearly see what im after..

thanks for your help

09-19-2009, 10:50 PM
In addition to the image you want, exactly, you need to also decide how you want it to work-- process the image every time the page loads, preprocess everything, do it only if it isn't already generated, etc. For this, the reasons behind the script are most important-- is it part of your layout, for products on a page, for user uploaded photos, etc.