Well I have a small problem. My script goes through a directory based on url query. While doing this it creates thumbnails for those images. Now the problem is that it creates PNG thumbnails but instead of transparent or at-least white background it puts in standard black.
Here is the code can any one point out my issue in the code, or possibly something I may have missed
<?php
$folder = $_GET['folder']; //POST if from form, GET if from URL
$thisdir = getcwd();
if(!file_exists($thisdir ."/"."$folder/thumbs")) {
mkdir($thisdir ."/"."$folder/thumbs" , 0777);
}
function returnimages($dirname="") {
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
$handle = opendir($dirname);
while(false !== ($filename = readdir($handle))) {
if(eregi($pattern, $filename)){ //if this file is a valid image
$files[] = $filename;
}
}
if (count($files)<>0) {
sort($files);
}
$curimage=0;
while($curimage !== count($files)){
$cropfile=$dirname.'/'.$files[$curimage];echo '<br>'.$cropfile;
if(preg_match('/[.](jpg)|(jpeg)$/', $cropfile)) {
$source_img = imagecreatefromjpeg($cropfile);
} elseif(preg_match('/[.](png)$/', $cropfile)) {
imagealphablending($source_img, false);
imagesavealpha($source_img, true);
$source_img = imagecreatefrompng($cropfile);
} elseif(preg_match('/[.](gif)$/', $cropfile)) {
$source_img = imagecreatefromgif($cropfile);
} else {
echo "Code 43: Unable to read file type.";
exit(0);
}
if (!$source_img) {
echo "could not create image handle";
exit(0);
}
$new_w = 480;
$new_h = 480;
$orig_w = imagesx($source_img);
$orig_h = imagesy($source_img);
$w_ratio = ($new_w / $orig_w);
$h_ratio = ($new_h / $orig_h);
if ($orig_w > $orig_h ) {//landscape from here new
$crop_w = round($orig_w * $h_ratio);
$crop_h = $new_h;
$src_x = ceil( ( $orig_w - $orig_h ) / 2 );
$src_y = 0;
} elseif ($orig_w < $orig_h ) {//portrait
$crop_h = round($orig_h * $w_ratio);
$crop_w = $new_w;
$src_x = 0;
$src_y = ceil( ( $orig_h - $orig_w ) / 2 );
} else {//square
$crop_w = $new_w;
$crop_h = $new_h;
$src_x = 0;
$src_y = 0;
}
$dest_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dest_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h); //till here
if(imagejpeg($dest_img, $dirname."/thumbs/".$files[$curimage], 80)) {
imagedestroy($dest_img);
imagedestroy($source_img);
} else {
echo "could not make thumbnail image";
exit(0);
}
$curimage++;
}
}
returnimages($name=$folder);
?>
This is because you’re using
imagejpegas an output function for every possible type of images. I changed your code in awhileloop to make it work:As you can see, depending on the filetype we’re using different output functions: imagejpeg, imagepng and imagegif. PNG and GIF files require some extra actions in order to preserve transparency. I’ve tested it on PHP 5.2.13 under Windows using JPEG, GIF and PNG (transparent and regular) files and seems like everything is working. You can tweak additional parameters, for example imagepng takes optional
$qualityand$filtersparameters.Also, regarding the code:
$_GET, this may be unsafe, because user can pass something like ‘/../../../../etc’foreachinstead ofwhilewith loop counter. I think glob can retrieve list of files. Andreturnimages($name=$folder);call doesn’t make sense to me. Do you really need to assign and call in one line?