Return to Snippet

Revision: 49568
at July 25, 2011 14:39 by dillonchr


Initial Code
<?php
##
#
# $name = full path to original source
# $filename = full path to thumbnail
# $new_w thumbnail width
# $new_h thumbnail height
# $debug echo a few diagnostics
#
##

function createthumb($name,$filename,$new_w=500,$new_h=373,$debug=false) {
	$results = "";
	$array = explode(".",strtolower($name));
	$system = end($array);
	$results = "file extension = $system <br>";
	if (preg_match("/jpg|jpeg/",$system)) $src_img = imagecreatefromjpeg($name);
	if (preg_match("/png/",$system)) $src_img = imagecreatefrompng($name);
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	
	$results .= "image size = $old_x x $old_y <br>";
	
	if($old_x > $old_y) {
		$thumb_h = $new_h;
		$ratio = $new_h / $old_y;
		$thumb_w = ($old_x * $ratio);
	}
	if($old_x < $old_y) {
		$thumb_w = $new_w;
		$ratio = $new_w / $old_x;
		$thumb_h = ($old_y * $ratio);
	}
	if($old_x == $old_y) {
		$thumb_w = $new_w;
		$thumb_h = $new_h;
	}
	if($new_h < $thumb_h) {
		$yloc = round(($thumb_h - $new_h) / 2);
	} else {
		$yloc = 0;
	}
	
	$results .= "output size = $thumb_w x $thumb_h <br>
	y offset = $yloc <br>";
	$dst_img = ImageCreateTrueColor($new_w,$new_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,$yloc,$thumb_w,$thumb_h,$old_x,$old_y); 
	if (preg_match("/png/",$system)) {
		imagepng($dst_img,$filename); 
	} else {
		imagejpeg($dst_img,$filename); 
	}
	imagedestroy($dst_img); 
	imagedestroy($src_img);
	if($debug) echo $results;
	return "Resized the image successfully!";
}

?>

Initial URL


Initial Description
Tweaked from the icant.co.uk thumbnail generator

Initial Title
PHP GD Thumbnail Generator 0.1

Initial Tags
php, resize

Initial Language
PHP