public static function magick_resize_image($convert, $src_fn, $resized_fn, $max_x, $max_y, $resize_type = RESIZE_CROP) { Logger::log("Resizing {$src_fn} to {$resized_fn} using ImageMagick"); if (!$convert) { $convert = ImageResize::find_magick(); } list($w, $h) = getimagesize($src_fn); $x_scale = floatval($max_x) / floatval($w); $y_scale = floatval($max_y) / floatval($h); switch ($resize_type) { case RESIZE_STRETCH: $args = "-geometry {$max_x}x{$max_y}!"; break; case RESIZE_CROP: case RESIZE_CROP_NO_EXPAND: case RESIZE_CROP_NO_SCALE: // first crop the input image, then resize that to fit the output $scale = max($x_scale, $y_scale); if ($resize_type == RESIZE_CROP_NO_EXPAND) { $scale = min(1.0, $scale); } if ($resize_type == RESIZE_CROP_NO_SCALE) { $scale = 1.0; } // convert output size into input coords $input_max_x = min($w, intval(floatval($max_x) / $scale + 0.5)); $input_max_y = min($h, intval(floatval($max_y) / $scale + 0.5)); if ($resize_type == RESIZE_CROP_NO_EXPAND) { $max_x = intval(floatval($input_max_x) * $scale + 0.5); $max_y = intval(floatval($input_max_y) * $scale + 0.5); } // now find offsets $input_x_offset = intval(($w - $input_max_x) / 2); $input_y_offset = intval(($h - $input_max_y) / 2); $args = "-crop {$input_max_x}x{$input_max_y}+{$input_x_offset}+{$input_y_offset} -geometry {$max_x}x{$max_y}!"; break; case RESIZE_FIT: $args = "-geometry {$max_x}x{$max_y}"; break; case RESIZE_FIT_NO_EXPAND: $max_x = min($max_x, $w); $max_y = min($max_y, $h); $args = "-geometry {$max_x}x{$max_y}"; break; } // call /usr/bin/convert and return an error if it fails $cmd = $convert . ' ' . ImageResize::shell_escape($src_fn) . " {$args} " . ImageResize::shell_escape($resized_fn); system($cmd, $retval); if ($retval) { throw new CNException(GENERAL_SOME_ERROR, "{$convert} returned error code {$retval} (for command: {$cmd})"); } }