如何旋转图像并保存图像

gal*_*iya 14 javascript php ajax jquery image-rotation

在我的应用程序中,我有一个div,一个按钮的图像.

我想旋转显示的图像,并在使用jquery单击按钮时保存旋转的图像.

我已经使用过代码:

http://code.google.com/p/jquery-rotate/

和jquery代码:

$(function() {                                    // doc ready
                var rotation = 0;                             // variable to do rotation with
                $("#img").click(function() {
                    rotation = (rotation + 45) % 360; // the mod 360 probably isn't needed
                    $("#cropbox").rotate(rotation);
                });
            });
Run Code Online (Sandbox Code Playgroud)

HTML代码:

<img src="demo_files/pool.jpg" id="cropbox" />
<input type="button" id="img" name="img" value="click" />
Run Code Online (Sandbox Code Playgroud)

当我使用上面的代码时,有两个图像,一个是旧图像,另一个是旋转图像.

在这里,我想旋转相同的图像,只显示旋转的图像.并将旋转的图像保存在目录中.

我怎么能用jquery做到这一点?如果用jquery不可能那么我怎么能用php/ajax做到这一点?

小智 23

//define image path
$filename="image.jpg";

// Load the image
$source = imagecreatefromjpeg($filename);

// Rotate
$rotate = imagerotate($source, $degrees, 0);

//and save it on your server...
imagejpeg($rotate, "myNEWimage.jpg");
Run Code Online (Sandbox Code Playgroud)

看一眼:

imagerotate()

和:

file_put_contents()

  • 你实际上需要使用[`imagepng()`](http://www.php.net/manual/en/function.imagepng.php)来编写文件,而不是`file_put_contents()`. (7认同)
  • 使用 imagepng() 或 imagejpeg() 而不是 file_put_contents()。 (2认同)

Gha*_*hel 14

图像旋转:PNG或JPEG取决于文件类型,并在服务器上保存

// File and rotation
$rotateFilename = '/var/www/your_image.image_type'; // PATH
$degrees = 90;
$fileType = strtolower(substr('your_image.image_type', strrpos('your_image.image_type', '.') + 1));

if($fileType == 'png'){
   header('Content-type: image/png');
   $source = imagecreatefrompng($rotateFilename);
   $bgColor = imagecolorallocatealpha($source, 255, 255, 255, 127);
   // Rotate
   $rotate = imagerotate($source, $degrees, $bgColor);
   imagesavealpha($rotate, true);
   imagepng($rotate,$rotateFilename);

}

if($fileType == 'jpg' || $fileType == 'jpeg'){
   header('Content-type: image/jpeg');
   $source = imagecreatefromjpeg($rotateFilename);
   // Rotate
   $rotate = imagerotate($source, $degrees, 0);
   imagejpeg($rotate,$rotateFilename);
}

// Free the memory
imagedestroy($source);
imagedestroy($rotate);
Run Code Online (Sandbox Code Playgroud)

这对我有用,试一试.