繁体   English   中英

显示时图像旋转 90 度(由智能手机拍摄的图像)

[英]Image is rotated 90 degree when displayed (image captured by a smartphone)

嗨,我正在一个 php 网站上工作,用户可以使用 PHP 上传图像,然后系统将显示图像,调整大小(缩小尺寸)。 调整大小代码如下:

<?php 
ini_set('memory_limit', -1); 
ini_set('max_execution_time', 40000); 
require_once 'ThumbLib.inc.php';                                                                          
$fileName = (isset($_GET['file'])) ? urldecode($_GET['file']) : null;
$thumb = PhpThumbFactory::create($fileName);
$thumb->Resize($_GET['width'], $_GET['height']);
$thumb->show();
?>

其中 html 代码是

<img src="show_image.php?width=230&height=1000000&file=appsub/<?php echo $v["xfile"]; ?>">

如果用户通过 PC 上传图像并没有什么问题,但是当用户使用智能手机(例如 iPhone)拍摄照片时,有时显示的图像会旋转 90 度。

我该如何解决这个问题?

照片可能具有“方向”数据,因此您可以根据需要将其旋转回正常状态。 您可以在用户上传图片后立即使用以下代码:

请注意,您的服务器必须安装了 Imagick。 (大多数新服务器都有)

<?php
function autoRotateImage($image) {

    $orientation = $image->getImageOrientation();

    switch($orientation) {
        case imagick::ORIENTATION_BOTTOMRIGHT:
            $image->rotateimage("#000", 180); // rotate 180 degrees
        break;

        case imagick::ORIENTATION_RIGHTTOP:
            $image->rotateimage("#000", 90); // rotate 90 degrees CW
        break;

        case imagick::ORIENTATION_LEFTBOTTOM:
            $image->rotateimage("#000", -90); // rotate 90 degrees CCW
        break;

    }
    // Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image!

    $image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
}
?> 

 

<?php

$image = new Imagick('./sourcepath/'.$upload1);
autoRotateImage($image);

// - Do other stuff to the image here -

$image->writeImage('./destinationpath/'. $upload1);
?>

如果您不保存旋转的图像,您可以使用以下显示它(旋转后)

<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;

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

// Load
$source = imagecreatefromjpeg($filename);

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

// Output
imagejpeg($rotate);

// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM