Creating thumbnail through PHP

Easy to use function to create thumbnail image from image, it very use full for creating catalog display where user need not to enter two file for thumbnail and main image

 PHP |  copy code |? 
01
/*thumb.php*/
02
if($_GET[image])
03
{
04
	$image = "imagepath/".$_GET[image];
05
	if (!$max_width)
06
		$max_width = 200;
07
	if (!$max_height)
08
		$max_height = 150;
09
 
10
	$size = getimagesize($image);
11
	$width = $size[0];
12
	$height = $size[1];
13
 
14
	$x_ratio = $max_width / $width;
15
	$y_ratio = $max_height / $height;
16
 
17
	if (($height <= $max_height) && ($width <= $max_width))
18
		{
19
			$tn_height = $height;
20
			$tn_width = $width;
21
		}
22
	elseif (($x_ratio * $height) < $max_height) 
23
		{
24
			$tn_height = ceil($x_ratio * $height);
25
			$tn_width = $max_width;
26
		}
27
	else {
28
		$tn_width = ceil($y_ratio * $width);
29
		$tn_height = $max_height;
30
		}
31
 
32
	$src = imagecreatefromjpeg($image);
33
	//echo "width".$tn_width."height".$tn_height;
34
	//exit;
35
	$dst = imagecreatetruecolor($tn_width,$tn_height);
36
	imagecopyresampled ($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
37
	//imagecreateresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
38
	header('Content-type: image/jpeg');
39
	imagejpeg($dst, null, -1);
40
	imagedestroy($src);
41
	imagedestroy($dst);
42
}
43

Usage
1
<img src="thumb.php?image=abc.jpg"/>

  • Share/Bookmark
No Responses to “Creating thumbnail through PHP”

Post a Comment