Reputation: 4253
I get exif to read orientation of upload images. The problem is in some images I'm getting this error:
warning exif_read_data(php3KLADx): File not supported in /home/i/public_html/orientation.php on line 5
any ideas how to avoid this?
<?php
function exif_orientation($file_tmp) {
$image = imagecreatefromstring(file_get_contents($file_tmp));
$exif = exif_read_data($file_tmp);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
imagejpeg($image, $file_tmp, 90);
}
}
?>
Upvotes: 5
Views: 11875
Reputation: 1454
You Should do it Like below :
if (!function_exists('imageOrientation'))
{
function imageOrientation(string $directory)
{
if(file_exists($directory))
{
$destination_extension = strtolower(pathinfo($directory, PATHINFO_EXTENSION));
if(in_array($destination_extension, ["jpg","jpeg"]) && exif_imagetype($directory) === IMAGETYPE_JPEG)
{
if(function_exists('exif_read_data'))
{
$exif = exif_read_data($directory);
if(!empty($exif) && isset($exif['Orientation']))
{
$orientation = $exif['Orientation'];
switch ($orientation)
{
case 2:
$flip = 1;
$deg = 0;
break;
case 3:
$flip = 0;
$deg = 180;
break;
case 4:
$flip = 2;
$deg = 0;
break;
case 5:
$flip = 2;
$deg = -90;
break;
case 6:
$flip = 0;
$deg = -90;
break;
case 7:
$flip = 1;
$deg = -90;
break;
case 8:
$flip = 0;
$deg = 90;
break;
default:
$flip = 0;
$deg = 0;
}
$img = imagecreatefromjpeg($directory);
if($deg !== 1 && $img !== null)
{
if($flip !== 0)
{
imageflip($img,$flip);
}
$img = imagerotate($img, $deg, 0);
imagejpeg($img, $directory);
}
}
}
}
}
}
}
Upvotes: 5
Reputation: 153
You could suppress the output of the warning by using:
@exif_read_data($file_tmp)
Of course, suppressing warnings is not a good idea, but there doesn't seem to be a way of avoiding this warning: there's no function to test the validity of the EXIF data without trying to read it and getting the warning.
I tried testing with
exif_imagetype($file_tmp)
but some valid image types can have readable EXIF data or not. PNG is one: I found examples where one file could be read just fine but the other gave the warning.
Upvotes: 5