使用 PHP 和 jQuery 快速旋转图像

发布于 2024-09-15 19:21:10 字数 1536 浏览 2 评论 0原文

我编写了一个将图像旋转 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&deg; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫负雪 2024-09-22 19:21:10

如果您愿意在浏览器中执行此操作,请参阅本文中的技巧 将允许您旋转任何 html 内容,包括图像标签。

-webkit-transform: rotate(-90deg); 
-moz-transform: rotate(-90deg); 
filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);

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);
治碍 2024-09-22 19:21:10

每次 PHP 脚本请求时,您似乎都会创建旋转版本。检查它是否已经存在,如果不存在则仅旋转。

换句话说,在您的 PHP 脚本中,检查您之前是否已经生成了旋转文件。如果是,请不要再次运行 python 脚本,只需重定向到旋转的文件。

您现在拥有的示例 - 在运行任何内容之前:

somefile.jpg

request.php?rotate=somefile.jpg:

rotate? yes -> exec python script

脚本完成后(文件名到底是什么并不重要):

somefile.jpg
somefile_rotated.jpg

所以,此时,您不需要在请求 somefile.jpg 时再次执行 python 脚本,但你仍然这样做:

request.php?rotate=somefile.jpg:

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

你可以做什么:

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

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
智商已欠费 2024-09-22 19:21:10

特别是什么太慢了?有很多方法可以加快速度:

  • 缓存旋转的图像。这是显而易见的。
  • 页面加载时加载旋转的图像,设置为不可见。这将导致浏览器缓存
  • AJAX 图像请求。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文