При изменении размера картинок(png сс прозрачным фоном) при помощи php скрипта происходит закрашивание фона в черный.
Почему это происходит и как этого избежать ?
Вот весь скрипт :
<?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);
}
?>