2017-03-17 630 views
1

我正在使用下面的代码来旋转和保存存储在服务器上的图像。将变量传递给Ajax调用

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="http://beneposto.pl/jqueryrotate/js/jQueryRotateCompressed.js"></script> 

<img src="uploads/101822973264163759893-1428320267361.jpeg" id="img"/> 

<script> 
$(document).ready(function(){ 
var value = 0 
    $("#img").rotate({ 
     bind: 
     { 
      click: function(){ 
       value +=90; 
       $(this).rotate({ animateTo:value}) 
      } 
     } 

    }); 


    $(document).on('click', '#img', function() { 
       // alert("Hello"); 

       var idno = "test"; 
       $.ajax({ 
         url: "saveimage.php", 
         dataType: "html", 
         type: 'POST', 
         data: "panelid="+idno, //variable with data 
         success: function(data){ 
          // $(".test").html(data); 
           // alert(data); 
         } 
       }); 
    }); 
}); 
</script> 

saveimage.php

<?Php 
$degrees = -90; //change this to be whatever degree of rotation you want 

header('Content-type: image/jpeg'); 

$filename = 'uploads/101822973264163759893-1428320267361.jpeg'; //this is the original file 

$source = imagecreatefromjpeg($filename) or notfound(); 
$rotate = imagerotate($source,$degrees,0); 

imagejpeg($rotate,$filename); //save the new image 

imagedestroy($source); //free up the memory 
imagedestroy($rotate); //free up the memory 

?> 

我在这里找到这段代码 - http://www.codehaven.co.uk/rotate-and-save-an-image-using-php/

这一切工作正常,但我想通过一个PHP变量持有的图像路径和名称的Ajax调用,并不知道如何做到这一点。

任何人都可以帮忙吗?

感谢,

约翰

回答

1

你也可以传递变量作为URL参数:

url: "saveimage.php?path=/folder/to/your/image/&image=image_name", 

您可以使用进行urlencode,以确保所有的特殊字符通过

+0

谢谢!这比我想象的要容易。 –