views:

61

answers:

3

I wrote a python script that rotates an image 90 degrees. I am including the python code in case you want to see it;

#! /usr/bin/python
# This Python file uses the following encoding: utf-8


#argv[1] needs to be send formatted meaning spaces and paranthesis ARE problems

__author__="john"
__date__ ="$Aug 17, 2010 1:48:36 PM$"

server_directory="some_directory"

import os
import os.path
import sys
import Image

#for turkish characters
def tr(utf):
    return utf.decode('utf-8')

img_directory=sys.argv[1]
img_directory_orig=img_directory.replace("\ ", " ")
file_url_and_name=server_directory+img_directory_orig
im = Image.open(file_url_and_name)
im1=im.rotate(270)
out=file(file_url_and_name,"w")
im1.save(out,"JPEG")
out.close()

Simple enough. So what I used to do is simply when a link is clicked a sample link is as below;

echo '<div style="text-align: center ;margin-left: auto ; margin-right: auto ;"><a class="button" href="fotograf.php?open='.$going_to_open_dir.'&rotate=temp/'."m_".$fake_going_to_open_dir."_".$fake_entry.'&num=foto'.$a1.'" onclick="this.blur();"><span>90&deg; turn</span></a></div>';

So far so good. Oh and let me add the php code calling my nice little python app;

if(isset($_GET["rotate"]))
    {
        exec("python rotate_image.py ".$_GET["rotate"]);
        header("location: fotograf.php?open=".$_GET['open']."&num=".$_GET['num']."#".$_GET['num']);
    }

So my problem is: Even though my system works its just too slow. Especially when there is about 600 pictures waiting to be loaded each time a picture turns. My question is there a way to speed it up using jQuery(Ajax)? Basically what I'm trying to do is : I am simply trying to rotate one image among 600 images in a web page and saving the rotated version on the server without the need of reloading the whole page.

A: 

You seem to be creating the rotated version each time it's requested from the PHP script. Check if it already exists and only rotate if not.

In other words, in your PHP script, check if you already generated the rotated file before. If yes, don't run the python script again, just redirect to the rotated file.

Example of what you have now - before anything is run:

somefile.jpg

request.php?rotate=somefile.jpg:

rotate? yes -> exec python script

after the script finishes (it doesn't matter what exactly the filenames are):

somefile.jpg
somefile_rotated.jpg

So, at this point, you don't need to exec the python script again when requesting somefile.jpg, but you still do:

request.php?rotate=somefile.jpg:

rotate? yes -> exec python script // even though you already have the output file

What you could do:

request.php?rotate=somefile.jpg:

rotate? yes 
did we rotate the file already?  (=is there a rotated version on our server?)
   -> yes, redirect to it
   -> no, exec python script, then redirect to the rotated file
Piskvor
The rotated version will never exist before hand since if you looked at the code it re-writes over the old image. Hence once turned... it stays forever turned...
JohnRoach
Yes, it stays forever turned. Therefore, it is pointless to keep on overwriting it, if you could just check "is it already there?". Updated the answer.
Piskvor
A: 

What in particular is too slow? There are lots of ways to speed this up:

  • Cache the rotated images. This is the obvious one.
  • Load the rotated images when the page is loaded, set to invisible. This will cause the browser to cache them
  • AJAX a request for the images.
katrielalex
The part that reloading the 600 images is slow as I have stated in my question. Each time the page reloads (for the get) It calls for all 600 images. And as for Cache I can't really use that since the image may have changed. i.e. rotated...
JohnRoach
@John: Huh? What is the functionality you are trying to achieve here?
katrielalex
I am simply trying to rotate one image among 600 images in a page and saving the rotated version on the server without the need of reloading the whole page.
JohnRoach
Sorry for not making that a little more clearer...
JohnRoach
@John: Oh! Yes, you can use jQuery to do that. You'd need to make a request to the server for the rotated image (which will also rotate the image), wait for the update, and dynamically change the image src. Do you want specific pointers on how to do that?
katrielalex
Use a server-side cache. Store the result of the rotation and serve up that file instead of having do a fresh rotation from scratch each time. Loading, uncompressing, rotating, recompressing an image are highly expensive in terms of CPU time and memory usage. You're forcing the server to do this for EVERY image request, EVERY TIME. If you get 50 users, that's 3000 rotations to do.
Marc B
@katrielalex pointers would be welcomed. thank you.
JohnRoach
@John: Try http://jqueryfordesigners.com/image-loading/ for starters. Googling "jquery load image" or similar should come up with a whole bunch of tutorials.
katrielalex
@John: but this is sort of sidestepping the problem, in that you are still requiring the server to perform a rotation every time one is requested. It would be faster to cache all the images at all necessary rotations server-side, and then just serve up the required one on demand.
katrielalex
+2  A: 

If you are happy to do this just in the browser, the tricks in this article will let you rotate any html content, including image tags.

-webkit-transform: rotate(-90deg); 
-moz-transform: rotate(-90deg); 
filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
Douglas
is there a way to only rotate the image and not re-load the whole page?? (and after reloading I need to save the image) Thank you for your prompt response.
JohnRoach
I've not experimented with it, but I imagine that you could have four css classes, rotate-0, rotate-90, rotate-180 and rotate-270, each with a variation of the css in my answer above. The you could use jQuery to change the classes applied to the image as you "rotate" them. The original image on the server would not need to change, though you could do that too in the background using Ajax.
Douglas