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 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表