问题
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
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 );
}
回答1:
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).
The image is automatically being rotated to 270 degrees clockwise, but when I open the image in a new window it is coming correctly.
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 is showing the image 'correctly' in that it is not altering the image in any way other than you instruct it to.
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;
}
}
来源:https://stackoverflow.com/questions/54228562/portrait-images-are-being-rotated-to-270-degrees-clockwise-by-codeigniter-resize