<?php
//Maximize script execution time
ini_set('max_execution_time', 0);
//Initial settings, Just specify Source and Destination Image folder.
$ImagesDirectory = '../upload/'; //Source Image Directory End with Slash
$DestImagesDirectory = '../upload/'; //Destination Image Directory End with Slash
$NewImageWidth = 800; //New Width of Image
$NewImageHeight = 800; // New Height of Image
$Quality = 80; //Image Quality
//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.
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 />';
}
}
}
closedir($dir);
}
//Function that resizes image.
function resizeImage($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
{
list($iWidth,$iHeight,$type) = getimagesize($SrcImage);
$ImageScale = min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
$NewWidth = ceil($ImageScale*$iWidth);
$NewHeight = ceil($ImageScale*$iHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
switch(strtolower(image_type_to_mime_type($type)))
{
case 'image/jpeg':
$NewImage = imagecreatefromjpeg($SrcImage);
break;
case 'image/png':
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($SrcImage, 255, 255, 255);
// removing the black from the placeholder
imagecolortransparent($SrcImage, $background);
// turning off alpha blending (to ensure alpha channel information is preserved, rather than removed (blending with the rest of the image in the form of black))
imagealphablending($SrcImage, false);
// turning on alpha channel information saving (to ensure the full range of transparency is preserved)
imagesavealpha($SrcImage, true);
$NewImage = imagecreatefrompng($SrcImage);
break;
case 'image/gif':
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($SrcImage, 255, 255, 255);
// removing the black from the placeholder
imagecolortransparent($SrcImage, $background);
$NewImage = imagecreatefromjpeg($SrcImage);
break;
default:
return false;
}
// Resize Image
if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
{
// copy file
if(imagejpeg($NewCanves,$DestImage,$Quality))
{
imagedestroy($NewCanves);
return true;
}
}
}
?>
2.045 views
Hallo, ik heb een routine om gemakkelijk beeldbestanden te verkleinen. Werkt, alleen niet voor png. De transparante achtergrond wordt zwart. Wat doe ik fout.