首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有可靠的方法将上传的图片旋转到正确的方向?

问有没有可靠的方法将上传的图片旋转到正确的方向?
EN

Stack Overflow用户
提问于 2012-04-23 16:35:23
回答 1查看 2.1K关注 0票数 0

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

代码语言:javascript
复制
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这样的大公司如何处理上传的照片的轮换?因为我的解决方案肯定不是最好的。

EN

回答 1

Stack Overflow用户

发布于 2012-04-23 16:43:47

我认为更好的方法是使用GD库,代码是这样的

代码语言:javascript
复制
// 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10277258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档