Retain alpha-channel when resizing images (useful for the logo).
This commit is contained in:
@@ -2284,8 +2284,12 @@ class G
|
||||
*/
|
||||
function resizeImage($path, $resWidth, $resHeight, $saveTo=null)
|
||||
{
|
||||
try {
|
||||
list($width, $height) = getimagesize($path);
|
||||
$imageInfo = @getimagesize($path);
|
||||
|
||||
if (!$imageInfo)
|
||||
throw new Exception("Could not get image information");
|
||||
|
||||
list($width, $height) = $imageInfo;
|
||||
$percentHeight = $resHeight / $height;
|
||||
$percentWidth = $resWidth / $width;
|
||||
$percent = ($percentWidth < $percentHeight) ? $percentWidth : $percentHeight;
|
||||
@@ -2294,26 +2298,36 @@ class G
|
||||
|
||||
// Resample
|
||||
$image_p = imagecreatetruecolor($resWidth, $resHeight);
|
||||
imagealphablending($image_p, false);
|
||||
imagesavealpha($image_p, true);
|
||||
|
||||
//calculating memory needed for image
|
||||
$imageInfo = GetImageSize($path);
|
||||
$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 < 50 ) $memoryNeeded = 50;
|
||||
if ( $memoryNeeded < 80 ) $memoryNeeded = 80;
|
||||
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);
|
||||
|
||||
// Output
|
||||
imagejpeg($image_p, $saveTo, 100);
|
||||
$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);
|
||||
}
|
||||
catch (Exception $oException) {
|
||||
throw $oException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge 2 arrays
|
||||
|
||||
Reference in New Issue
Block a user