/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /* ThumbFly 2.0 - by kailey lampert ([email protected], trepmal.com) to include your on-the-fly thumbnail do this: <img src="<?php echo thumbfly( array( 'src'=>'image.jpg' , 'w' => 250 , 'clean' => true ) ); ?>" /> free to use & modify, you could probably make it better anyway - in fact, if you do, let me know! params (array): src = relative/server path to image (required) w = desired width (optional, default = null) h = desired height (optional, default = null) name = name of thumbnail (optional, default = src) clean = boolean (optional, default = false) if true, remakes thumbs with same name warn = boolean (optional, default = false) if true, creates a 'brkn img' image where it can't create thumbnail (width & height: if one is defined - image will be scaled, if neither - height & width will be halved) */ function thumbfly( $params ) { $defaults = array('src' => null, 'w' => null, 'h' => null, 'name' => null, 'clean' => false, 'warn' => false); $newname = $tmpdir.$newname; if ( !$w && !$h ) { $w = 135; $h = 45; } // if no width or height given, use defaults else if ( !$w && $h ) { $w = $h; } // if 1 dimension given, make it square else if ( !$h && $w ) { $h = $w; } // if 1 dimension given, make it square else { $newname = ''; } /* nothing happening... */ else : //src is real and thumb isn't cached... let's start resizing! $or = $ow/$oh; //original ratio if ( !$w && !$h ) { $w = $ow*(0.5); $h = $oh*(0.5); } // if no width or height given - change defaults here! else if ( !$w && $h ) { $w = ($h*$or); } //only height? scale else if ( !$h && $w ) { $h = ($w/$or); } //only width? scale $r = $w/$h; //here to prevent division by zero $modwidth = $ow; $modheight = $oh; //scaled size to work from $off_w = 0; $off_h = 0; //offsets if ($r > $or) : //if new is more landscape-y than original $modheight = $ow/$r; //slice off some of top & bottom $off_h = ($oh-$modheight)/2; //get .5 the diff for centering (even slicing) elseif ($ratio < $or) : //and reverse... $modwidth = $oh*$r; $off_w = ($ow-$modwidth)/2; endif; switch ($type) { case 1: /*gif*/ if ($trns_ind >= 0) { $trns_ind = imagecolorallocate($image_thumb, $trns_color['red'], $trns_color['green'], $trns_color['blue']); } imagecopyresampled($image_thumb, $image_original, '0', '0', $off_w, $off_h, $w, $h, $modwidth, $modheight); $the_thumb .= '.gif'; $thumb_loc .= '.gif'; break; case 2: /*jpg*/ imagecopyresampled($image_thumb, $image_original, '0', '0', $off_w, $off_h, $w, $h, $modwidth, $modheight); break; case 3: /*png*/ imagecopyresampled($image_thumb, $image_original, '0', '0', $off_w, $off_h, $w, $h, $modwidth, $modheight); break; }//end switch endif; return $newname; }// end function ?>
URL: http://trepmal.com/scripts/thumbfly-on-the-fly-thumbnails/