所以我上传的图片我尝试做的就是让用户旋转它,接收该值并旋转图像并用新图像替换原始图像。
所以我有一个按钮:
<a href="#" data-id="2" class="btn btn-primary">Rotate</a>
在我的js文件中:
$('.rotate').on('click', function(e){
e.preventDefault();
if(rotate < 360)
rotate = rotate +90;
else
rotate = 0;
var id = $(this).data('id');
$('.img-container img').attr('style', '-webkit-transform: rotate(' + rotate + 'deg)');
$.ajax({
type: "POST",
url: "functions/post",
data: { rotate: rotate, id: id }
});
});
and inside&#34; functions / post&#34;:
$degrees = $_POST['rotate'];
$postid = $_POST['postid'];
//user the postid to get the imagefilepath from db
$filename = '../img/uploads/' . $post->getimagepath();
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
imagejpeg($rotate);
list($width, $height) = getimagesize($rotate);
$newImage = imagecreatetruecolor($width, $height);
imagecopyresampled($newImage, $source, 0, 0, 0, 0, $width, $height, imagesx($source), imagesy($source));
imagejpeg($newImage, realpath('../img/uploads/' . $post->getimagepath()));
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
imagedestroy($newImage);
但它没有保存旋转的文件.. :( 这个代码我是通过搜索和谷歌搜索得到的,但似乎无法让它对我有用。这些代码都不是一成不变的,所以任何需要改变的东西都会让它变得有效,然后它就会变得非常好并且非常感激。
答案 0 :(得分:1)
您没有保存旋转的图像,而是重新保存原始图像。
试试这个:
$degrees = $_POST['rotate'];
$postid = $_POST['postid'];
//user the postid to get the imagefilepath from db
$filename = '../img/uploads/' . $post->getimagepath();
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
imagejpeg($rotate, realpath('../img/uploads/' . $post->getimagepath()));
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
(未经测试)但你明白了。