【问题标题】:php iphone/IOS6 upload rotation issue: what is best way to save rotated imagephp iphone/IOS6上传旋转问题:保存旋转图像的最佳方法是什么
【发布时间】:2013-01-15 02:28:22
【问题描述】:

使用 IOS6 的 safari 移动浏览器,文件上传功能让用户可以选择拍照。不幸的是,在拍摄照片时,虽然照片拇指正确显示在浏览器中,但当您上传到服务器时,文件会旋转 90 度。这似乎是由于 iphone 设置的 exif 数据。我有通过在服务时旋转图像来固定方向的代码。但是,我怀疑保存旋转的、正确定向的图像会更好,这样我就不必再担心方向了。我的许多其他照片甚至都没有 exif 数据,如果可以避免的话,我不想弄乱它。

任何人都可以建议代码来保存图像以使其正确定位吗?

这是旋转图像的代码。以下代码将显示正确定向的图像,但是,我要做的是保存它,以便我可以随时提供它而无需担心方向。

我还想在下面的代码中替换 impagejpeg 调用,以便任何代码都适用于 gif 和 jpg。

感谢您的建议/代码!

PHP

//Here is sample image after uploaded to server and moved to a directory
$target = "pics/779_pic.jpg";
$source = imagecreatefromstring(file_get_contents($target));
$exif = exif_read_data($target);
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']) {
        case 8:
            $image = imagerotate($source,90,0);
//echo 'It is 8';
            break;
        case 3:
            $image = imagerotate($source,180,0);
//echo 'It is 3';
            break;
        case 6:
            $image = imagerotate($source,-90,0);
//echo 'It is 6';
            break;

    }
}
// $image now contains a resource with the image oriented correctly
//This is where I want to save resource properly oriented instead of display.
 header('Content-type: image/jpg');
imagejpeg($image);
?>

【问题讨论】:

  • 发现它可以在没有标题和 imagejpeg($image,$target); 的情况下完成仅适用于 jpeg,但这在短期内解决了我的问题。

标签: php iphone rotation orientation exif


【解决方案1】:

只有 JPEG 或 TIFF 文件可以携带 EXIF 元数据,因此无需担心使用您的代码处理 GIF(或 PNG)。

我认为是官方规范的第 9 页:

压缩文件以 JPEG (ISO/IEC 10918-1) 格式记录,并插入了应用程序标记段(APP1 和 APP2)。未压缩文件以 TIFF Rev. 6.0 格式记录。

http://www.cipa.jp/english/hyoujunka/kikaku/pdf/DC-008-2010_E.pdf

【讨论】:

    【解决方案2】:

    要保存图像,只需使用相同的函数imagejpeg 和下一个参数来保存图像,例如:

    imagejpeg($image, $target, 100);
    

    在这种情况下,您不需要指定标题,因为您没有显示任何内容。

    参考: http://sg3.php.net/manual/en/function.imagejpeg.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2015-07-25
      • 1970-01-01
      相关资源
      最近更新 更多