+# The image will retain aspect ratio, and be either $max_width wide or
+# $max_height tall (or, if the aspect is just right, both)
+function resize_image($filename, $max_width = '70', $max_height = '70') {
+ $max_width = format_int_70($max_width);
+ $height_width = format_int_70($height_width);
+
+ imagimagick_mogrify($filename, "-geometry ${max_width}x$max_height");
+}
+
+# Argument: path to image file
+#
+# Return: string in the format WIDTHxHEIGHT, or boolean false
+#
+# Example: image_dimensions('uploads/foo.png'); ==> "124x58"
+function image_dimensions($image) {
+ $identify = path_to('identify');
+ $command = "$identify -format '%wx%h' " . escapeshellarg($image);
+ $dimensions = rtrim(`$command`);
+ if($dimensions == '') {
+ return false;
+ } else {
+ return $dimensions;
+ }
+}
+
+# return an array of the width and height of the image passed.
+# calls die() if this can't be done for any reason.
+function image_w_h_or_die($filename) {
+ $wxh = image_dimensions($filename);
+ if($wxh == false) {
+ die("couldn't git image dimensions of $filename");
+ }
+ $wh = explode('x', $wxh);
+ if(count($wh) != 2) {
+ die("image $filename seems to have " . count($wh) . ' dimensions');
+ }
+ return $wh;
+}
+
+
+# Like save_uploaded_file() (above) except that it converts all images to PNG
+# or JPEG, converts to sRGB colorspace, and optionally scales and/or creates a
+# thumbnail. And, if $path ends with a period, the correct extension will be
+# appended.
+#
+# You are encouraged to use convert_uploaded_image() instead of this function,
+# because it has a more useful return value.
+#
+# If the image_width and image_height parameters are above zero, then the image
+# will be scaled (see below).
+#
+# If the thumb_width and thumb_height parameters are above zero, then a 2nd
+# image will be created and scaled (see below) with the same name, except
+# having "_thumb" added before the extension.
+#
+# Scaling: images are scaled (maintaining the aspect ratio) so they are as big
+# as possible without either dimension being larger than what you specify.
+#
+# This function just returns the name of the main image. To get the dimensions
+# and names, call convert_uploaded_image().
+function save_uploaded_image($key, $path, $image_width = 0, $image_height = 0, $thumbnail_width = 0, $thumbnail_height = 0) {
+ $image_w_h_thumb_w_h = convert_uploaded_image($key, $path, $image_width, $image_height, $thumbnail_width, $thumbnail_height);
+ return ereg_replace(' .*', '', $image_w_h_thumb_w_h);
+}
+
+function ext_to_web_image_ext($in) {
+ if($in == 'png' || $in == 'gif') {
+ return 'png';