* * Automatically resize uploaded images using ImageMagick * * Developed and tested using the PmWiki 2.2.0-beta series. * * To install, add the following line to your configuration file : include_once("$FarmD/cookbook/autothumber.php"); * * For more information, please see the online documentation at * http://www.pmwiki.org/wiki/Cookbook/AutoThumber * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License, * Version 2, as published by the Free Software Foundation. * http://www.gnu.org/copyleft/gpl.html */ $RecipeInfo['AutoThumber']['Version'] = '2008-04-05'; $HandleActions['postupload'] = 'AutoThumberHandlePostUpload'; XLSDV( 'en', array( 'ULimagesuccess' => 'image successfully uploaded & resized' ) ); function AutoThumberHandlePostUpload($pagename, $auth = 'upload') { global $ImgExtPattern, $AutoThumberSet, $UploadVerifyFunction, $UploadFileFmt, $EnableUploadVersions, $LastModFile, $Now; SDV( $AutoThumberSet, array( 'default' => array( 'ext'=>'.orig', 'opt'=>'', 'resize-opt'=>'>', 'maxsize'=>0.9 ), 'big' => array( 'ext'=>'', 'w'=>300, 'h'=>600 ), 'small' => array( 'ext'=>'.small', 'w'=>100, 'h'=>100 ) ) ); $uploadfile = $_FILES['uploadfile']; $upname = $_REQUEST['upname']; if ($upname=='') $upname = $uploadfile['name']; $upname = MakeUploadName( $pagename, $upname ); if ( !preg_match( "/^(.*)($ImgExtPattern)$/", $upname, $um ) || !count($AutoThumberSet) ) { $puf = is_callable( $HandleActions['postupload'] ) ? $HandleActions['postupload'] : 'HandlePostUpload'; $puf( $pagename, $auth ); return; } $page = RetrieveAuthPage( $pagename, $auth, true, READPAGE_CURRENT ); if (!$page) Abort("?cannot upload to $pagename"); if (!function_exists($UploadVerifyFunction)) Abort('?no UploadVerifyFunction available'); $sout = shell_exec('convert -version'); if ( strpos($sout,'ImageMagick') === FALSE ) Abort('?no ImageMagick convert command found'); $filedir = FmtPageName( $UploadFileFmt, $pagename ); $defaults = isset($AutoThumberSet['default']) ? $AutoThumberSet['default'] : reset($AutoThumberSet); $filename = $um[1] . @$defaults['ext'] . $um[2]; $filepath = "$filedir/$filename"; $upreport = '{$PageUrl}?action=upload&uprname'; $result = $UploadVerifyFunction( $pagename, $uploadfile, $filepath ); if ( $result != '' ) { if ( $result == 'upresult=exists' ) Redirect( $pagename, "$upreport=$filename&upresult=exists"); else Redirect( $pagename, "$upreport=$upname&$result"); return; } mkdirp($filedir); if ( IsEnabled( $EnableUploadVersions, 0 ) ) @rename( $filepath, "$filepath,$Now" ); if ( !move_uploaded_file( $uploadfile['tmp_name'], $filepath ) ) Abort("?cannot move uploaded file to $filepath"); fixperms($filepath,0444); $orig_geom = FALSE; foreach( $AutoThumberSet as $s ) { $sfn = $um[1] . @$s['ext'] . $um[2]; if ( $sfn == $filename ) continue; $sfp = "$filedir/$sfn"; if ( IsEnabled( $EnableUploadVersions, 0 ) ) @rename( $sfp, "$sfp,$Now" ); if ( empty($s['w']) && empty($s['h']) && empty($s['opt']) ) { if ( !copy( $filepath, $sfp ) ) Abort("?cannot copy image to $sfp"); } else { $ssin = ( !empty($s['w']) && !empty($s['h']) ) ? '-size '.(2*$s['w']).'x'.(2*$s['h']) : ''; $sopt = isset($s['opt']) ? $s['opt'] : @$defaults['opt']; if ( !empty($s['w']) ) $sg = $s['w']; if ( !empty($s['h']) ) $sg .= 'x'.$s['h']; $sg .= isset($s['resize-opt']) ? $s['resize-opt'] : @$defaults['resize-opt']; if ($sg!='') $sg = "-thumbnail '$sg'"; $sout = shell_exec("convert $ssin '$filepath' $sopt $sg '$sfp'"); if ($sout!='') Abort("?ImageMagick convert says: $sout"); $smax = isset($s['maxsize']) ? $s['maxsize'] : @$defaults['maxsize']; if ( $smax && ( filesize($sfp) > $smax * filesize($filepath) ) ) { if (!$orig_geom) $orig_geom = shell_exec("identify -format '%w %h' '$filepath'"); $sout = shell_exec("identify -format '%w %h' '$sfp'"); if ( ( $sout == $orig_geom ) && preg_match('/^\d+ \d+$/',$sout) ) if ( !copy( $filepath, $sfp ) ) Abort("?cannot copy original image to $sfp"); } } fixperms($sfp,0444); } if ($LastModFile) { touch($LastModFile); fixperms($LastModFile); } Redirect( $pagename, "$upreport=$upname&upresult=imagesuccess" ); }