How to check/fix image rotation before upload image using PHP

前端 未结 1 1692
别跟我提以往
别跟我提以往 2020-12-09 13:03

I have a issiue when uploading a image taken using iphone. I am trying to use PHP and the imgrotate function to automaticly decide if the image needs to be rotated to the co

相关标签:
1条回答
  • 2020-12-09 13:42

    You are using imagerotate wrong. It expect first parameter to be resource while you are passing the filename. Check manual

    Try this:

    <?php
    
    $filename = $_FILES['file']['name'];
    $filePath = $_FILES['file']['tmp_name'];
    $exif = exif_read_data($_FILES['file']['tmp_name']);
    if (!empty($exif['Orientation'])) {
        $imageResource = imagecreatefromjpeg($filePath); // provided that the image is jpeg. Use relevant function otherwise
        switch ($exif['Orientation']) {
            case 3:
            $image = imagerotate($imageResource, 180, 0);
            break;
            case 6:
            $image = imagerotate($imageResource, -90, 0);
            break;
            case 8:
            $image = imagerotate($imageResource, 90, 0);
            break;
            default:
            $image = $imageResource;
        } 
    }
    
    imagejpeg($image, $filename, 90);
    
    ?>
    

    Do not forget to free the memory by adding following two lines:

    imagedestroy($imageResource);
    imagedestroy($image);
    
    0 讨论(0)
提交回复
热议问题