tags:

views:

529

answers:

3

I do not have the ability to use PHP's GD imagerotate() function, so I need to find another solution. I used the rotateImage() method found in the comments of the imagerotate() PHP help page, but it is very slow on large images.

I am horrible at image math, and am having a hard time formulating the correct Google search to get me pointed in the correct direction to get my images rotated more quickly.

Ideally I'd like to avoid calling an external application if I can find a solution that is close to the same speed from within PHP.

I do not have ImageMagick installed and would like non-ImageMagick that I can try out before resorting to installing ImageMagick.

Update

I was asked to specify why I had so many restrictions.

I had initially assumed that I could use imagerotate() directly but I discovered that it only works if PHP is built with PHP's version of GD. Some distros (at least Ubuntu, which I am using as my development environment) refuse to link against PHP's heavily modified version of the GD library. In these cases, trying to call imagerotate() results in:

PHP Fatal error: Call to undefined function: imagerotate()

I found a PHP workaround for image rotation using GD (without using the imagerotate() function) but it seems very slow. 15-20 seconds for a 2848x2136 image.

I am hoping to find a faster algorithm using GD but will consider using ImageMagick or some other external library as a last resort. It is not that I am unable to install ImageMagick, it is that I would prefer to find a faster alternative using PHP built-in functions before adding more dependencies to this project.

About exec(): As a general rule I try pretty hard to avoid handing off to exec(). In fact, I cannot remember anytime in the last five years that I have had to do this at all. I will consider doing so if nothing else rotates quickly enough, but I'd like to check out some other options before I go there.

+2  A: 

If you really can't use the GD library, is it possible you could use 4 different images for each one, each with the image being rotated at 90 degrees?

You could save 4 files per image - image_normal.jpg, image_90.jpg, image_180.jpg, image_270.jpg

Then when the user wants to rotate the image, you can just load the corresponding file.

Not the greatest hack, but a solution at best.

Hope that helps.

Evernoob
I think he is having trouble actually doing the rotation at speed, not actually doing the rotation.
graham.reeds
Yeah I know, but this method would be fast because no calculations or methods are necessary. You just load an image which you'd have to load anyway.
Evernoob
But he hasn't said if he will only be rotating through 90 degrees only.
graham.reeds
And also, he will anyway need to actually rotate images before saving them into these four files ;)
FractalizeR
@graham.reeds and @FractalizeR are correct. The image rotation cannot be done ahead of time with another application (like Photoshop) though in theory your idea is sound.
Beau Simensen
+3  A: 

This page seems to list a series of methods, but you will have to do the coding yourself. http://www.leptonica.com/rotation.html

graham.reeds
@graham.reeds Thanks for this. I actually found this last night as well. When I wrote this question I had hoped that someone would send me to some PHP code at least loosely based on this to get me started. Starting to feel like I'm going to have to do it myself which is scary. Pixels and I do not get along very well sometimes...
Beau Simensen
+1  A: 

You have access to exec() functions but cannot install ImageMagick? How is that?

You know you can dl() libraries at runtime, right?

Quamis
I did not say I cannot install ImageMagick, just that I wanted to try and find a faster alternative to my current GD solution before I resorted to other third party applications or libraries like ImageMagick.
Beau Simensen
Well you wont find anything done in script(PHP) that will get faster than compiled code... If you want speed you need to resort to external C/C++ libraries or apps.
Quamis