Convert Images to Thumbnails Images Using PHP

by Darren Hedlund

 This image convertor uses features to automatically change a normal image to a smaller thumbnailed version of that image. To get started make a main folder for the thumbnailer to work. Now in that folder make two sub folders: main and thumb. Make sure to chmod the main and thumb folder as chmod 777.

Now, in the main folder throw in some of your normal large images, copy and paste the code below and place in you root folder that contains the main and thumb folders, and call the file index.php. Launch the index.php file, and it will begin to remake your large images to small images, and place them into the thumb folder.

Check the tutorial on displaying images to learn more on creating an image gallery..

 

<?
$new_w = 100;
$cfg_fullsizepics_path = "main";
$cfg_thumb_path = "thumb";
$filepath = "main";
$dir = dir($filepath);
while($entry=$dir->read()) {
if($entry == "." || $entry == "..") {
continue;
}
$fp = @fopen("$filepath/$entry","r");
$photo_filename = "$entry";
$image_stats = GetImageSize($cfg_fullsizepics_path."/".$entry);
$imagewidth = $image_stats[0];
$imageheight = $image_stats[1];
$img_type = $image_stats[2];
$ratio = ($imagewidth / $new_w);
$new_h = round($imageheight / $ratio);
if (!file_exists($cfg_thumb_path."/".$entry)) {
if ($img_type=="2") {
$src_img = imagecreatefromjpeg($cfg_fullsizepics_path."/".$entry);
$dst_img = imagecreate($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
imagejpeg($dst_img, "$cfg_thumb_path"."/$entry");
} elseif ($img_type=="3") {
$dst_img=ImageCreate($new_w,$new_h);
$src_img=ImageCreateFrompng($cfg_fullsizepics_path."/".$entry);
ImageCopyResized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
Imagepng($dst_img, "$cfg_thumb_path"."/$entry");
} elseif ($img_type=="1") {
$cfg_thumb_url=$cfg_fullsizepics_url;
}
}
echo "$entry";
echo "
";
}
?>


Darren Hedlund is a freelance Web developer, writer, and data analyst. Darren has a graduate degree in Computer Information Science and has spent the last 15 years developing application and environments from hand held, windows, web, virtual science, gaming, artificial intelligence and graphics design. Darren's coding knowledge ranges from C+, Visual Basic, .NET, PHP, JSP, REXX, KIXX, and many others. His graphical and environmental knowledge stems in Macromedia Flash, 3D studio Max, Curious Labs Poser, Adobe Photoshop, and many others. Darren works in many platforms ranging from database, visual design, and, system development.
View the original article Convert Images to Thumbnails Images Using PHP