includes/functions.php
changeset 231 b11a2f1353c0
parent 228 b0a4d179be85
parent 230 3daa715e0f69
child 238 a78537db2850
equal deleted inserted replaced
229:97ae8e9d5e29 231:b11a2f1353c0
  3221     return false;
  3221     return false;
  3222   }
  3222   }
  3223   return true;
  3223   return true;
  3224 }
  3224 }
  3225 
  3225 
       
  3226 /**
       
  3227  * Scales an image to the specified width and height, and writes the output to the specified
       
  3228  * file. Will use ImageMagick if present, but if not will attempt to scale with GD. This will
       
  3229  * always scale images proportionally.
       
  3230  * @param string Path to image file
       
  3231  * @param string Path to output file
       
  3232  * @param int Image width, in pixels
       
  3233  * @param int Image height, in pixels
       
  3234  * @param bool If true, the output file will be deleted if it exists before it is written
       
  3235  * @return bool True on success, false on failure
       
  3236  */
       
  3237 
       
  3238 function scale_image($in_file, $out_file, $width = 225, $height = 225, $unlink = false)
       
  3239 {
       
  3240   global $db, $session, $paths, $template, $plugins; // Common objects
       
  3241   
       
  3242   if ( !is_int($width) || !is_int($height) )
       
  3243     return false;
       
  3244   
       
  3245   if ( !file_exists($in_file) )
       
  3246     return false;
       
  3247   
       
  3248   if ( preg_match('/["\'\/\\]/', $in_file) || preg_match('/["\'\/\\]/', $out_file) )
       
  3249     die('SECURITY: scale_image(): infile or outfile path is screwy');
       
  3250   
       
  3251   if ( file_exists($out_file) && !$unlink )
       
  3252     return false;
       
  3253   else if ( file_exists($out_file) && $unlink )
       
  3254     @unlink($out_file);
       
  3255   if ( file_exists($out_file) )
       
  3256     // couldn't unlink (delete) the output file
       
  3257     return false;
       
  3258     
       
  3259   $file_ext = substr($in_file, ( strrpos($in_file, '.') + 1));
       
  3260   switch($file_ext)
       
  3261   {
       
  3262     case 'png':
       
  3263       $func = 'imagecreatefrompng';
       
  3264       break;
       
  3265     case 'jpg':
       
  3266     case 'jpeg':
       
  3267       $func = 'imagecreatefromjpeg';
       
  3268       break;
       
  3269     case 'gif':
       
  3270       $func = 'imagecreatefromgif';
       
  3271       break;
       
  3272     case 'xpm':
       
  3273       $func = 'imagecreatefromxpm';
       
  3274       break;
       
  3275     default:
       
  3276       return false;
       
  3277   }
       
  3278     
       
  3279   $magick_path = getConfig('imagemagick_path');
       
  3280   $can_use_magick = (
       
  3281       getConfig('enable_imagemagick') == '1' &&
       
  3282       file_exists($magick_path)              &&
       
  3283       is_executable($magick_path)
       
  3284     );
       
  3285   $can_use_gd = (
       
  3286       function_exists('getimagesize')         &&
       
  3287       function_exists('imagecreatetruecolor') &&
       
  3288       function_exists('imagecopyresampled')   &&
       
  3289       function_exists($func)
       
  3290     );
       
  3291   if ( $can_use_magick )
       
  3292   {
       
  3293     if ( !preg_match('/^([\/A-z0-9_-]+)$/', $magick_path) )
       
  3294     {
       
  3295       die('SECURITY: ImageMagick path is screwy');
       
  3296     }
       
  3297     $cmdline = "$magick_path \"$in_file\" -resize \"{$width}x{$height}>\" \"$out_file\"";
       
  3298     system($cmdline, $return);
       
  3299     if ( !file_exists($out_file) )
       
  3300       return false;
       
  3301     return true;
       
  3302   }
       
  3303   else if ( $can_use_gd )
       
  3304   {
       
  3305     @list($width_orig, $height_orig) = @getimagesize($in_file);
       
  3306     if ( !$width_orig || !$height_orig )
       
  3307       return false;
       
  3308     // calculate new width and height
       
  3309     
       
  3310     $ratio = $width_orig / $height_orig;
       
  3311     if ( $ratio > 1 )
       
  3312     {
       
  3313       // orig. width is greater that height
       
  3314       $new_width = $width;
       
  3315       $new_height = round( $width / $ratio );
       
  3316     }
       
  3317     else if ( $ratio < 1 )
       
  3318     {
       
  3319       // orig. height is greater than width
       
  3320       $new_width = round( $height / $ratio );
       
  3321       $new_height = $height;
       
  3322     }
       
  3323     else if ( $ratio == 1 )
       
  3324     {
       
  3325       $new_width = $width;
       
  3326       $new_height = $width;
       
  3327     }
       
  3328     if ( $new_width > $width_orig || $new_height > $height_orig )
       
  3329     {
       
  3330       // Too big for our britches here; set it to only convert the file
       
  3331       $new_width = $width_orig;
       
  3332       $new_height = $height_orig;
       
  3333     }
       
  3334     
       
  3335     $newimage = @imagecreatetruecolor($new_width, $new_height);
       
  3336     if ( !$newimage )
       
  3337       return false;
       
  3338     $oldimage = @$func($in_file);
       
  3339     if ( !$oldimage )
       
  3340       return false;
       
  3341     
       
  3342     // Perform scaling
       
  3343     imagecopyresampled($newimage, $oldimage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
       
  3344     
       
  3345     // Get output format
       
  3346     $out_ext = substr($out_file, ( strrpos($out_file, '.') + 1));
       
  3347     switch($out_ext)
       
  3348     {
       
  3349       case 'png':
       
  3350         $outfunc = 'imagepng';
       
  3351         break;
       
  3352       case 'jpg':
       
  3353       case 'jpeg':
       
  3354         $outfunc = 'imagejpeg';
       
  3355         break;
       
  3356       case 'gif':
       
  3357         $outfunc = 'imagegif';
       
  3358         break;
       
  3359       case 'xpm':
       
  3360         $outfunc = 'imagexpm';
       
  3361         break;
       
  3362       default:
       
  3363         imagedestroy($newimage);
       
  3364         imagedestroy($oldimage);
       
  3365         return false;
       
  3366     }
       
  3367     
       
  3368     // Write output
       
  3369     $outfunc($newimage, $out_file);
       
  3370     
       
  3371     // clean up
       
  3372     imagedestroy($newimage);
       
  3373     imagedestroy($oldimage);
       
  3374     
       
  3375     // done!
       
  3376     return true;
       
  3377   }
       
  3378   // Neither scaling method worked; we'll let plugins try to scale it, and then if the file still doesn't exist, die
       
  3379   $code = $plugins->setHook('scale_image_failure');
       
  3380   foreach ( $code as $cmd )
       
  3381   {
       
  3382     eval($cmd);
       
  3383   }
       
  3384   if ( file_exists($out_file) )
       
  3385     return true;
       
  3386   return false;
       
  3387 }
       
  3388 
  3226 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  3389 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  3227 
  3390 
  3228 ?>
  3391 ?>