$height) { $xoord = ceil(($width - $height) / 2); // Then we read a square frame that equals the width $width = $height; } else { $yoord = ceil(($height - $width) / 2); $height = $width; } // Rotate JPG pictures // for more info on orientation see // http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/ $degrees = 0; $flip = ''; if (preg_match("/.jpg$|.jpeg$/i", $_GET['filename'])) { if (function_exists('exif_read_data') && function_exists('imagerotate')) { $exif = exif_read_data($_GET['filename'], 0, true); $ort = $exif['IFD0']['Orientation']; switch ($ort) { case 3: // 180 rotate right $degrees = 180; break; case 6: // 90 rotate right $degrees = 270; break; case 8: // 90 rotate left $degrees = 90; break; case 2: // flip vertical $flip = 'vertical'; break; case 7: // flipped $degrees = 90; $flip = 'vertical'; break; case 5: // flipped $degrees = 270; $flip = 'vertical'; break; case 4: // flipped $degrees = 180; $flip = 'vertical'; break; } } } $target = imagecreatetruecolor($get_size, $get_size); // if the picture can be transparent, add a white background if (in_array($get_filename_type, array("GIF", "PNG"))) { $backgroundColor = imagecolorallocate($target, 255, 255, 255); imagefill($target, 0, 0, $backgroundColor); } if ($get_filename_type == "JPG") { $source = imagecreatefromjpeg($get_filename); } if ($get_filename_type == "GIF") { $source = imagecreatefromgif($get_filename); } if ($get_filename_type == "PNG") { $source = imagecreatefrompng($get_filename); } imagecopyresampled($target, $source, 0, 0, $xoord, $yoord, $get_size, $get_size, $width, $height); imagedestroy($source); //proper rotation by jan niggemann if ($degrees != 0) { $target = imagerotate($target, $degrees, 0); } //proper mirror (aka flip) by jan niggemann if ($flip == 'vertical') { //only in php >= 5.5.0 ImageJPEG(imageflip($target, IMG_FLIP_VERTICAL),null,80); flipVertical($target); flipHorizontal($target); flipVertical($target); } ob_start(); // Start output buffering. header('Content-type: image/jpeg'); // We always render the thumbnail in JPEG even if the source is GIF or PNG. imagejpeg($target, null, 80); imagedestroy($target); $cachedImage = ob_get_contents(); // Get the buffer content. ob_end_flush(); // End buffering if (is_writable(dirname($thumbname))) { $fd = fopen($thumbname, "w"); // Save buffer to disk if ($fd) { fwrite($fd, $cachedImage); fclose($fd); } }