Retain alpha-channel when resizing images (useful for the logo).
This commit is contained in:
@@ -2284,35 +2284,49 @@ class G
|
|||||||
*/
|
*/
|
||||||
function resizeImage($path, $resWidth, $resHeight, $saveTo=null)
|
function resizeImage($path, $resWidth, $resHeight, $saveTo=null)
|
||||||
{
|
{
|
||||||
try {
|
$imageInfo = @getimagesize($path);
|
||||||
list($width, $height) = getimagesize($path);
|
|
||||||
$percentHeight = $resHeight / $height;
|
|
||||||
$percentWidth = $resWidth / $width;
|
|
||||||
$percent = ($percentWidth < $percentHeight) ? $percentWidth : $percentHeight;
|
|
||||||
$resWidth = $width * $percent;
|
|
||||||
$resHeight = $height * $percent;
|
|
||||||
|
|
||||||
// Resample
|
if (!$imageInfo)
|
||||||
$image_p = imagecreatetruecolor($resWidth, $resHeight);
|
throw new Exception("Could not get image information");
|
||||||
|
|
||||||
//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);
|
|
||||||
|
|
||||||
// Output
|
list($width, $height) = $imageInfo;
|
||||||
imagejpeg($image_p, $saveTo, 100);
|
$percentHeight = $resHeight / $height;
|
||||||
chmod($saveTo, 0666);
|
$percentWidth = $resWidth / $width;
|
||||||
}
|
$percent = ($percentWidth < $percentHeight) ? $percentWidth : $percentHeight;
|
||||||
catch (Exception $oException) {
|
$resWidth = $width * $percent;
|
||||||
throw $oException;
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user