肖像图像正被 codeigniter 调整大小库顺时针旋转 270 度

Portrait Images are being rotated to 270 degrees clockwise by codeigniter resize library

每当我上传图片并使用图片标签/背景图片显示它时属性,图片会自动顺时针旋转270度,但是当我在新的[=18=中打开图片时】 来对了。

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

当我尝试使用 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 方向数据的移动设备拍摄的(有关更多详细信息,请参阅 this excellent post 关于 EXIF 方向)。

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

实际上情况恰恰相反:图像没有旋转,它的显示与存储的完全一样。在新的浏览器 window 或其他图像处理程序中打开图像,它会根据 EXIF 方向值自动旋转以显示如您所愿。

GD 显示图像 'correctly' 因为它不会以任何方式改变图像,除非您指示它这样做。

要以您认为正确的方式显示图像,您需要使用以下代码(来自 ), 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;
    }
}