includes/functions.php
changeset 898 c75754f5b1da
parent 875 0c3dd4c166c0
child 906 c949e82b8f49
equal deleted inserted replaced
897:f31c252c52c1 898:c75754f5b1da
  3839 function scale_image($in_file, $out_file, $width = 225, $height = 225, $unlink = false)
  3839 function scale_image($in_file, $out_file, $width = 225, $height = 225, $unlink = false)
  3840 {
  3840 {
  3841   global $db, $session, $paths, $template, $plugins; // Common objects
  3841   global $db, $session, $paths, $template, $plugins; // Common objects
  3842   
  3842   
  3843   if ( !is_int($width) || !is_int($height) )
  3843   if ( !is_int($width) || !is_int($height) )
  3844     return false;
  3844     throw new Exception('Invalid height or width.');
  3845   
  3845   
  3846   if ( !file_exists($in_file) )
  3846   if ( !file_exists($in_file) )
  3847     return false;
  3847     throw new Exception('Input file does not exist');
  3848   
  3848   
  3849   $in_file = escapeshellarg($in_file);
  3849   $in_file_sh = escapeshellarg($in_file);
  3850   $out_file = escapeshellarg($out_file);
  3850   $out_file_sh = escapeshellarg($out_file);
  3851   
  3851   
  3852   if ( file_exists($out_file) && !$unlink )
  3852   if ( file_exists($out_file) && !$unlink )
  3853     return false;
  3853     throw new Exception('Refusing to write output file as it already exists and $unlink was not specified.');
  3854   else if ( file_exists($out_file) && $unlink )
  3854   else if ( file_exists($out_file) && $unlink )
  3855     @unlink($out_file);
  3855     @unlink($out_file);
  3856   if ( file_exists($out_file) )
  3856   if ( file_exists($out_file) )
  3857     // couldn't unlink (delete) the output file
  3857     // couldn't unlink (delete) the output file
  3858     return false;
  3858     throw new Exception('Failed to delete existing output file.');
  3859     
  3859     
  3860   $file_ext = substr($in_file, ( strrpos($in_file, '.') + 1));
  3860   $file_ext = strtolower(substr($in_file, ( strrpos($in_file, '.') + 1)));
  3861   switch($file_ext)
  3861   switch($file_ext)
  3862   {
  3862   {
  3863     case 'png':
  3863     case 'png':
  3864       $func = 'imagecreatefrompng';
  3864       $func = 'imagecreatefrompng';
  3865       break;
  3865       break;
  3872       break;
  3872       break;
  3873     case 'xpm':
  3873     case 'xpm':
  3874       $func = 'imagecreatefromxpm';
  3874       $func = 'imagecreatefromxpm';
  3875       break;
  3875       break;
  3876     default:
  3876     default:
  3877       return false;
  3877       throw new Exception('Invalid extension of input file.');
  3878   }
  3878   }
  3879     
  3879     
  3880   $magick_path = getConfig('imagemagick_path');
  3880   $magick_path = getConfig('imagemagick_path');
  3881   $can_use_magick = (
  3881   $can_use_magick = (
  3882       getConfig('enable_imagemagick') == '1' &&
  3882       getConfig('enable_imagemagick') == '1' &&
  3889       function_exists('imagecopyresampled')   &&
  3889       function_exists('imagecopyresampled')   &&
  3890       function_exists($func)
  3890       function_exists($func)
  3891     );
  3891     );
  3892   if ( $can_use_magick )
  3892   if ( $can_use_magick )
  3893   {
  3893   {
  3894     if ( !preg_match('/^([\/A-z0-9_-]+)$/', $magick_path) )
  3894     if ( !preg_match('/^([\/A-z0-9:\. _-]+)$/', $magick_path) )
  3895     {
  3895     {
  3896       die('SECURITY: ImageMagick path is screwy');
  3896       die('SECURITY: ImageMagick path is screwy');
  3897     }
  3897     }
  3898     $cmdline = "$magick_path \"$in_file\" -resize \"{$width}x{$height}>\" \"$out_file\"";
  3898     $cmdline = "$magick_path $in_file_sh -resize \"{$width}x{$height}>\" $out_file_sh";
  3899     system($cmdline, $return);
  3899     system($cmdline, $return);
  3900     if ( !file_exists($out_file) )
  3900     if ( !file_exists($out_file) )
  3901       return false;
  3901       throw new Exception('ImageMagick: did not produce output image file.');
  3902     return true;
  3902     return true;
  3903   }
  3903   }
  3904   else if ( $can_use_gd )
  3904   else if ( $can_use_gd )
  3905   {
  3905   {
  3906     @list($width_orig, $height_orig) = @getimagesize($in_file);
  3906     @list($width_orig, $height_orig) = @getimagesize($in_file);
  3907     if ( !$width_orig || !$height_orig )
  3907     if ( !$width_orig || !$height_orig )
  3908       return false;
  3908       throw new Exception('GD: Could not get height and width of input file.');
  3909     // calculate new width and height
  3909     // calculate new width and height
  3910     
  3910     
  3911     $ratio = $width_orig / $height_orig;
  3911     $ratio = $width_orig / $height_orig;
  3912     if ( $ratio > 1 )
  3912     if ( $ratio > 1 )
  3913     {
  3913     {
  3933       $new_height = $height_orig;
  3933       $new_height = $height_orig;
  3934     }
  3934     }
  3935     
  3935     
  3936     $newimage = @imagecreatetruecolor($new_width, $new_height);
  3936     $newimage = @imagecreatetruecolor($new_width, $new_height);
  3937     if ( !$newimage )
  3937     if ( !$newimage )
  3938       return false;
  3938       throw new Exception('GD: Request to create new truecolor image refused.');
  3939     $oldimage = @$func($in_file);
  3939     $oldimage = @$func($in_file);
  3940     if ( !$oldimage )
  3940     if ( !$oldimage )
  3941       return false;
  3941       throw new Exception('GD: Request to load input image file failed.');
  3942     
  3942     
  3943     // Perform scaling
  3943     // Perform scaling
  3944     imagecopyresampled($newimage, $oldimage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
  3944     imagecopyresampled($newimage, $oldimage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
  3945     
  3945     
  3946     // Get output format
  3946     // Get output format
  3947     $out_ext = substr($out_file, ( strrpos($out_file, '.') + 1));
  3947     $out_ext = strtolower(substr($out_file, ( strrpos($out_file, '.') + 1)));
  3948     switch($out_ext)
  3948     switch($out_ext)
  3949     {
  3949     {
  3950       case 'png':
  3950       case 'png':
  3951         $outfunc = 'imagepng';
  3951         $outfunc = 'imagepng';
  3952         break;
  3952         break;
  3961         $outfunc = 'imagexpm';
  3961         $outfunc = 'imagexpm';
  3962         break;
  3962         break;
  3963       default:
  3963       default:
  3964         imagedestroy($newimage);
  3964         imagedestroy($newimage);
  3965         imagedestroy($oldimage);
  3965         imagedestroy($oldimage);
  3966         return false;
  3966         throw new Exception('GD: Invalid extension of output file.');
  3967     }
  3967     }
  3968     
  3968     
  3969     // Write output
  3969     // Write output
  3970     $outfunc($newimage, $out_file);
  3970     $outfunc($newimage, $out_file);
  3971     
  3971     
  3982   {
  3982   {
  3983     eval($cmd);
  3983     eval($cmd);
  3984   }
  3984   }
  3985   if ( file_exists($out_file) )
  3985   if ( file_exists($out_file) )
  3986     return true;
  3986     return true;
  3987   return false;
  3987   
       
  3988   throw new Exception('Failed to find an appropriate method for scaling.');
  3988 }
  3989 }
  3989 
  3990 
  3990 /**
  3991 /**
  3991  * Determines whether a GIF file is animated or not. Credit goes to ZeBadger from <http://www.php.net/imagecreatefromgif>.
  3992  * Determines whether a GIF file is animated or not. Credit goes to ZeBadger from <http://www.php.net/imagecreatefromgif>.
  3992  * Modified to conform to Enano coding standards.
  3993  * Modified to conform to Enano coding standards.