Answer the question
In order to leave comments, you need to log in
Why is the background painted black when resizing a png shock with a transparent background using php?
When resizing images (png with a transparent background) using a php script, the background is painted black.
Why does this happen and how to avoid it?
Here is the whole script:
<?php
//Maximize script execution time
ini_set('max_execution_time', 0);
//Initial settings, Just specify Source and Destination Image folder.
$ImagesDirectory = 'C:\Users\Sun\Desktop\origin-picture-logo/';//Source Image Directory End with Slash
$DestImagesDirectory = 'D:\Color/'; //Destination Image Directory End with Slash
$NewImageWidth ; //New Width of Image
$NewImageHeight; // New Height of Image
$Quality ; //Image Quality
//Function that resizes image.
function resizeImage($SrcImage,$DestImage, $MaxWidth=20,$MaxHeight= 29,$Quality)
{
//echo $SrcImage;
global $iWidth;
global $iHeight;
global $img;
$img = $SrcImage;
list($iWidth,$iHeight,$type) = getimagesize($SrcImage);
$ImageScale = min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
$NewWidth = ceil($ImageScale*$iWidth);
$NewHeight = ceil($ImageScale*$iHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
imagealphablending($NewCanves, false);
imagesavealpha($NewCanves, true);
switch(strtolower(image_type_to_mime_type($type)))
{
case 'image/png':
$NewImage = imagecreatefrompng($SrcImage);
break;
case 'image/png':
$NewImage = imagecreatefrompng($SrcImage);
break;
case 'image/gif':
$NewImage = imagecreatefromgif($SrcImage);
break;
default:
return false;
}
// Resize Image
if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
{
// echo $iWidth;
$stndartwidth = $iWidth;
// copy file
if(imagejpeg($NewCanves,$DestImage,$Quality))
{
imagedestroy($NewCanves);
return true;
}
}
}
//Open Source Image directory, loop through each Image and resize it.
if($dir = opendir($ImagesDirectory)){
while(($file = readdir($dir))!== false){
$imagePath = $ImagesDirectory.$file;
$destPath = $DestImagesDirectory.$file;
$checkValidImage = @getimagesize($imagePath);
if(file_exists($imagePath) && $checkValidImage) //Continue only if 2 given parameters are true
{
//Image looks valid, resize.
resizeImage($imagePath,$destPath,$NewImageWidth = 230,$NewImageHeight = 138,$Quality = 100);
//if ($iWidth > $iHeight ){echo 'dsfd' ;resizeImage($imagePath,$destPath,$NewImageWidth = 280,$NewImageHeight = 280,$Quality = 100); ;}
//if ($iWidth < $iHeight ){echo 'sadas' ;resizeImage($imagePath,$destPath,$NewImageWidth = 260,$NewImageHeight = 260,$Quality = 100) ; ;}
echo $iWidth;
if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
{
echo $file.' resize Success!<br />';
/*
Now Image is resized, may be save information in database?
*/
}else{
echo $file.' resize Failed!<br />';
}
}
}
// echo $imagePath;
closedir($dir);
}
?>
Answer the question
In order to leave comments, you need to log in
The point is that imagecreatetruecolor creates an image with a black background. When the “color pairing mode” is on, a transparent pixel will not replace the background, but a new color will be calculated in accordance with the new color and the background color, taking into account the alpha channel of both colors - as a result, for a completely transparent pixel, we get the background color - i.e. black.
The solution is the following
After the line:
Add this:
// Turn off the color pairing mode
// Turn on the preservation of the alpha channel
Replace the line
with
And in the line below, specify the quality 0-9, if the maximum, then set $Quality=9
resizeImage($imagePath,$destPath,$NewImageWidth=230,$NewImageHeight=138,$Quality=100);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question