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; |
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; |