Hello guys. I´ve downloaded this code from the site but can´t work out why does it make so dirty thumbnails. They are coloured, or just bad. I´ll show you an example if you want. Well, the code is:
<?$maxw = 150; // maximum width of images (height will be scaled accordingly)
$thumbdir = "thumbs"; // directory to place thumbnail images
$url = $_SERVER["PHP_SELF"]."?";
$sfxs = array(".jpg",".jpeg",".png",".JPG");
$curdir = dirname(stripslashes($_SERVER["PATH_TRANSLATED"]));
$slash = (strstr($curdir,"\\") ) ? "\\" : "/";
$thumbdir = $thumbdir;
$images = array();
@ $handle = opendir($curdir);
if (!$handle ) {
error("could not open directory!");
exit;
}
while ( $file = readdir($handle) ) {
if (isValid($file,$sfxs) ) {
$tmp = array();
$tmp[0] = $file;
$tmp[1] = filemtime($file);
$tmp[2] = filesize($file);
array_push($images,$tmp);
// do we need to create a thumbnail?
if ( !file_exists($thumbdir.$slash.$file) ) {
createThumb($file);
}
}
}
function isValid($f,$a) {
$t = getSuffix($f);
return ( in_array($t,$a) ) ? true : false;
}
function getSuffix($f) {
$n = strrpos($f,".");
return substr($f,$n,strlen($f)-$n);
}
function createThumb($f) {
// use max width config value
global $maxw, $curdir, $copyfile, $thumbdir, $slash;
$type = getSuffix($f);
// png or jpeg?
// either way get image
if ($type==".png") {
$input = imagecreatefrompng($f);
} else {
$input = imagecreatefromjpeg($f);
}
if ( !$input ) {
error("not a valid image file :(");
return false;
}
// get size ( [0]=width, [1]=height )
$tmp = getimagesize($f);
if ( !$tmp ) {
error("Could not get input image size");
return false;
}
// get width of new thumbnail by taking
// the smaller value from max and tmp width
$w = ($tmp[0]>$maxw) ? $maxw : $tmp[0];
// scale height according to width
$h = $tmp[1] * ($maxw/$tmp[0]);
// create output image
@ $output = imagecreate($w,$h);
if ( !$output ) {
error("could not create output image");
return false;
}
// copy big image over to thumbnail, and resize down
imagecopyresampled( $output,$input, 0,0, 0,0, $w,$h, $tmp[0],$tmp[1] );
$newfile = $thumbdir.$slash.$f;
// do the outputting!
if ( $type == ".png" ) {
imagepng($output,$newfile);
} else {
imagejpeg($output,$newfile);
}
}
function error($str) {
echo '<div style="background-color:#ffffff;color:#660000;font-weight:bold">'.$str.'</div>';
}
?>
Any ideas? Thanks in advance!
Edit: changed resized to resampled.