使用 PHP 和 jQuery 快速旋转图像
我编写了一个将图像旋转 90 度的 python 脚本。我包含了 python 代码,以防您想查看它;
#! /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()
够简单的。所以我过去所做的只是当单击链接时,示例链接如下所示;
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° turn</span></a></div>';
到目前为止,一切都很好。哦,让我添加调用我漂亮的小 python 应用程序的 php 代码;
if(isset($_GET["rotate"]))
{
exec("python rotate_image.py ".$_GET["rotate"]);
header("location: fotograf.php?open=".$_GET['open']."&num=".$_GET['num']."#".$_GET['num']);
}
所以我的问题是:尽管我的系统运行速度太慢。尤其是每次一张图片转动时,大约有600张左右的图片等待加载。我的问题是有没有办法使用 jQuery(Ajax) 来加速它?基本上我想做的是:我只是尝试在网页中的 600 张图像中旋转一张图像,并将旋转后的版本保存在服务器上,而不需要重新加载整个页面。
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° 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您愿意在浏览器中执行此操作,请参阅本文中的技巧 将允许您旋转任何 html 内容,包括图像标签。
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.
每次 PHP 脚本请求时,您似乎都会创建旋转版本。检查它是否已经存在,如果不存在则仅旋转。
换句话说,在您的 PHP 脚本中,检查您之前是否已经生成了旋转文件。如果是,请不要再次运行 python 脚本,只需重定向到旋转的文件。
您现在拥有的示例 - 在运行任何内容之前:
request.php?rotate=somefile.jpg:
脚本完成后(文件名到底是什么并不重要):
所以,此时,您不需要在请求 somefile.jpg 时再次执行 python 脚本,但你仍然这样做:
request.php?rotate=somefile.jpg:
你可以做什么:
request.php?rotate=somefile.jpg:
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:
request.php?rotate=somefile.jpg:
after the script finishes (it doesn't matter what exactly the filenames are):
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:
What you could do:
request.php?rotate=somefile.jpg:
特别是什么太慢了?有很多方法可以加快速度:
What in particular is too slow? There are lots of ways to speed this up: