--- a/imagetools.php Mon Sep 01 13:05:52 2008 -0400
+++ b/imagetools.php Mon Sep 01 13:06:50 2008 -0400
@@ -38,7 +38,7 @@
/**
* Scales an image to the specified width and height, and writes the output to the specified
* file. Will use ImageMagick if present, but if not will attempt to scale with GD. This will
- * always scale images proportionally.
+ * always scale images proportionally, unless $preserve_ratio is set to false.
*
* Ported from Enano CMS (which is also my project)
*
@@ -47,10 +47,11 @@
* @param int Image width, in pixels
* @param int Image height, in pixels
* @param bool If true, the output file will be deleted if it exists before it is written
+ * @param bool If true, preserves the image's aspect ratio (default)
* @return bool True on success, false on failure
*/
-function scale_image($in_file, $out_file, $width = 225, $height = 225, $unlink = false)
+function scale_image($in_file, $out_file, $width = 225, $height = 225, $unlink = false, $preserve_ratio = true)
{
global $db, $session, $paths, $template, $plugins; // Common objects
@@ -117,8 +118,9 @@
// ImageMagick path seems screwy
return false;
}
- $cmdline = "\"$magick_path\" \"$in_file\" -resize \"{$width}x{$height}>\" \"$out_file\"";
- system($cmdline, $return);
+ $op = ( $preserve_ratio ) ? '>' : '!';
+ $cmdline = "\"$magick_path\" \"$in_file\" -resize '{$width}x{$height}$op' \"$out_file\"";
+ exec($cmdline, $output, $return);
if ( !file_exists($out_file) )
return false;
return true;
@@ -130,29 +132,37 @@
return false;
// calculate new width and height
- $ratio = $width_orig / $height_orig;
- if ( $ratio > 1 )
+ if ( $preserve_ratio )
{
- // orig. width is greater that height
- $new_width = $width;
- $new_height = round( $width / $ratio );
+ $ratio = $width_orig / $height_orig;
+ if ( $ratio > 1 )
+ {
+ // orig. width is greater that height
+ $new_width = $width;
+ $new_height = round( $width / $ratio );
+ }
+ else if ( $ratio < 1 )
+ {
+ // orig. height is greater than width
+ $new_width = round( $height / $ratio );
+ $new_height = $height;
+ }
+ else if ( $ratio == 1 )
+ {
+ $new_width = $width;
+ $new_height = $width;
+ }
+ if ( $new_width > $width_orig || $new_height > $height_orig )
+ {
+ // Too big for our britches here; set it to only convert the file
+ $new_width = $width_orig;
+ $new_height = $height_orig;
+ }
}
- else if ( $ratio < 1 )
- {
- // orig. height is greater than width
- $new_width = round( $height / $ratio );
- $new_height = $height;
- }
- else if ( $ratio == 1 )
+ else
{
$new_width = $width;
- $new_height = $width;
- }
- if ( $new_width > $width_orig || $new_height > $height_orig )
- {
- // Too big for our britches here; set it to only convert the file
- $new_width = $width_orig;
- $new_height = $height_orig;
+ $new_height = $height;
}
$newimage = @imagecreatetruecolor($new_width, $new_height);