php旋转并保存图像 - 没有做任何事情?

时间:2013-06-11 03:31:29

标签: php image image-rotation

我正在尝试使用以下代码来旋转并保存图像,但它似乎没有做任何事情,也没有吐出任何错误?

这是我的代码:

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

$path   = '/Volumes/yoda/websites/zepel.website.2013/images/blog/display';
$file   = '10.jpg';
$degrees    = 90;

$filename   = $path."/".$file;

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

imagejpeg($filename,$rotate);

imagedestroy($source);
imagedestroy($rotate);

非常感谢任何帮助。我的图像文件夹也设置为777。我似乎无法弄清楚它为什么不起作用?

4 个答案:

答案 0 :(得分:3)

给它一个旋转:(已测试)

<?php

$path   = '/Volumes/yoda/websites/zepel.website.2013/images/blog/display';
$file   = '10.jpg';
$degrees = 90;

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

$filename = $path . "/" .$file;

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

imagejpeg($rotate);
imagedestroy($source);
imagedestroy($rotate);

?>

答案 1 :(得分:1)

问题在于imagejpg功能。 它应该是这样的:

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

chek imagerotate manual link

修改

问题是,你是在浏览器中显示图像而算是另一个文本,这就是为什么你得到那个“奇怪的文字”,试着保存图像:

imagejpeg($rotate, "test.jpeg");

并删除标题('Content-type:image / jpeg');

答案 2 :(得分:0)

You can use this for rotate image

$image = 'images/me.jpg';



// Set the path to the image to rotate
$input_image = $image;
//How many degrees you wish to rotate
$degrees = 90;
// Create the canvas

            $info = getimagesize($image);

            if($info['mime']) {
                if($info['mime']=='image/gif'){
                    $canvas = imagecreatefromgif($image);
                    // Rotates the image
                    $rotate = imagerotate( $canvas, $degrees, 0 ) ;
                    // Save the image as output.jpg
                    imagegif( $rotate, $image );
                }elseif($info['mime']=='image/jpeg'){
                    $canvas = imagecreatefromjpeg($image);
                    // Rotates the image
                    $rotate = imagerotate( $canvas, $degrees, 0 ) ;
                    // Save the image as output.jpg
                    imagejpeg( $rotate, $image );

                }elseif($info['mime']=='image/png'){
                    $canvas = imagecreatefrompng($image);
                    // Rotates the image
                    $rotate = imagerotate( $canvas, $degrees, 0 ) ;
                    // Save the image as output.jpg
                    imagepng( $rotate, $image );
                }
            }
// Clear the memory of the tempory image
imagedestroy( $canvas );

?>
<img src="<?=$image?>">

答案 3 :(得分:0)

这对我有用(更改为png) 如果标头数据尚未输出,则可以使用标头。 如果标题不存在,请使用“ /> 如果不添加时间,则浏览器可能只会显示缓存的版本,而不会显示您刚刚更新的旋转版本。请注意,使用lastmod会导致更长的页面加载时间,尤其是在图像较大的情况下,但是如果用户想查看发生了什么,则必须使用它。

$rotateFilename="path to file (not http)";
$degrees = 90;

if($fileType == 'jpg' || $fileType == 'jpeg'){
 // if header data has not gone out yet...  header('Content-type: image/jpeg');
$source = imagecreatefromjpeg($rotateFilename);
 // Rotate
$rotate = imagerotate($source, $degrees, 0); //do the deed
imagejpeg($rotate,$rotateFilename); //saves the file


// Free the memory
imagedestroy($source);
imagedestroy($rotate);
}