36 } |
36 } |
37 |
37 |
38 /** |
38 /** |
39 * Scales an image to the specified width and height, and writes the output to the specified |
39 * Scales an image to the specified width and height, and writes the output to the specified |
40 * file. Will use ImageMagick if present, but if not will attempt to scale with GD. This will |
40 * file. Will use ImageMagick if present, but if not will attempt to scale with GD. This will |
41 * always scale images proportionally. |
41 * always scale images proportionally, unless $preserve_ratio is set to false. |
42 * |
42 * |
43 * Ported from Enano CMS (which is also my project) |
43 * Ported from Enano CMS (which is also my project) |
44 * |
44 * |
45 * @param string Path to image file |
45 * @param string Path to image file |
46 * @param string Path to output file |
46 * @param string Path to output file |
47 * @param int Image width, in pixels |
47 * @param int Image width, in pixels |
48 * @param int Image height, in pixels |
48 * @param int Image height, in pixels |
49 * @param bool If true, the output file will be deleted if it exists before it is written |
49 * @param bool If true, the output file will be deleted if it exists before it is written |
|
50 * @param bool If true, preserves the image's aspect ratio (default) |
50 * @return bool True on success, false on failure |
51 * @return bool True on success, false on failure |
51 */ |
52 */ |
52 |
53 |
53 function scale_image($in_file, $out_file, $width = 225, $height = 225, $unlink = false) |
54 function scale_image($in_file, $out_file, $width = 225, $height = 225, $unlink = false, $preserve_ratio = true) |
54 { |
55 { |
55 global $db, $session, $paths, $template, $plugins; // Common objects |
56 global $db, $session, $paths, $template, $plugins; // Common objects |
56 |
57 |
57 if ( !is_int($width) || !is_int($height) ) |
58 if ( !is_int($width) || !is_int($height) ) |
58 return false; |
59 return false; |
115 if ( !preg_match('/^([\/A-z0-9_-]+)$/', $magick_path) ) |
116 if ( !preg_match('/^([\/A-z0-9_-]+)$/', $magick_path) ) |
116 { |
117 { |
117 // ImageMagick path seems screwy |
118 // ImageMagick path seems screwy |
118 return false; |
119 return false; |
119 } |
120 } |
120 $cmdline = "\"$magick_path\" \"$in_file\" -resize \"{$width}x{$height}>\" \"$out_file\""; |
121 $op = ( $preserve_ratio ) ? '>' : '!'; |
121 system($cmdline, $return); |
122 $cmdline = "\"$magick_path\" \"$in_file\" -resize '{$width}x{$height}$op' \"$out_file\""; |
|
123 exec($cmdline, $output, $return); |
122 if ( !file_exists($out_file) ) |
124 if ( !file_exists($out_file) ) |
123 return false; |
125 return false; |
124 return true; |
126 return true; |
125 } |
127 } |
126 else if ( $can_use_gd ) |
128 else if ( $can_use_gd ) |
128 @list($width_orig, $height_orig) = @getimagesize($in_file); |
130 @list($width_orig, $height_orig) = @getimagesize($in_file); |
129 if ( !$width_orig || !$height_orig ) |
131 if ( !$width_orig || !$height_orig ) |
130 return false; |
132 return false; |
131 // calculate new width and height |
133 // calculate new width and height |
132 |
134 |
133 $ratio = $width_orig / $height_orig; |
135 if ( $preserve_ratio ) |
134 if ( $ratio > 1 ) |
136 { |
135 { |
137 $ratio = $width_orig / $height_orig; |
136 // orig. width is greater that height |
138 if ( $ratio > 1 ) |
|
139 { |
|
140 // orig. width is greater that height |
|
141 $new_width = $width; |
|
142 $new_height = round( $width / $ratio ); |
|
143 } |
|
144 else if ( $ratio < 1 ) |
|
145 { |
|
146 // orig. height is greater than width |
|
147 $new_width = round( $height / $ratio ); |
|
148 $new_height = $height; |
|
149 } |
|
150 else if ( $ratio == 1 ) |
|
151 { |
|
152 $new_width = $width; |
|
153 $new_height = $width; |
|
154 } |
|
155 if ( $new_width > $width_orig || $new_height > $height_orig ) |
|
156 { |
|
157 // Too big for our britches here; set it to only convert the file |
|
158 $new_width = $width_orig; |
|
159 $new_height = $height_orig; |
|
160 } |
|
161 } |
|
162 else |
|
163 { |
137 $new_width = $width; |
164 $new_width = $width; |
138 $new_height = round( $width / $ratio ); |
|
139 } |
|
140 else if ( $ratio < 1 ) |
|
141 { |
|
142 // orig. height is greater than width |
|
143 $new_width = round( $height / $ratio ); |
|
144 $new_height = $height; |
165 $new_height = $height; |
145 } |
|
146 else if ( $ratio == 1 ) |
|
147 { |
|
148 $new_width = $width; |
|
149 $new_height = $width; |
|
150 } |
|
151 if ( $new_width > $width_orig || $new_height > $height_orig ) |
|
152 { |
|
153 // Too big for our britches here; set it to only convert the file |
|
154 $new_width = $width_orig; |
|
155 $new_height = $height_orig; |
|
156 } |
166 } |
157 |
167 |
158 $newimage = @imagecreatetruecolor($new_width, $new_height); |
168 $newimage = @imagecreatetruecolor($new_width, $new_height); |
159 if ( !$newimage ) |
169 if ( !$newimage ) |
160 return false; |
170 return false; |