图像旋转不起作用 PHP



这让我头疼:/我似乎无法弄清楚为什么图像没有旋转。函数中的代码正在运行,所以我不明白为什么图像只是不旋转。这是函数

function image_fix_orientation($image) {
    $exif = exif_read_data($image);
    $orientation = $exif['Orientation'];
    print_r($exif);
    if (!empty($orientation)) {
        echo 'has orientation';
        //$original_photo = imagecreatefromjpeg($image);
        switch ($orientation) {
            case 3:
                echo 'run 3 ';
                $original_photo = imagecreatefromjpeg($image);
                $new_photo = imagerotate ($original_photo , 180 , 0 );
                break;
            case 6:
                echo ' run 6';
                $original_photo = imagecreatefromjpeg($image);
                $new_photo = imagerotate ($original_photo , -90 , 0 );
                break;
            case 8:
                echo ' run 8';
                $original_photo = imagecreatefromjpeg($image);
                $new_photo = imagerotate ($original_photo , 90 , 0 );
                break;
        }
        return $new_photo;
    }
}
$image = 'test/IMG_4386.JPG';
$new = 'test/IMG_4386_NEW.JPG';
$new_photo = image_fix_orientation($image); 
imagejpeg($new_photo, $new);
echo '<br>';
echo '<img src="'.$new.'" width="800" height="800" />';

谢谢

编辑:替换有效的代码。这将创建一个正确旋转的新图像。我不想创建图像。我实际上需要在将图像上传到服务器之前对其进行编辑,但我不知道如何做到这一点。基本上,我试图使这个函数工作,因为我需要从$_FILES中获取图像,如果需要,请旋转它,然后将其传递给将处理图像处理的类。我无权访问该课程。这可能吗?在将图像传递给将处理图像的类之前旋转图像?

可以试试这个。

<?php
    function image_fix_orientation($image) {
        $exif = exif_read_data($image);
        $orientation = $exif['Orientation'];
        print_r($exif);
        $original_photo = null;
        if (!empty($orientation)) {

            switch ($orientation) {
                case 3:
                    $original_photo = imagecreatefromjpeg($image);
                    return imagerotate ($original_photo , 180 , 0 );
                case 1:
                    $original_photo = imagecreatefromjpeg($image);
                    return imagerotate ($original_photo , -90 , 0 );
                case 8:
                    $original_photo = imagecreatefromjpeg($image);
                    return imagerotate ($original_photo , 90 , 0 );
            }
        }
    }
    $rotatedImage = image_fix_orientation("/path/to/image.jpeg");
    // Set the content type header - in this case image/jpeg
    header('Content-Type: image/jpeg');
    // Output the image
    imagejpeg($rotatedImage);
   ?>

转自 http://php.net/manual/en/function.imagerotate.php

最新更新