肖像图像通过codeigniter调整大小库顺时针旋转到270度

Himanshu Sharma

每当我上传图像并使用图像标签/背景图像属性显示它时,图像会自动顺时针旋转到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方向数据的移动设备捕获的(有关详细信息,请参阅有关EXIF方向的出色文章)。

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

实际上,情况恰恰相反:图像没有旋转,正好按照存储的状态显示。在新的浏览器窗口或其他图像处理程序中打开图像,该图像将根据EXIF方向值自动旋转以按预期显示。

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

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

$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;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章