mirror of
https://github.com/e107inc/e107.git
synced 2025-07-27 18:00:30 +02:00
Bugtracker #4721 - type of resized images matches file extension
This commit is contained in:
@@ -11,8 +11,8 @@
|
||||
| GNU General Public License (http://gnu.org).
|
||||
|
|
||||
| $Source: /cvs_backup/e107_0.8/e107_handlers/resize_handler.php,v $
|
||||
| $Revision: 1.9 $
|
||||
| $Date: 2008-10-03 20:28:54 $
|
||||
| $Revision: 1.10 $
|
||||
| $Date: 2009-04-23 20:23:42 $
|
||||
| $Author: e107steved $
|
||||
|
|
||||
|
|
||||
@@ -20,6 +20,23 @@
|
||||
*/
|
||||
if (!defined('e107_INIT')) { exit; }
|
||||
|
||||
// Given an image file name, return the mime type string. Returns FALSE if invalid
|
||||
function mimeFromFilename($fileName)
|
||||
{
|
||||
$fileExt = strtolower(substr(strrchr($fileName, "."), 1));
|
||||
$mimeTypes = array(
|
||||
'jpg' => 'jpeg',
|
||||
'gif' => 'gif',
|
||||
'png' => 'png',
|
||||
'jpeg' => 'jpeg',
|
||||
'pjpeg' => 'jpeg',
|
||||
'bmp' => 'bmp'
|
||||
);
|
||||
if (!isset($mimeTypes[$fileExt])) { return FALSE; } // only allow image files }
|
||||
return "Content-type: image/".$mimeTypes[$fileExt];
|
||||
}
|
||||
|
||||
|
||||
function resize_image($source_file, $destination_file, $type = "upload", $model = "")
|
||||
{
|
||||
// $destination_file - 'stdout' sends direct to browser. Otherwise treated as file name
|
||||
@@ -96,17 +113,8 @@ function resize_image($source_file, $destination_file, $type = "upload", $model
|
||||
case 'noscale' : // No scaling of small images- just want destination to be the same as source
|
||||
if ($destination_file == 'stdout')
|
||||
{
|
||||
$fileExt = strtolower(substr(strrchr($source_file, "."), 1));
|
||||
$mimeTypes = array(
|
||||
'jpg' => 'jpeg',
|
||||
'gif' => 'gif',
|
||||
'png' => 'png',
|
||||
'jpeg' => 'jpeg',
|
||||
'pjpeg' => 'jpeg',
|
||||
'bmp' => 'bmp'
|
||||
);
|
||||
if (!isset($mimeTypes[$fileExt])) { return FALSE; } // only allow image files }
|
||||
header("Content-type: image/".$mimeTypes[$fileExt]);
|
||||
if (($result = mimeFromFilename($source_file)) === FALSE) { return FALSE; }
|
||||
header($result);
|
||||
if (@readfile($source_file) === FALSE) { return FALSE; }
|
||||
}
|
||||
else
|
||||
@@ -159,13 +167,16 @@ function resize_image($source_file, $destination_file, $type = "upload", $model
|
||||
{
|
||||
case IMAGETYPE_PNG : // 3 - PNG
|
||||
$src_img = @imagecreatefrompng($source_file);
|
||||
$fileExt = 'png';
|
||||
break;
|
||||
case IMAGETYPE_GIF : // 1 - GIF
|
||||
if (!function_exists('imagecreatefromgif')) return FALSE; // Some versions of GD library don't support GIF
|
||||
$src_img = @imagecreatefromgif($source_file);
|
||||
$fileExt = 'gif';
|
||||
break;
|
||||
case IMAGETYPE_JPEG : // 2 - Jpeg
|
||||
$src_img = @imagecreatefromjpeg($source_file);
|
||||
$fileExt = 'jpg';
|
||||
break;
|
||||
default :
|
||||
return FALSE; // Unsupported image type
|
||||
@@ -187,24 +198,56 @@ function resize_image($source_file, $destination_file, $type = "upload", $model
|
||||
}
|
||||
if ($returnError)
|
||||
{
|
||||
echo "Resizing error: {$returnError}<br />";
|
||||
echo "Resizing error (1): {$returnError}<br />";
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Now output or save the resized file
|
||||
|
||||
$destName = $destination_file;
|
||||
if ($destination_file == "stdout")
|
||||
{
|
||||
header("Content-type: image/jpeg");
|
||||
if (!imagejpeg($dst_img, '', $im_quality)) $returnError = -1;
|
||||
$destName = '';
|
||||
if (($result = mimeFromFilename($source_file)) === FALSE)
|
||||
{
|
||||
$returnError = -6;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!imagejpeg($dst_img, $destination_file, $im_quality)) { $returnError = -1; }
|
||||
header($result);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$fileExt = strtolower(substr(strrchr($destination_file, "."), 1));
|
||||
}
|
||||
|
||||
if ($returnError == 0)
|
||||
{ // We can output the image, or save it to a file
|
||||
switch ($fileExt)
|
||||
{
|
||||
case 'png' :
|
||||
if (!imagepng($dst_img, $destName, 6)) { $returnError = -1; } // Fix the quality for now
|
||||
break;
|
||||
case 'gif' :
|
||||
if (!imagegif($dst_img, $destName)) { $returnError = -1; }
|
||||
$outputFunc = 'imagegif';
|
||||
break;
|
||||
case 'jpg' :
|
||||
case 'jpeg' :
|
||||
if (!imagejpeg($dst_img, $destName, $im_quality)) { $returnError = -1; }
|
||||
$outputFunc = 'imagejpeg';
|
||||
break;
|
||||
default :
|
||||
$returnError = -7; // Invalid output extensiot
|
||||
}
|
||||
}
|
||||
|
||||
if (!imagedestroy($src_img)) { $returnError = -2; }
|
||||
if (!imagedestroy($dst_img)) { $returnError = -3; }
|
||||
if ($returnError)
|
||||
{
|
||||
echo "Resizing error: {$returnError}<br />";
|
||||
echo "Resizing error (2): {$returnError}<br />";
|
||||
return FALSE;
|
||||
}
|
||||
break;
|
||||
@@ -216,6 +259,10 @@ function resize_image($source_file, $destination_file, $type = "upload", $model
|
||||
if ($destination_file == "stdout") return TRUE; // Can't do anything more if file sent to stdout - assume success
|
||||
|
||||
@chmod($destination_file, 0644);
|
||||
if ($pref['image_owner'])
|
||||
{
|
||||
@chown($destination_file, $pref['image_owner']);
|
||||
}
|
||||
|
||||
$image_stats = getimagesize($destination_file);
|
||||
if ($image_stats == null)
|
||||
|
Reference in New Issue
Block a user