肖像图像通过codeigniter调整大小库顺时针旋转到270度 [英] Portrait Images are being rotated to 270 degrees clockwise by codeigniter resize library

查看:89
本文介绍了肖像图像通过codeigniter调整大小库顺时针旋转到270度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Whenever I am uploading an image and displaying it using image tag/ background image property, The image is automatically being rotated to 270 degrees clockwise, But when I open the image in a new window it is coming correctly.

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

I tried to display an image using a simple image tab with basic styles, but if the image is in portrait mode, it is converting it to landscape

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

When I tried to resize it using codeignitor resize library( GD2 ), It is behaving the same way as HTML( rotating the resulting image to 270 degrees clockwise). After resizing they have permanently converted to landscape mode. The code used to resize images in CodeIgniter is

        $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方向数据的移动设备捕获的(请参阅这篇关于EXIF方向的出色文章以了解更多详情)。

This is happening because the images were captured using a mobile device that has embedded EXIF orientation data (refer to this excellent post about EXIF orientation for more detail).


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

The image is automatically being rotated to 270 degrees clockwise, but when I open the image in a new window it is coming correctly.

实际上,情况正相反:图像未旋转,其显示方式与存储时完全相同。在新的浏览器窗口或其他图像处理程序中打开图像,该图像会根据EXIF方向值自动旋转,以按预期显示。

Actually the opposite is happening: the image is not being rotated, it is being displayed exactly as it is stored. Opening the image in a new browser window, or other image manipulation program, it is being automatically rotated according to the EXIF orientation value to display as you expect.

GD显示正确显示图像,因为它不会以您指示的方式改变图像。

GD is showing the image 'correctly' in that it is not altering the image in any way other than you instruct it to.

以您认为正确的方式显示图像您将需要使用以下代码(来自此答案),该代码取决于 exif扩展已在您的php.ini中启用

To display the image in the way you consider to be correct you will need to use the following code (from this answer), which depends on the exif extension being enabled 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;
    }
}

这篇关于肖像图像通过codeigniter调整大小库顺时针旋转到270度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆