Thumbnails
|
|
|
|
<?php
function printThumbnail($imgfile,$max_width=100,$max_height=100) {
list($org_width,$org_height,$orgtype) = getimagesize($imgfile);
$div_width = $org_width / $max_width;
$div_height = $org_height / $max_height;
if($div_width >= $div_height) {
$new_width = $max_width;
$new_height = round($org_height / $div_width);
}
else {
$new_height = $max_height;
$new_width = round($org_width / $div_height);
}
switch($orgtype) {
case 1: $im = imagecreatefromgif($imgfile); break;
case 2: $im = imagecreatefromjpeg($imgfile); break;
case 3: $im = imagecreatefrompng($imgfile); break;
}
if($im) {
$tn = imagecreate($new_width,$new_height);
if($tn) {
imagecopyrisized($tn,$im,0,0,0,0,$new_width,$new_height,$org_width,$org_height);
switch($orgtype) {
case 1: header("Content-Type: image/gif"); imagegif($tn); break;
case 2: header("Content-Type: image/jpeg"); imagejpeg($tn); break;
case 3: header("Content-Type: image/png"); imagepng($tn); break;
}
imagedestroy($tn);
}
}
}
function saveThumbnail($imgfile,$thfile,$max_width=100,$max_height=100,$jpgq=60) {
list($org_width,$org_height,$orgtype) = getimagesize($imgfile);
$div_width = $org_width / $max_width;
$div_height = $org_height / $max_height;
if($div_width >= $div_height) {
$new_width = $max_width;
$new_height = round($org_height / $div_width);
}
else {
$new_height = $max_height;
$new_width = round($org_width / $div_height);
}
switch($orgtype) {
case 1: $im = imagecreatefromgif($imgfile); break;
case 2: $im = imagecreatefromjpeg($imgfile); break;
case 3: $im = imagecreatefrompng($imgfile); break;
}
if($im) {
$tn = imagecreate($new_width,$new_height);
if($tn) {
imagecopyresized($tn,$im,0,0,0,0,$new_width,$new_height,$org_width,$org_height);
switch($orgtype) {
case 1: imagegif($tn,$thfile); return 1; break;
case 2: imagejpeg($tn,$thfile,$jpgq); return 2; break;
case 3: imagepng($tn,$thfile); return 3; break;
}
imagedestroy($tn);
}
}
}
function printSavedThumbnail($imgfile,$imgpath,$thpath,$max_width=100,$max_height=100,$jpgq=60) {
if(!file_exists($thpath.$imgfile)) {
if($imgtype = saveThumbnail($imgpath.$imgfile,$thpath.$imgfile,$max_width,$max_height,$jpgq))
printImageFile($imgtype,$thpath,$imgfile);
}
else {
list(,,$imgtype) = getimagesize($thpath.$imgfile);
if($imgtype) printImageFile($imgtype,$thpath.$imgfile);
}
}
function printImageFile($imgtype,$imgfile) {
switch($imgtype) {
case 1: header("Content-Type: image/gif"); break;
case 2: header("Content-Type: image/jpeg"); break;
case 3: header("Content-Type: image/png"); break;
}
$fh = fopen($imgfile,"r");
if($fh) {
fpassthru($fh);
fclose($fh);
}
} ?>
|
|
|
Usage Example
|
// The Path to the Image dir.
$imgpath = "./images/";
// The Path to the Thumbnail dir (where the Thumbnails are saved)
$thpath = "./images/thumb/";
// The Max. Width
$max_width = 200;
// The Max. Height
$max_height = 200;
// If the Image is the JPEG Format, set here the Quality.
$jpeg_qual = 100;
// This is the On-the-Fly Method
if(file_exists($imgpath."logo.jpg")) printThumbnail("logo.jpg");
// OR use the this Method. Its better because it is faster then on-the-fly!
if(file_exists($imgpath."logo.jpg")) printSavedThumbnail("logo.jpg",$imgpath,$thpath,$max_width,$max_height,$jpeg_qual);
|
|
|
Rate This Script
|
|
|
|