3

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

这是我的代码:

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

4 回答 4

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);

?>
于 2013-06-11T03:50:59.080 回答
1

问题出在 imagejpg 函数中。应该是这样的:

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

chek imagerotate 手动链接

编辑

问题是您在浏览器中显示图像并处理另一个文本,这就是为什么您会得到那个“奇怪的文本”,尝试保存图像:

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

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

于 2013-06-11T03:46:35.250 回答
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?>">
于 2016-05-02T11:03:23.220 回答
0

这对我有用,(更改为 png )如果标题数据尚未消失,您可以使用标题。如果 header 已经消失,请使用 " /> 如果您不添加时间,您的浏览器可能只会显示缓存版本,而不是您刚刚更新的旋转版本。请注意,使用 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);
}
于 2019-04-18T12:08:15.400 回答