Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi
I am using EXIF Orientation to get the orientation of the images coming from iphone5 and 6. Unfortunately when I print $file_exif not able to get the orientation of that images. If I have download some sample images from google having orientation I can see the value in the variable.
I have also tried to customize a bit still not able get the orientation.
Original Code in the Module :
function _exif_orientation_rotate($file) {
if (function_exists('exif_read_data') && $file->filemime == 'image/jpeg') {
$file_exif = exif_read_data(drupal_realpath($file->uri));
// Ensure that the Orientation key|value exists, otherwise leave.
if (!isset($file_exif['Orientation'])) {
return;
}
// Orientation numbers and corresponding degrees.
// @note: Odd numbers are flipped images, would need different process.
switch ($file_exif['Orientation']) {
case 3:
$degrees = 180;
break;
case 6:
$degrees = 90;
break;
case 8:
$degrees = 270;
break;
default:
$degrees = 0;
}
if ($degrees > 0) {
// Load the image object for manipulation
$file_image = image_load(drupal_realpath($file->uri));
if (image_rotate($file_image, $degrees)) {
image_save($file_image);
}
}
}
}
Modified Code to check :
if (function_exists('exif_read_data') && $file->filemime == 'image/jpeg') {
// $file_exif = exif_read_data(drupal_realpath($file->uri));
// // Ensure that the Orientation key|value exists, otherwise leave.
// if (!isset($file_exif['Orientation'])) {
// return;
// }
$image_file_path = drupal_realpath($file->uri);
// $image_file_path = $image_file_info[0]->filepath;
$imge_info = image_get_info($image_file_path);
$img = imagecreatefromjpeg("{$image_file_path}");
$width = $imge_info['width'];
$height = $imge_info['height'];
$thumbWidth = $imge_info['width'];
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor($height * ( $thumbWidth / $width ));
// create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
// copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$exif = exif_read_data(drupal_realpath($file->uri));
$file_exif = $exif["Orientation"];
// Orientation numbers and corresponding degrees.
// @note: Odd numbers are flipped images, would need different process.
echo '<pre>';
print_r($exif); die;
switch ($file_exif['Orientation']) {
case 3:
$degrees = 180;
break;
case 6:
$degrees = 90;
break;
case 8:
$degrees = 270;
break;
default:
$degrees = 0;
}
if ($degrees > 0) {
// Load the image object for manipulation
$file_image = image_load(drupal_realpath($file->uri));
if (image_rotate($file_image, $degrees)) {
image_save($file_image);
}
}
}
}
Thanks
Jyotisankar
Comment | File | Size | Author |
---|---|---|---|
#6 | correct_background_color.patch | 434 bytes | emilymoi |
#1 | exif_orientation-orientation-iphone5-6-2540228-2-7.39.patch | 487 bytes | ReBa |
Comments
Comment #1
ReBa CreditAttribution: ReBa as a volunteer commentedI've been dealing with the same issue for my iphone device.
There was an issue within the image.gd.inc image_gd_rotate function where the (only) jpeg image can't get a proper background and the background returned -1 which broke the resource to become a boolean in stead of a resource object.
Please review.
Comment #2
mglamanUpdating issue formatting.
Comment #3
mglamanWow, great catch, and that makes complete sense.
Comment #6
emilymoi CreditAttribution: emilymoi commentedWas running an older version of the module and just noticed the problem with image.gd.inc today.
What I don't understand is why a string 'FFFFFF' is passed instead of a hex int 0xFFFFFF here. The documentation clearly asks for a hex int and if you take a look in image.gd.inc, it's doing shift operations on the background variable.
Passing a string 'FFFFFF' does not break only because the shift operations in image.gd.inc end up with a valid background color of RGB 0,0,0 and because we're only rotating in increments of 90 degrees we don't see the black background.
This should be changed to
Comment #7
RunePhilosof CreditAttribution: RunePhilosof commentedComment #10
othermachines CreditAttribution: othermachines commentedProblem mentioned in #6 was fixed in #2781621-4: Invalid hex value specified.