【问题标题】:Any reliable method to rotate uploaded pictures in the right direction?有什么可靠的方法可以将上传的图片朝正确的方向旋转?
【发布时间】:2012-04-23 17:16:20
【问题描述】:

前段时间,我写了一个方法,可以从在线社区上传的图片中读取 EXIF 数据。从图片中获得方向后,我将文件旋转到正确的方向。我的脚本只有一小部分:

public function rotate($sourceFile, $targetFile, $orienation)
{
    switch($orienation){
        case 3: $degrees = '180'; break;
        case 6: $degrees = '90'; break;
        case 8: $degrees = '270'; break;
        default: $degrees = '0';
    }

    $file = $sourceFile . DS . $targetFile;
    $rotate = GlobalConfig::BIN_IMAGICK_CONVERT . ' -rotate ' . $degrees . ' ' . $file . ' ' . $file;
    exec($rotate);
}

我的脚本正在运行。但我的问题是,其他大个子喜欢什么 facebook,处理上传图片的旋转?因为我的解决方案肯定不是最好的。

【问题讨论】:

  • 您是在问“执行旋转操作的最佳方式是什么”或“确定图像方向的最佳方式是什么”?
  • 注意可以使用原生的ImageMagickphp扩展
  • 如果if ($degrees),至少我会包装exec() - 将图像旋转0度是没有意义的......

标签: php image upload rotation exif


【解决方案1】:

我相信更好的方法是使用 GD 库,代码如下所示

// input
$image = 'myfile.jpg';

//read orientation information from EXIF

$exif = exif_read_data($filename);
$ort = $exif['IFD0']['Orientation'];

//then based on $ort you can determine the rotation degree,
//check the link below for more info

//rotation degree
$degrees = 180;

// create "image object"
$source = imagecreatefromjpeg($image) ;

// rotate
$rotate = imagerotate($source, $degrees, 0) ;

// set the header
header('Content-type: image/jpeg') ;

// output
imagejpeg($rotate) ;

更多信息:Exif Orientation Tag

【讨论】:

  • 我忘了说你也可以使用Imagick php.net/manual/en/imagick.rotateimage.php
  • 对不起,我的要求不准确。我认为这个过程的困难部分是,找出图像必须向哪个方向旋转。因为并非所有智能手机或相机都会保存方向。我读了这个例子的方向: $exif = exif_read_data('image.jpg', 0, true); var_dump($exif['IFD0']['方向']);
  • @johchri 看看this SO question
  • @johchri,我更新了答案并添加了一个可以帮助您的链接,如果没有关于图像中存储的方向的信息,我认为您超出了 PHP 问题的范围并为 AI 做出更多贡献
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
相关资源
最近更新 更多