Retain alpha-channel when resizing images (useful for the logo).

This commit is contained in:
Alexandre Rosenfeld
2011-02-21 20:53:55 +00:00
parent 37399eaf07
commit 7284b14e4b

View File

@@ -2284,35 +2284,49 @@ class G
*/
function resizeImage($path, $resWidth, $resHeight, $saveTo=null)
{
try {
list($width, $height) = getimagesize($path);
$percentHeight = $resHeight / $height;
$percentWidth = $resWidth / $width;
$percent = ($percentWidth < $percentHeight) ? $percentWidth : $percentHeight;
$resWidth = $width * $percent;
$resHeight = $height * $percent;
$imageInfo = @getimagesize($path);
// Resample
$image_p = imagecreatetruecolor($resWidth, $resHeight);
//calculating memory needed for image
$imageInfo = GetImageSize($path);
$memoryNeeded = Round( ($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] + Pow(2, 16)) * 1.95) / (1024*1024);
if ( $memoryNeeded < 50 ) $memoryNeeded = 50;
ini_set('memory_limit', intval($memoryNeeded) . 'M');
if (strcasecmp(substr(strtolower($path),-4),'.jpg')===0) $image = imagecreatefromjpeg($path);
if (strcasecmp(substr(strtolower($path),-5),'.jpeg')===0)$image = imagecreatefromjpeg($path);
if (strcasecmp(substr(strtolower($path),-4),'.png')===0) $image = imagecreatefrompng($path);
if (strcasecmp(substr(strtolower($path),-4),'.gif')===0) $image = imagecreatefromgif($path);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $resWidth, $resHeight, $width, $height);
if (!$imageInfo)
throw new Exception("Could not get image information");
// Output
imagejpeg($image_p, $saveTo, 100);
chmod($saveTo, 0666);
}
catch (Exception $oException) {
throw $oException;
}
list($width, $height) = $imageInfo;
$percentHeight = $resHeight / $height;
$percentWidth = $resWidth / $width;
$percent = ($percentWidth < $percentHeight) ? $percentWidth : $percentHeight;
$resWidth = $width * $percent;
$resHeight = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($resWidth, $resHeight);
imagealphablending($image_p, false);
imagesavealpha($image_p, true);
$background = imagecolorallocate($image_p, 0, 0, 0);
ImageColorTransparent($image_p, $background); // make the new temp image all transparent
//Assume 3 channels if we can't find that information
if (!array_key_exists("channels", $imageInfo))
$imageInfo["channels"] = 3;
$memoryNeeded = Round( ($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] + Pow(2, 16)) * 1.95) / (1024*1024);
if ( $memoryNeeded < 80 ) $memoryNeeded = 80;
ini_set('memory_limit', intval($memoryNeeded) . 'M');
$functions = array(
IMAGETYPE_GIF => array('imagecreatefromgif', 'imagegif'),
IMAGETYPE_JPEG => array('imagecreatefromjpeg', 'imagejpeg'),
IMAGETYPE_PNG => array('imagecreatefrompng', 'imagepng'),
);
if (!array_key_exists($imageInfo[2], $functions))
throw new Exception("Image format not supported");
list($inputFn, $outputFn) = $functions[$imageInfo[2]];
$image = $inputFn($path);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $resWidth, $resHeight, $width, $height);
$outputFn($image_p, $saveTo);
chmod($saveTo, 0666);
}
/**