PHP thumbnail generator

12.Jul.2010

On almost every website, we need a image thumbnail generator since we don’t want to load whole images to users computer. Here is a snippet of PHP thumbnail generator, that helps you create thumbnails of your images. However, this PHP thumbnail generator is not very wise, and shouldn’t be used on high traffic websites since it only generates thumbnail on the fly, and not making a copy of this resized image.

PHP thumbnail generator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
$upfile = $_REQUEST['p'];
 
$max_width  = $_REQUEST["w"];
$max_height = $_REQUEST["h"];
 
$def_width  = $_REQUEST["wh"];
$def_height = $_REQUEST["hw"];
 
if(empty($def_width) && empty($def_height)) {
	$def_width = '245';
	$def_height = '245';
}
if ($def_height=='') $def_height = $def_width;
 
//IMAGE PATH
$exportPng = false;
if(!file_exists($_SERVER['DOCUMENT_ROOT'] . $upfile)) $upfile = $_SERVER['DOCUMENT_ROOT'] . $upfile;
else {
	$upfile = "img/nophoto.jpg";
	$exportPng = true;
}
 
//since we already have predefined values of extension
$ext = strtolower(substr($upfile, -3));
 
//INCREASE MEMORY LIMIT WHEN WORKING WITH
ini_set('memory_limit', '64M');
 
//MAKE NEW IMAGE OUT OF EXTENSION
if ($ext=="gif") { $src = @ImageCreateFromGif($upfile); header("Content-Type: image/gif"); }
if ($ext=="jpg") { $src = @ImageCreateFromJpeg($upfile); header("Content-Type: image/jpeg"); }
if ($ext=="png") { $src = @ImageCreateFromPng($upfile); header("Content-Type: image/png"); }
 
$size = GetImageSize($upfile);
$width = $size[0];
$height = $size[1];
 
if ($def_width == '') {
	// Proportionally resize the image to the max sizes
	if (($max_width == '')&&($max_height == '')) {
		$max_width = $width;
		$max_height = $height;
	}
	if ($max_width == '')  $max_width  = ($max_height*$width) / $height;
	if ($max_height == '') $max_height = ($max_width*$height) / $width;
	if ($max_height > $max_width)  $highter_dimension=$max_height;
				else $highter_dimension = $max_width;
 
	if ($width == 0) $x_ratio = $max_width;
		  else $x_ratio = $max_width / $width;
	if ($height == 0) $y_ratio = $max_height;
		   else $y_ratio = $max_height / $height;
 
	if( ($width <= $max_width) && ($height <= $max_height) ) {
		$tn_width = $width;
		$tn_height = $height;
	} elseif (($x_ratio * $height) < $max_height) {
		$tn_height = ceil($x_ratio * $height);
		$tn_width = $max_width;
	} else {
		$tn_width = ceil($y_ratio * $width);
		$tn_height = $max_height;
	}
	$dst = ImageCreateTrueColor($tn_width, $tn_height);
	ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
}
 
if($def_width != '') {
	$highter_dimension = $def_width;
	if ($def_width < $def_height) $highter_dimension = $def_height;
 
	$factor_w = $width / $def_width;
	$factor_h = $height / $def_height;
 
	if ($factor_w > $factor_h) {
		$new_height = floor($def_height * $factor_h);
		$new_width = floor($def_width  * $factor_h);
	} else {
		$new_height = floor($def_height * $factor_w);
		$new_width = floor($def_width  * $factor_w);
	}
 
	$src_x = ceil(($width  - $new_width) / 2);
	$src_y = ceil(($height - $new_height) / 2);
 
	$dst = ImageCreateTrueColor($def_width, $def_height);
	@ImageCopyResized($dst, $src, 0, 0, $src_x, $src_y, $def_width, $def_height, $new_width, $new_height);
}
 
if(!$exportPng)	 Header("Content-type: image/jpeg");
			else Header("Content-type: image/png");
 
 
//OUTPUT INTO PNG TO GET HIGHER QUALITY IMAGE
if(!$exportPng)  ImageJpeg($dst);
			else ImagePng($dst);
 
@ImageDestroy($src);
@ImageDestroy($dst);

How to use PHP thumbnail generator

Let’s say you have saved previous snippet as i.php in root of your website.

In order to have thumbnail with predefined width and height, add following code to your HTML:

1
<img src="/i.php?wh=300&hw=200&p=/path/to/image.jpg" />

In order to have thumbnail with predefined width and have height scaled based upon original size, add following code to your HTML:

1
<img src="/i.php?w=300&p=/path/to/image.jpg" />