【问题标题】:Portrait Images are being rotated to 270 degrees clockwise by codeigniter resize library肖像图像被codeigniter调整大小库顺时针旋转270度
【发布时间】:2019-01-17 03:23:38
【问题描述】:

每当我上传图像并使用图像标签/背景图像属性显示它时,图像会自动顺时针旋转 270 度,但是当我在新窗口中打开图像时,它会正确显示。

我尝试使用具有基本样式的简单图像选项卡显示图像,但如果图像处于纵向模式,则会将其转换为横向

当我尝试使用 codeignitor 调整大小库 (GD2) 调整它的大小时,它的行为方式与 HTML 相同(将生成的图像顺时针旋转 270 度)。调整大小后,它们已永久转换为横向模式。 CodeIgniter 中用于调整图片大小的代码是

        $this->load->library( 'image_lib' );
        $config[ 'image_library' ] = 'gd2';
        $config[ 'source_image' ] = $file;
        $config[ 'maintain_ratio' ] = TRUE;
        $config[ 'overwrite' ] = TRUE;
        $config[ 'master_dim' ] = 'auto';
        $config[ 'width' ] = $width;
        $config[ 'height' ] = $height;
        $config[ 'autoOrient' ] = FALSE;
        $config[ 'new_image' ] = $file;
        $this->image_lib->initialize( $config );
        if ( !$this->image_lib->resize() ) {
            return array( 'msg' => $this->image_lib->display_errors(), 'error' => 0 );
        }else{
            return array( 'msg' => 'success', 'error' => 1 );
        }

【问题讨论】:

  • 如果没有要检查的图像,很难为您提供帮助 - 请在您的问题中附上一张!但这可能是图像来自移动设备并具有 EXIF 方向值的情况。
  • 是的,这仅适用于从移动设备上传的图像。

标签: html image codeigniter gd


【解决方案1】:

发生这种情况是因为图像是使用嵌入了 EXIF 方向数据的移动设备捕获的(有关更多详细信息,请参阅this excellent post 关于 EXIF 方向)。

图像会自动顺时针旋转 270 度,但是当我在新窗口中打开图像时,它会正确显示。

实际上正好相反:图像没有被旋转,它完全按照存储的方式显示。在新的浏览器窗口或其他图像处理程序中打开图像,它会根据 EXIF 方向值自动旋转以按预期显示。

GD 正在“正确”显示图像,因为它不会以任何方式更改图像,除非您指示它。

要以您认为正确的方式显示图像,您需要使用以下代码(来自this answer),这取决于exif extensionenabled in your php.ini

$filepath = ''; // path to the image you want to manipulate.
$image = ''; // using imagecreatefrom...

// Rotate image correctly!
$exif = exif_read_data($filepath);
if (!empty($exif['Orientation'])) {
    switch ($exif['Orientation']) {
        case 1: // nothing
            break;
        case 2: // horizontal flip
            imageflip($image, IMG_FLIP_HORIZONTAL);
            break;
        case 3: // 180 rotate left
            $image = imagerotate($image, 180, 0);
            break;
        case 4: // vertical flip
            imageflip($image, IMG_FLIP_VERTICAL);
            break;
        case 5: // vertical flip + 90 rotate right
            imageflip($image, IMG_FLIP_VERTICAL);
            $image = imagerotate($image, -90, 0);
            break;
        case 6: // 90 rotate right
            $image = imagerotate($image, -90, 0);
            break;
        case 7: // horizontal flip + 90 rotate right
            imageflip($image, IMG_FLIP_HORIZONTAL);
            $image = imagerotate($image, -90, 0);
            break;
        case 8:    // 90 rotate left
            $image = imagerotate($image, 90, 0);
            break;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 2019-07-11
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多