PHP read_exif_data和调整方向

如果方向关闭,我正在使用以下代码旋转上载的jpeg图像。从iPhone和Android上传的图像只有问题。


if(move_uploaded_file($_FILES['photo']['tmp_name'], $upload_path . $newfilename)){

            chmod($upload_path . $newfilename, 0755);

            $exif = exif_read_data($upload_path . $newfilename);

            $ort = $exif['IFD0']['Orientation'];

            switch($ort)

            {


                case 3: // 180 rotate left

                    $image->imagerotate($upload_path . $newfilename, 180, -1);

                    break;



                case 6: // 90 rotate right

                    $image->imagerotate($upload_path . $newfilename, -90, -1);

                    break;


                case 8:    // 90 rotate left

                    $image->imagerotate($upload_path . $newfilename, 90, -1);

                    break;

            }

            imagejpeg($image, $upload_path . $newfilename, 100);

            $success_message = 'Photo Successfully Uploaded';

        }else{

            $error_count++;

            $error_message = 'Error: Upload Unsuccessful<br />Please Try Again';

        }

我从jpeg读取EXIF数据的方式有问题吗?它没有像预期的那样旋转图像。


这是我运行var_dump($ exif)时发生的情况;


array(41) {

    ["FileName"]=> string(36) "126e7c0efcac2b76b3320e6187d03cfd.JPG"

    ["FileDateTime"]=> int(1316545667)

    ["FileSize"]=> int(1312472)

    ["FileType"]=> int(2)

    ["MimeType"]=> string(10) "image/jpeg"

    ["SectionsFound"]=> string(30) "ANY_TAG, IFD0, THUMBNAIL, EXIF"

    ["COMPUTED"]=> array(8) {

        ["html"]=> string(26) "width="2048" height="1536""

        ["Height"]=> int(1536)

        ["Width"]=> int(2048)

        ["IsColor"]=> int(1)

        ["ByteOrderMotorola"]=> int(1)

        ["ApertureFNumber"]=> string(5) "f/2.8"

        ["Thumbnail.FileType"]=> int(2)

        ["Thumbnail.MimeType"]=> string(10) "image/jpeg" }

        ["Make"]=> string(5) "Apple"

        ["Model"]=> string(10) "iPhone 3GS"

        ["Orientation"]=> int(6)

        ["XResolution"]=> string(4) "72/1"

largeQ
浏览 594回答 3
3回答

阿波罗的战车

imagerotate文档为第一个参数引用的类型与您使用的类型不同:由图像创建功能之一(例如imagecreatetruecolor())返回的图像资源。这是使用此功能的一个小例子:function resample($jpgFile, $thumbFile, $width, $orientation) {&nbsp; &nbsp; // Get new dimensions&nbsp; &nbsp; list($width_orig, $height_orig) = getimagesize($jpgFile);&nbsp; &nbsp; $height = (int) (($width / $width_orig) * $height_orig);&nbsp; &nbsp; // Resample&nbsp; &nbsp; $image_p = imagecreatetruecolor($width, $height);&nbsp; &nbsp; $image&nbsp; &nbsp;= imagecreatefromjpeg($jpgFile);&nbsp; &nbsp; imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);&nbsp; &nbsp; // Fix Orientation&nbsp; &nbsp; switch($orientation) {&nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image_p = imagerotate($image_p, 180, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 6:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image_p = imagerotate($image_p, -90, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 8:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image_p = imagerotate($image_p, 90, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; // Output&nbsp; &nbsp; imagejpeg($image_p, $thumbFile, 90);}

素胚勾勒不出你

根据丹尼尔(Daniel)的代码,我编写了一个函数,该函数仅在必要时旋转图像,而无需重新采样。GDfunction image_fix_orientation(&$image, $filename) {&nbsp; &nbsp; $exif = exif_read_data($filename);&nbsp; &nbsp; if (!empty($exif['Orientation'])) {&nbsp; &nbsp; &nbsp; &nbsp; switch ($exif['Orientation']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = imagerotate($image, 180, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 6:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = imagerotate($image, -90, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 8:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = imagerotate($image, 90, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}一线版本(GD)function image_fix_orientation(&$image, $filename) {&nbsp; &nbsp; $image = imagerotate($image, array_values([0, 0, 0, 180, 0, 0, -90, 0, 90])[@exif_read_data($filename)['Orientation'] ?: 0], 0);}图像魔术function image_fix_orientation($image) {&nbsp; &nbsp; if (method_exists($image, 'getImageProperty')) {&nbsp; &nbsp; &nbsp; &nbsp; $orientation = $image->getImageProperty('exif:Orientation');&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $filename = $image->getImageFilename();&nbsp; &nbsp; &nbsp; &nbsp; if (empty($filename)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filename = 'data://image/jpeg;base64,' . base64_encode($image->getImageBlob());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $exif = exif_read_data($filename);&nbsp; &nbsp; &nbsp; &nbsp; $orientation = isset($exif['Orientation']) ? $exif['Orientation'] : null;&nbsp; &nbsp; }&nbsp; &nbsp; if (!empty($orientation)) {&nbsp; &nbsp; &nbsp; &nbsp; switch ($orientation) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image->rotateImage('#000000', 180);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 6:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image->rotateImage('#000000', 90);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 8:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image->rotateImage('#000000', -90);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

森栏

对于上传图像的人来说,功能更简单,它会在必要时自动旋转。function image_fix_orientation($filename) {&nbsp; &nbsp; $exif = exif_read_data($filename);&nbsp; &nbsp; if (!empty($exif['Orientation'])) {&nbsp; &nbsp; &nbsp; &nbsp; $image = imagecreatefromjpeg($filename);&nbsp; &nbsp; &nbsp; &nbsp; switch ($exif['Orientation']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = imagerotate($image, 180, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 6:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = imagerotate($image, -90, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 8:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = imagerotate($image, 90, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; imagejpeg($image, $filename, 90);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP