Example #1
0
/**
 * 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);
            // 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;
    }
}
Example #2
0
/**
 * Resizes src_img proportionally to fit within max_width and max_height limits
 * if it is too large.
 * If GD2 is present, it'll use it to achieve better quality.
 * It saves the new image to destination_filename, as preferred_format
 * if possible, default is jpeg.
 * @uses GD
 *
 * @param resource $src_img
 * @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, $modSettings;
    if (checkImagick()) {
        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;
        $imagick = new Imagick($destName);
        $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;
        $imagick->setImageFormat($default_formats[$preferred_format]);
        $imagick->resizeImage($dest_width, $dest_height, Imagick::FILTER_LANCZOS, 1, true);
        $success = $imagick->writeImage($destName);
        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);
        }
        // Free the memory.
        imagedestroy($src_img);
        if ($dst_img != $src_img) {
            imagedestroy($dst_img);
        }
        return $success;
    } else {
        // Without GD, no image resizing at all.
        return false;
    }
}