JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
no dashes in filenames, improved image uploading, make_thumbnail()
authorJason Woofenden <jason183@herkamire.com>
Thu, 3 May 2007 13:37:18 +0000 (09:37 -0400)
committerJason Woofenden <jason183@herkamire.com>
Thu, 3 May 2007 13:37:18 +0000 (09:37 -0400)
format.php
metaform.php
metaform/template.html
metaform/template.php
upload.php

index 1bac609..a513722 100644 (file)
@@ -32,8 +32,8 @@ function format_zip($str) {
 
 function format_filename($str) {
        $str = strtolower($str);
-       $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
-       return ereg_replace('^[0-9.-]*', '', $str);
+       $str = ereg_replace('[^a-z0-9_.]', '_', $str);
+       return ereg_replace('^[.]*', '', $str);
 }
 
 function format_varname($str) {
index 5ba0f44..88dd437 100644 (file)
@@ -224,8 +224,8 @@ function make_php() {
                                }
                        } else {
                                $tem->sub('formats');
-                               $tem->sub('tem_sets');
                        }
+                       $tem->sub('tem_sets');
                        if(!$always_field and $input != 'checkbox' and $input != 'radio') {
                                $always_field = $name;
                        }
index 531f416..bd641dd 100644 (file)
@@ -4,6 +4,7 @@
 <head>
   <title>~form_name~ entry</title>
   <style type="text/css"><!--
+td.field { vertical-align: bottom; }
 td.caption { text-align: right; vertical-align: top; font-weight: bold; }
 td.errorcaption { text-align: right; vertical-align: top; font-weight: bold; color: red; }
 div.error { border: 2px solid red; padding: 13px; margin: 20px; background: #ffdddd; }
@@ -19,7 +20,7 @@ div.error { border: 2px solid red; padding: 13px; margin: 20px; background: #ffd
   <form action="~form_name~.html~enctype_attr~" method="post"><!--~~editing start~~--><input type="hidden" name="~form_name~_edit_id" value="~~~form_name~_edit_id.attr~~" /><!--~~end~~--><!--~uploads start~--><input type="hidden" name="MAX_FILE_SIZE" value="~~upload_max_filesize~~" /><!--~end~-->
     <table cellspacing="0" cellpadding="4" border="0" summary=""><!--~row start~-->
 
-      <tr><!--~image start~--><td class="caption">~caption.html~: </td><td><input type="file" name="~name~" /></td><!--~end~--><!--~textbox start~--><td class="caption">~caption.html~: </td><td><input type="text" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~password start~--><td class="caption">~caption.html~: </td><td><input type="password" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~textarea start~--><td class="caption">~caption.html~: </td><td><textarea rows="20" cols="50" name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~radio start~--><td class="caption">~caption.html~: </td><td><input type="radio" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~checkbox start~--><td class="caption">~caption.html~: </td><td><input type="checkbox" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~submit start~--><td class="submit_row" colspan="2"><input type="submit" name="~name~" value="~caption.attr~" /></td><!--~end~--></tr><!--~end~-->
+      <tr><!--~image start~--><td class="caption">~caption.html~: </td><td class="field"><input type="file" name="~name~" /><input type="hidden" name="old_~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~textbox start~--><td class="caption">~caption.html~: </td><td class="field"><input type="text" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~password start~--><td class="caption">~caption.html~: </td><td class="field"><input type="password" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~textarea start~--><td class="caption">~caption.html~: </td><td><textarea rows="20" cols="50" name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~radio start~--><td class="caption">~caption.html~: </td><td class="field"><input type="radio" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~checkbox start~--><td class="caption">~caption.html~: </td><td class="field"><input type="checkbox" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~submit start~--><td class="submit_row" colspan="2"><input type="submit" name="~name~" value="~caption.attr~" /></td><!--~end~--></tr><!--~end~-->
 
     </table>
   </form>
index 14836c7..754b710 100644 (file)
@@ -34,7 +34,11 @@ require_once('code/wfpl/upload.php');<!--~end~-->
 function ~form_name~_get_fields() {
        <!--~formats start~-->
        $~name~ = format_~format~($_REQUEST['~name~']);<!--~end~--><!--~image_upload start~-->
-       $~name~ = save_uploaded_image('~name~', $GLOBALS['upload_directory']);<!--~end~-->
+       if($_FILE['~name~'] && $_FILE['~name~']['error'] == 0) {
+               $~name~ = substr(save_uploaded_image('~name~', $GLOBALS['upload_directory']), strlen($GLOBALS['upload_directory']));
+       } else {
+               $~name~ = format_filename($_REQUEST['old_~name~']);
+       }<!--~end~-->
        <!--~tem_sets start~-->
        tem_set('~name~', $~name~);<!--~end~-->
 
index 143482c..46f8b3c 100644 (file)
@@ -60,7 +60,6 @@
 # the type you specified, it will convert the image for you.
 
 
-
 $GLOBALS['mime_to_ext'] = array(
        'text/plain' => 'txt',
        'text/html'  => 'html',
@@ -157,12 +156,31 @@ function save_uploaded_file($key, $path) {
        }
 
        if(!move_uploaded_file($_FILES[$key]['tmp_name'], $filename)) {
-               die('file upload failed');
+               return false;
        }
 
        return $filename;
 }
 
+function path_to_convert() {
+       if(!isset($GLOBALS['path_to_convert'])) {
+               $convert = '/usr/local/bin/convert';
+               if(!file_exists($convert)) {
+                       $convert = '/usr/bin/convert';
+               }
+               if(!file_exists($convert)) {
+                       $convert = `which convert`;
+               }
+               if($convert == '' || !file_exists($convert)) {
+                       die("can't find imagemagick's 'convert' program");
+               }
+
+               $GLOBALS['path_to_convert'] = $convert;
+       }
+
+       return $GLOBALS['path_to_convert'];
+}
+               
 
 # returns new filename with .png extension
 function gif_to_png($filename, $new_filename = 'just change extension') {
@@ -175,17 +193,8 @@ function gif_to_png($filename, $new_filename = 'just change extension') {
                $new_filename .= '.png';
        }
 
-       $convert = '/usr/local/bin/convert';
-       if(!file_exists($convert)) {
-               $convert = '/usr/bin/convert';
-       }
-       if(!file_exists($convert)) {
-               $convert = `which convert`;
-       }
-       if($convert == '' || !file_exists($convert)) {
-               die("can't find imagemagick's 'convert' program");
-       }
-               
+       $convert = path_to_convert();
+
        $command = "$convert " . escapeshellarg($filename) . ' ' . escapeshellarg($new_filename);
 
        exec($command, $dummy, $ret);
@@ -196,6 +205,41 @@ function gif_to_png($filename, $new_filename = 'just change extension') {
        return $new_filename;
 }
 
+# make a thumbnail image.
+#
+# Thumbnail will have the same filename, except "_thumb" will be added right
+# before the dot preceding the extension. so foo.png yields foo_thumb.png
+#
+# 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) {
+               die("couldn't make thumbnail because filename has no extension.");
+       }
+
+       $convert = path_to_convert();
+
+       # can't be too careful
+       $max_width = ereg_replace('[^0-9]', '', $max_width);
+       if($max_width == '') {
+               $max_width = '70';
+       }
+       $max_height = ereg_replace('[^0-9]', '', $max_height);
+       if($max_height == '') {
+               $max_height = '70';
+       }
+       
+       $command = "$convert -geometry ${max_width}x$max_height " . escapeshellarg($filename) . ' ' . escapeshellarg($thumb);
+
+       exec($command, $dummy, $ret);
+       if($ret != 0) {
+               die("Thumbnail creatin failed. convert did exit($ret)");
+       }
+
+       return $thumb;
+}
+
 # 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.