Gossamer Forum
Home : Products : DBMan : Discussions :

Automatic Thumbnailer without ImageMagick, using GD and a little PHP

Quote Reply
Automatic Thumbnailer without ImageMagick, using GD and a little PHP
Now this may or may not be useful for many of you. all i know is that i run almost everything using DBman, but due to my server not allowing ImageMagick to run, i have had to resort to php, almost every server has GD installed, and this may help you if you are one like me who wants to thumbnail a lot of pictures in one directory without ImageMagick.

Now assuming your directory where the Pictures are is pics/ and your thumbnail directory is thumbs/

open notepad, insert this code

and save it as thumbnail.php upload it to the server where


then link or go straight to that file using your browser and VIOLA. this will thumbnail everything in the specified directory.

You can specify the dimension by changing this line, by making them 275, 275 that means it will maintain that ratio, it wont really make a 275 x 275 image. its just like that so that whatever is the larger side will be 275. and it will fix the other dimension to keep the proportions.

Ohh another thing, this php file is much like an HTML file, so just add whatever HTML tags you want all the way at the bottom, and it will display that page when it created the thumbnails. if it didnt create them you will see a bunch of errors.

and 9 out of 10 times the error lies on how the JPG's where saved. In this case you cannot use SAVE FOR WEB (example using photoshop) because that takes away the necessary headers this script need to read to create the files. So just make sure that its RGB, and save using SAVE AS, either PNG or JPG.

change this line only with the directories and file prefix you want, and the dimensions.

if ($pics[0]!=""){
foreach ($pics as $p){
Function checkgd()
checks the version of gd, and returns "yes" when it's higher than 2
function checkgd(){
$phpinfo=stristr($phpinfo,"gd version");
if ($gd[0]=='2'){$gd2="yes";}
return $gd2;
Function ditchtn($arr,$thumbname)
filters out thumbnails
function ditchtn($arr,$thumbname){
foreach ($arr as $item){
if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
return $tmparr;
Function createthumb($name,$filename,$new_w,$new_h)
creates a resized image
$name Original filename
$filename Filename of the resized image
$new_w width of resized image
$new_h height of resized image
function createthumb($name,$filename,$new_w,$new_h){
global $gd2;
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
if ($old_x > $old_y) {
if ($old_x < $old_y) {
if ($old_x == $old_y) {
if ($gd2==""){
if (preg_match("/png/",$system[1])){
} else {
Function directory($directory,$filters)
reads the content of $directory, takes the files that apply to $filter
and returns an array of the filenames.
You can specify which files to read, for example
$files = directory(".","jpg,gif");
gets all jpg and gif files in this directory.
$files = directory(".","all");
gets all files.
function directory($dir,$filters){
if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
if ($filters != "all"){
while (($file = readdir($handle))!==false) {
for ($f=0;$f<sizeof($filters);$f++):
if ($system[1] == $filters[$f]){$files[] = $file;}
return $files;

This definetely works differently than what the file upload mod could use, but in my case, i initially used a file upload cgi that does a pop up window and you can upload as many files as you want, the down side is that you have to individually enter the file name of the image you want displayed when you actually upload the files. The one advantage of this i saw that i could have one record have as many images as possible and have thumbnails without using the upload mod, and on the display side for the project i thought it was easier and better to use than to have numbered directories with numbered images. Plus i was having issues with the files not being uploaded, but the directories would be created, i was guessing is that it was not properly CHMOD 777 newly created directory.

hope this helps