如何旋转图像并保存图像
在我的应用程序中,我有一个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); }); });
HTML代码:
当我使用上面的代码时,有两个图像,一个是旧图像,另一个是旋转图像。
在这里,我想旋转相同的图像并仅显示旋转的图像。并将旋转的图像保存在目录中。
我怎么能用jquery做到这一点? 如果用jquery不可能那么我怎么能用php / ajax做到这一点?
//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");
看一眼:
imagerotate()
和:
file_put_contents()
图像旋转: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);
这对我有用,试一试。