Thanks man, thats great :) Its pretty cool to see what can be done with so little code (althought it is hard to understand code)
Im mostly interested in resizing and rounding edges. Your 1st code snipped might also be handful for spambot-protecting email addresses.
Since were sharing, here is a function i use to add thumbnails to a gallery.
It also implements caching in a folder.
$pic = path to the current full size photo file
$tn_path = directory used for storing thumbnails.
The rest is obvious from the comments.
I think you should not use the same a different tn_dir for every directory containing images to avoid name conflicts.
function get_tn($pic, $tn_path, $thumbsize=100, $size_axis='false')
{
//returns the path of the thumbnail, creates thumbnail if it doesn't exist yet
//thumsize: size in pixels. this size applies to x or y depending of the value of the sizeaxis argument.
//sizeaxis:
// - if "x" then the x (width) of the thumbnail will be equal to thumsize
// - if "y" then the y (height) of the thumbnail will be equal to thumsize
// - if not defined then either x or y (whichever is bigger) will be equal to thumbsize
// NOTES:
// - if image is smaller than thumbsize, then IT WILL NOT BE RESIZED!
// - will create tn_path if not exists
extension_loaded('gd') or dl("php_gd2");
// get pic path and filename
$dir_sep = "/";
$file_info = explode($dir_sep, $pic);
$pic_filename = array_pop($file_info);
$pic_path = implode($dir_sep, $file_info).$dir_sep;
// tn filename (we must make sure it will be .jpg)
$temp = explode('.', $pic_filename);
array_pop($temp);
$tn_filename = implode('.',$temp).'.jpg';
// check if tn_dir ends with a slash
strlen( strrchr ( $tn_path, $dir_sep) ) == 1 or $tn_path .= $dir_sep ;
// create dir if not exists
is_dir($tn_path) or mkdir($tn_path);
// generate thumbnail if none found
if (!is_file($tn_path.$tn_filename)) {
$tn_extension = strtolower(array_pop(explode('.', $pic_filename)));
if($tn_extension == "gif") $image = imagecreatefromgif($pic_path.$pic_filename);
elseif($tn_extension == "jpg" or $tn_extension == "jpeg") $image = imagecreatefromjpeg($pic_path.$pic_filename);
elseif($tn_extension == "png") $image = imagecreatefrompng($pic_path.$pic_filename);
else {
echo "<b>Image extension not recognised: $pic</b>";
return false;
}
if (!isset($image) or !$image) {
echo "<b>Error while drawing thumbnail $pic</b>";
return false;
}
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
if ($size_axis = 'x' or (!$size_axis and $imagewidth >= $imageheight) ) {
$thumbwidth = $thumbsize;
$factor = $thumbsize / $imagewidth;
$thumbheight = $imageheight * $factor;
}
if ($size_axis = 'y' or (!$size_axis and $imageheight >= $imagewidth) ) {
$thumbheight = $thumbsize;
$factor = $thumbsize / $imageheight;
$thumbwidth = $imagewidth * $factor;
}
$thumb = @imagecreatetruecolor($thumbwidth,$thumbheight);
//imagecopyresized($thumb, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imagewidth, $imageheight);
// better function:
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imagewidth, $imageheight);
if (!imagejpeg($thumb, $tn_path.$tn_filename)) {
echo "<b>Error while saving thumbnail $tn_path.$tn_filename</b>";
return false;
}
imagedestroy($image);
imagedestroy($thumb);
}
return $tn_path.$tn_filename; //returns the path of the created thumbnail
}
Edit: I should probably use some kind of checksum based on the full path to the full size image to generate thumbnail filenames, this way I could use the same thumbnail cache directory to store all thumbnails without risking name conflicts. I should also create subdirectories according to thumbnails size. This is quite bad code actually. But someone might make good use of it.