includes/functions.php
changeset 230 3daa715e0f69
parent 191 3dbe848431b0
child 231 b11a2f1353c0
child 232 4cc0736ca168
equal deleted inserted replaced
224:6a4573507ff8 230:3daa715e0f69
  3180   if ( !isset($cron_tasks[$hour_interval]) )
  3180   if ( !isset($cron_tasks[$hour_interval]) )
  3181     $cron_tasks[$hour_interval] = array();
  3181     $cron_tasks[$hour_interval] = array();
  3182   $cron_tasks[$hour_interval][] = $func;
  3182   $cron_tasks[$hour_interval][] = $func;
  3183 }
  3183 }
  3184 
  3184 
       
  3185 /**
       
  3186  * Scales an image to the specified width and height, and writes the output to the specified
       
  3187  * file. Will use ImageMagick if present, but if not will attempt to scale with GD. This will
       
  3188  * always scale images proportionally.
       
  3189  * @param string Path to image file
       
  3190  * @param string Path to output file
       
  3191  * @param int Image width, in pixels
       
  3192  * @param int Image height, in pixels
       
  3193  * @param bool If true, the output file will be deleted if it exists before it is written
       
  3194  * @return bool True on success, false on failure
       
  3195  */
       
  3196 
       
  3197 function scale_image($in_file, $out_file, $width = 225, $height = 225, $unlink = false)
       
  3198 {
       
  3199   global $db, $session, $paths, $template, $plugins; // Common objects
       
  3200   
       
  3201   if ( !is_int($width) || !is_int($height) )
       
  3202     return false;
       
  3203   
       
  3204   if ( !file_exists($in_file) )
       
  3205     return false;
       
  3206   
       
  3207   if ( preg_match('/["\'\/\\]/', $in_file) || preg_match('/["\'\/\\]/', $out_file) )
       
  3208     die('SECURITY: scale_image(): infile or outfile path is screwy');
       
  3209   
       
  3210   if ( file_exists($out_file) && !$unlink )
       
  3211     return false;
       
  3212   else if ( file_exists($out_file) && $unlink )
       
  3213     @unlink($out_file);
       
  3214   if ( file_exists($out_file) )
       
  3215     // couldn't unlink (delete) the output file
       
  3216     return false;
       
  3217     
       
  3218   $file_ext = substr($in_file, ( strrpos($in_file, '.') + 1));
       
  3219   switch($file_ext)
       
  3220   {
       
  3221     case 'png':
       
  3222       $func = 'imagecreatefrompng';
       
  3223       break;
       
  3224     case 'jpg':
       
  3225     case 'jpeg':
       
  3226       $func = 'imagecreatefromjpeg';
       
  3227       break;
       
  3228     case 'gif':
       
  3229       $func = 'imagecreatefromgif';
       
  3230       break;
       
  3231     case 'xpm':
       
  3232       $func = 'imagecreatefromxpm';
       
  3233       break;
       
  3234     default:
       
  3235       return false;
       
  3236   }
       
  3237     
       
  3238   $magick_path = getConfig('imagemagick_path');
       
  3239   $can_use_magick = (
       
  3240       getConfig('enable_imagemagick') == '1' &&
       
  3241       file_exists($magick_path)              &&
       
  3242       is_executable($magick_path)
       
  3243     );
       
  3244   $can_use_gd = (
       
  3245       function_exists('getimagesize')         &&
       
  3246       function_exists('imagecreatetruecolor') &&
       
  3247       function_exists('imagecopyresampled')   &&
       
  3248       function_exists($func)
       
  3249     );
       
  3250   if ( $can_use_magick )
       
  3251   {
       
  3252     if ( !preg_match('/^([\/A-z0-9_-]+)$/', $magick_path) )
       
  3253     {
       
  3254       die('SECURITY: ImageMagick path is screwy');
       
  3255     }
       
  3256     $cmdline = "$magick_path \"$in_file\" -resize \"{$width}x{$height}>\" \"$out_file\"";
       
  3257     system($cmdline, $return);
       
  3258     if ( !file_exists($out_file) )
       
  3259       return false;
       
  3260     return true;
       
  3261   }
       
  3262   else if ( $can_use_gd )
       
  3263   {
       
  3264     @list($width_orig, $height_orig) = @getimagesize($in_file);
       
  3265     if ( !$width_orig || !$height_orig )
       
  3266       return false;
       
  3267     // calculate new width and height
       
  3268     
       
  3269     $ratio = $width_orig / $height_orig;
       
  3270     if ( $ratio > 1 )
       
  3271     {
       
  3272       // orig. width is greater that height
       
  3273       $new_width = $width;
       
  3274       $new_height = round( $width / $ratio );
       
  3275     }
       
  3276     else if ( $ratio < 1 )
       
  3277     {
       
  3278       // orig. height is greater than width
       
  3279       $new_width = round( $height / $ratio );
       
  3280       $new_height = $height;
       
  3281     }
       
  3282     else if ( $ratio == 1 )
       
  3283     {
       
  3284       $new_width = $width;
       
  3285       $new_height = $width;
       
  3286     }
       
  3287     if ( $new_width > $width_orig || $new_height > $height_orig )
       
  3288     {
       
  3289       // Too big for our britches here; set it to only convert the file
       
  3290       $new_width = $width_orig;
       
  3291       $new_height = $height_orig;
       
  3292     }
       
  3293     
       
  3294     $newimage = @imagecreatetruecolor($new_width, $new_height);
       
  3295     if ( !$newimage )
       
  3296       return false;
       
  3297     $oldimage = @$func($in_file);
       
  3298     if ( !$oldimage )
       
  3299       return false;
       
  3300     
       
  3301     // Perform scaling
       
  3302     imagecopyresampled($newimage, $oldimage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
       
  3303     
       
  3304     // Get output format
       
  3305     $out_ext = substr($out_file, ( strrpos($out_file, '.') + 1));
       
  3306     switch($out_ext)
       
  3307     {
       
  3308       case 'png':
       
  3309         $outfunc = 'imagepng';
       
  3310         break;
       
  3311       case 'jpg':
       
  3312       case 'jpeg':
       
  3313         $outfunc = 'imagejpeg';
       
  3314         break;
       
  3315       case 'gif':
       
  3316         $outfunc = 'imagegif';
       
  3317         break;
       
  3318       case 'xpm':
       
  3319         $outfunc = 'imagexpm';
       
  3320         break;
       
  3321       default:
       
  3322         imagedestroy($newimage);
       
  3323         imagedestroy($oldimage);
       
  3324         return false;
       
  3325     }
       
  3326     
       
  3327     // Write output
       
  3328     $outfunc($newimage, $out_file);
       
  3329     
       
  3330     // clean up
       
  3331     imagedestroy($newimage);
       
  3332     imagedestroy($oldimage);
       
  3333     
       
  3334     // done!
       
  3335     return true;
       
  3336   }
       
  3337   // Neither scaling method worked; we'll let plugins try to scale it, and then if the file still doesn't exist, die
       
  3338   $code = $plugins->setHook('scale_image_failure');
       
  3339   foreach ( $code as $cmd )
       
  3340   {
       
  3341     eval($cmd);
       
  3342   }
       
  3343   if ( file_exists($out_file) )
       
  3344     return true;
       
  3345   return false;
       
  3346 }
       
  3347 
  3185 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  3348 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  3186 
  3349 
  3187 ?>
  3350 ?>