dongxian3418 2010-08-19 14:24
浏览 55
已采纳

使用PHP和jQuery快速旋转图像

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.

  • 写回答

3条回答 默认 最新

  • dousi4900 2010-08-19 14:29
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序