From: Jason Woofenden Date: Mon, 24 Sep 2007 14:41:20 +0000 (-0400) Subject: fixed make_thumbnail() and image support in metaform. added image_dimensions(), refac... X-Git-Url: https://jasonwoof.com/gitweb/?p=wfpl.git;a=commitdiff_plain;h=fa9853503d584b6373257df6b66c79c8ec5290df fixed make_thumbnail() and image support in metaform. added image_dimensions(), refactored path_to_convert() into general path_to('whatever') --- diff --git a/metaform/template.php b/metaform/template.php index 14f88f1..a64fa04 100644 --- a/metaform/template.php +++ b/metaform/template.php @@ -43,7 +43,7 @@ require_once('code/wfpl/upload.php'); function ~form_name~_get_fields() { $~name~ = format_~format~($_REQUEST['~name~'], '~name~'); - if($_FILE['~name~'] && $_FILE['~name~']['error'] == 0) { + if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) { $~name~ = substr(save_uploaded_image('~name~', $GLOBALS['upload_directory']), strlen($GLOBALS['upload_directory'])); } else { $~name~ = format_filename($_REQUEST['old_~name~']); diff --git a/upload.php b/upload.php index 05e1866..e349cf0 100644 --- a/upload.php +++ b/upload.php @@ -162,22 +162,38 @@ function save_uploaded_file($key, $path) { return $filename; } -function path_to_convert() { - if(!isset($GLOBALS['path_to_convert'])) { - $GLOBALS['path_to_convert'] = _path_to_convert(); +# this function exists to deal with cases where binaries are installed in very +# standard places (like /usr/bin or /usr/local bin) and PHP's PATH environment +# variable is not set appropriately. +function path_to($prog, $or_die = true) { + $prog = ereg_replace('[^a-zA-Z0-9_.-]', '', $prog); + $prog = ereg_replace('^[-.]*', '', $prog); + if($prog == '') { + die('Invalid argument to path_to()'); } - return $GLOBALS['path_to_convert']; -} + if(!isset($GLOBALS["path_to_$prog"])) { + $ret = _path_to($prog, $or_die); + if($ret == false) { + return false; + } + $GLOBALS["path_to_$prog"] = $ret; + } -function _path_to_convert() { + return $GLOBALS["path_to_$prog"]; +} + +function _path_to($prog, $or_die) { # relies on PHP's short-circuit mechanism - if(file_exists($convert = '/usr/local/bin/convert') || - file_exists($convert = '/usr/bin/convert') || + if(file_exists($convert = "/usr/local/bin/$prog") || + file_exists($convert = "/usr/bin/$prog") || ($convert = `which convert` != '' && file_exists($convert))) { return $convert; } else { - die("can't find imagemagick's 'convert' program"); + if($or_die) { + die("Failed to locate '$prog' executable."); + } + return false; } } @@ -193,7 +209,7 @@ function gif_to_png($filename, $new_filename = 'just change extension') { $new_filename .= '.png'; } - $convert = path_to_convert(); + $convert = path_to('convert'); $command = "$convert " . escapeshellarg($filename) . ' ' . escapeshellarg($new_filename); @@ -213,12 +229,16 @@ function gif_to_png($filename, $new_filename = 'just change extension') { # Thumbnail will retain aspect ratio, and be either $max_width wide or # $max_height tall (or, if the aspect is just right, both) function make_thumbnail($filename, $max_width = '70', $max_height = '70') { - $thumb = ereg_replace('[.]([a-z]+)$', "_thumb.\\1", $filename); - if($thumb == $filename) { + $last_dot = strrpos($filename, '.'); + if($last_dot === false) { die("couldn't make thumbnail because filename has no extension."); } - $convert = path_to_convert(); + $thumb = substr($filename, 0, $last_dot); + $thumb .= '_thumb'; + $thumb .= substr($filename, $last_dot); + + $convert = path_to('convert'); # can't be too careful $max_width = ereg_replace('[^0-9]', '', $max_width); @@ -234,12 +254,28 @@ function make_thumbnail($filename, $max_width = '70', $max_height = '70') { exec($command, $dummy, $ret); if($ret != 0) { - die("Thumbnail creatin failed. convert did exit($ret)"); + die("Thumbnail creation failed. Convert called exit($ret)"); } return $thumb; } +# 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 = substr(`$command`, 0, -1); # substr() to remove newline + if($dimensions == '') { + return false; + } else { + return $dimensions; + } +} + # like save_uploaded_file() (above) except it converts gifs to pngs. # # FIXME: if a filename is passed in the end of path, we should check if the file type matches, and if not run convert.