/** * resize */ static function resize($file, $required_width, $required_height = false, $method = 'extent', $gravity = 'center', $fill = false) { //first check file exists and is readable if (!is_readable(ONXSHOP_PROJECT_DIR . $file)) { return false; } //prepare variables if (is_numeric($required_height)) { $directory = ONXSHOP_PROJECT_DIR . "var/thumbnails/{$required_width}x{$required_height}"; $thumb_file = "var/thumbnails/{$required_width}x{$required_height}/" . md5($file); } else { $directory = ONXSHOP_PROJECT_DIR . "var/thumbnails/{$required_width}"; $thumb_file = "var/thumbnails/{$required_width}/" . md5($file); } $file_rp = ONXSHOP_PROJECT_DIR . $file; $image_resize_options = "_{$method}_{$gravity}_{$fill}"; // TODO check valid options if ($image_resize_options == "___") { $image_resize_options = ""; } if ($image_resize_options) { $thumb_file = $thumb_file . $image_resize_options; } $thumb_file_rp = ONXSHOP_PROJECT_DIR . $thumb_file; //check if the destination directory exits if (!is_readable($directory)) { if (!mkdir($directory)) { msg("common_image.resize(): Cannot create folder {$directory}", 'error'); } } //determinate real image size $size = common_image::getImageSize($file_rp); $width = $required_width; $height = round($required_width / $size['proportion']); msg("Thumbnail will have size {$width} x {$height}", 'ok', 3); /** * resize method option */ if (!in_array(strtolower($method), array('crop', 'extent'))) { $method = 'extent'; } /** * gravity option * NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast */ if (!in_array(strtolower($gravity), array('northwest', 'north', 'northeast', 'west', 'center', 'east', 'southwest', 'south', 'southeast'))) { $gravity = 'center'; } /** * fill option */ if ($fill == true) { $fill = '^'; } else { $fill = ''; } /** * if height is specified align in center and allow to fill the space * see http://www.imagemagick.org/Usage/thumbnails/#cut * http://www.imagemagick.org/Usage/resize/#shrink */ if (is_numeric($required_height)) { $background = common_image::supportsTransparency($file_rp) ? 'none' : 'white'; $other_im_params = "-background {$background} -alpha background -gravity {$gravity} -{$method} {$width}x{$required_height}"; $height = $required_height; } else { $other_im_params = ''; } /** * return cached file or create with ImageMagick */ if (is_readable($thumb_file_rp)) { return $thumb_file; } else { $image_configuration = common_image::initConfiguration(); $jpeg_quality = $image_configuration['jpeg_quality']; $file_rp_escaped = escapeshellarg($file_rp); $width = (int) $width; $height = (int) $height; $jpeg_quality = (int) $jpeg_quality; $thumb_file_rp_escaped = escapeshellarg($thumb_file_rp); //usage: resize $filename $width $height $target_filename $quality $other_options ///usr/bin/convert "$1" -colorspace RGB -depth 8 -quality $QUALITY -thumbnail $2x$3^ $6 "$4" $colorspace = self::isOldImageMagickVersion() ? 'RGB' : 'sRGB'; $shell_command = "convert {$file_rp_escaped} -colorspace {$colorspace} -depth 8 -quality {$jpeg_quality} -thumbnail {$width}x{$height}{$fill} {$other_im_params} {$thumb_file_rp_escaped}"; $result = local_exec($shell_command); if (is_readable($thumb_file_rp)) { return $thumb_file; } else { return false; } } }