Your Location is: Home > Php
PHP Combine EXIF Rotate & JPEG Compression Functions
Question
Can someone help me combine these functions please? Detects EXIF orientation, rotates and compresses JPEG, second one compresseS JPEGs without EXIF data. I'M aware this is very clumsy and need to streamline it if anyone can suggest improvement?
// If EXIF Orientation data exists, rotate image as required
$savetmpname = $temp['tmp_name'];
function correctImageOrientation($savetmpname) {
if (function_exists('exif_read_data')) {
$exif = exif_read_data($savetmpname);
if($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if($orientation != 1){
$img = imagecreatefromjpeg($savetmpname);
$deg = 0;
switch ($orientation) {
case 3:
$deg = 180;
break;
case 6:
$deg = 270;
break;
case 8:
$deg = 90;
break;
}
if ($deg) {
$img = imagerotate($img, $deg, 0);
}
// rewrite rotated image back to the disk
imagejpeg($img, $savetmpname, 25);
} // if there is some rotation necessary
} // if have the exif orientation info
} // if function exists
}
//Compress JPEGs without EXIF orientation
function compressJPG($savetmpname) {
if (exif_imagetype($savetmpname) == IMAGETYPE_JPEG) {
$img = imagecreatefromjpeg($savetmpname);
//unlink($savetmpname);
imagejpeg($img,$savetmpname,25);
}
}
// Move uploaded files, rotate and compress if required
move_uploaded_file($savetmpname, $filetowrite);
correctImageOrientation($filetowrite);
compressJPG($filetowrite);