Convert Images to Thumbnails Images Using PHP
By Darren W. Hedlund2005-03-22
Convert Images to Thumbnail Images
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 "
";
}
?>
Tutorial pages:
|
|
|||||||||
You might also want to check these out:
|
Leave a Comment on "Convert Images to Thumbnails Images Using PHP"
You must be logged in to post a comment.
Link to This Tutorial Page!

