+# this function is just like save_uploaded_image() above except that the return
+# value is a string like "filename width height" or (if you specified both
+# thumbnail dimensions) "image_filename image_width image_height thumb_filename
+# thumb_width thumb_height"
+#
+#
+# examples:
+# convert_uploaded_image('image', 'uploads/', 500, 500);
+# might return: "uploads/foo.jpg 500 400"
+# convert_uploaded_image('image', 'uploads/', 500, 500, 70, 70);
+# might return: "uploads/foo.jpg 500 400 uploads/foo_thumb.jpg 70 56"
+function convert_uploaded_image($key, $path, $image_width = 0, $image_height = 0, $thumb_width = 0, $thumb_height = 0) {
+ $ret = '';
+ $tmp_filename = save_uploaded_file($key, $path . '__.');
+ $ext_rpos = strrpos($tmp_filename, '.');
+ if($ext_rpos === false) {
+ die('save_uploaded_file() gave us a filename with no extension.');
+ }
+ $tmp_base = substr($tmp_filename, 0, $ext_rpos);
+ $tmp_ext = substr($tmp_filename, $ext_rpos + 1);
+ if(substr($path, -1) == '/') {
+ $filename = $path . substr($tmp_base, strlen($path) + 2);
+ $filename .= '.' . ext_to_web_image_ext($tmp_ext);
+ } elseif(substr($path, -1) == '.') {
+ $filename = $path . ext_to_web_image_ext($tmp_ext);
+ } else {
+ $filename = $path;
+ }
+
+ $convert_params = '-colorspace RGB';
+ if($image_width > 0 && $image_height > 0) {
+ $convert_params .= " -geometry ${image_width}x$image_height";
+ }
+ imagemagick_convert($tmp_filename, $filename, $convert_params);
+ unlink($tmp_filename);
+ list($w, $h) = image_w_h_or_die($filename);
+ $ret = "$filename $w $h";
+ if($thumb_width > 0 && $thumb_height > 0) {
+ $thumb_name = make_thumbnail($filename, $thumb_width, $thumb_height);
+ list($w, $h) = image_w_h_or_die($thumb_name);
+ $ret .= " $thumb_name $w $h";
+ }
+ return $ret;
+}