break; } // Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image! $image->setImageOrientation(imagick::ORIENTATION_TOPLEFT); } if (isset($_POST['myFile']) && !empty($_POST['myFile'])) { // Example: $path_parts = pathinfo($_POST['myFile']); //echo $path_parts['dirname'], "\n"; //echo $path_parts['basename'], "\n"; //echo $path_parts['extension'], "\n"; $tmp_filename = "uploads/" . $_POST['myFile']; // If it doesn't work change this to 'name' $im = new Imagick($tmp_filename); // Rotate correctly (using EXIF data) autoRotateImage($im); // Change format to jpg //$im->setImageFormat( "jpg" ); $im->setImageColorspace(255); // TODO might not be needed? $im->setCompression(Imagick::COMPRESSION_JPEG); $im->setCompressionQuality(90); // TODO set accordingly (in conjunction with blur setting) $im->setImageFormat('jpeg'); // TODO proper size, and blur setting. Also try FILTER_CATROM (similar to LANCZOS but much faster) // TODO disable upscaling $im->resizeImage(1080, 1920, Imagick::FILTER_CATROM, 1, true); // overwrite tmp file $im->writeImage($tmp_filename); // TODO if this doesn't work, can we just send output to $destfilename below? $im->clear();
/** * Resizes an image proportionally to fit within the defined max_width and max_height limits * * - Will do nothing to the image if the file fits within the size limits * - If Image Magick is present it will use those function over any GD solutions * - If GD2 is present, it'll use it to achieve better quality (imagecopyresampled) * - Saves the new image to destination_filename, in the preferred_format * if possible, default is jpeg. * * @uses GD * @uses Imagick * * @package Graphics * @param resource|null $src_img null for Imagick images, resource form imagecreatefrom for GD * @param string $destName * @param int $src_width * @param int $src_height * @param int $max_width * @param int $max_height * @param bool $force_resize = false * @param int $preferred_format = 0 */ function resizeImage($src_img, $destName, $src_width, $src_height, $max_width, $max_height, $force_resize = false, $preferred_format = 0) { global $gd2; if (checkImagick()) { // These are the file formats we know about static $default_formats = array('1' => 'gif', '2' => 'jpeg', '3' => 'png', '6' => 'bmp', '15' => 'wbmp'); $preferred_format = empty($preferred_format) || !isset($default_formats[$preferred_format]) ? 2 : $preferred_format; // Since Imagick can throw exceptions, lets catch them try { // Get a new instance of Imagick for use $imagick = new Imagick($destName); // Fix image rotations for iphone cameras etc. autoRotateImage($imagick); // Set the input and output image size $src_width = empty($src_width) ? $imagick->getImageWidth() : $src_width; $src_height = empty($src_height) ? $imagick->getImageHeight() : $src_height; $dest_width = empty($max_width) ? $src_width : $max_width; $dest_height = empty($max_height) ? $src_height : $max_height; // Create a new image in our prefered format and resize it if needed $imagick->setImageFormat($default_formats[$preferred_format]); $imagick->resizeImage($dest_width, $dest_height, Imagick::FILTER_LANCZOS, 1, true); // Save the new image in the destination location $success = $imagick->writeImage($destName); // Free resources associated with the Imagick object $imagick->destroy(); } catch (Exception $e) { // Not currently used, but here is the error $success = $e->getMessage(); $success = false; } return !empty($success); } elseif (checkGD()) { $success = false; // Determine whether to resize to max width or to max height (depending on the limits.) if (!empty($max_width) || !empty($max_height)) { if (!empty($max_width) && (empty($max_height) || $src_height * $max_width / $src_width <= $max_height)) { $dst_width = $max_width; $dst_height = floor($src_height * $max_width / $src_width); } elseif (!empty($max_height)) { $dst_width = floor($src_width * $max_height / $src_height); $dst_height = $max_height; } // Don't bother resizing if it's already smaller... if (!empty($dst_width) && !empty($dst_height) && ($dst_width < $src_width || $dst_height < $src_height || $force_resize)) { // (make a true color image, because it just looks better for resizing.) if ($gd2) { $dst_img = imagecreatetruecolor($dst_width, $dst_height); // Deal nicely with a PNG - because we can. if (!empty($preferred_format) && $preferred_format == 3) { imagealphablending($dst_img, false); if (function_exists('imagesavealpha')) { imagesavealpha($dst_img, true); } } } else { $dst_img = imagecreate($dst_width, $dst_height); } // Resize it! if ($gd2) { imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } else { imagecopyresamplebicubic($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } } else { $dst_img = $src_img; } } else { $dst_img = $src_img; } // Save the image as ... if (!empty($preferred_format) && $preferred_format == 3 && function_exists('imagepng')) { $success = imagepng($dst_img, $destName); } elseif (!empty($preferred_format) && $preferred_format == 1 && function_exists('imagegif')) { $success = imagegif($dst_img, $destName); } elseif (function_exists('imagejpeg')) { $success = imagejpeg($dst_img, $destName, 80); } // Free the memory. imagedestroy($src_img); if ($dst_img != $src_img) { imagedestroy($dst_img); } return $success; } else { return false; } }