1<?php
2/*
3image.php
4
5A simple image resize script that resizes images given either a maxsize, maxheight or maxwidth
6
7Usage
8=====
9-to resize an image to a max of 400px along the longest side
10<img src="image.php?size=400&file=filename.jpg" />
11
12-to resize an image to a height of 400px (width will be kept to the right aspect ratio)
13<img src="image.php?size=h400&file=filename.jpg" />
14
15-to resize an image to a width of 400px (height will be kept to the right aspect ratio)
16<img src="image.php?size=w400&file=filename.jpg" />
17
18This script is very simple and should not be considered for production use. There are many image
19resizing scripts available that have better error checking, support for other formats (this only
20supports jpg) and have image caching. Cachine makes a HUGE difference to overall speed.
21
22@author Harvey Kane harvey@harveykane.com
23
24*/
25
26/* Get information from Query String */
27if (!isset($_GET['file']) || !isset($_GET['size'])) {
28	echo "Image variables not specified correctly";
29	exit();
30}
31
32$file = $_GET['file'];
33$size = $_GET['size'];
34
35/* Get image dimensions and ratio */
36list($width, $height) = getimagesize($file);
37$ratio = $width / $height;
38
39/* Decide how we should resize image - fixed width or fixed height */
40if (substr($size, 0, 1) == 'h') {
41	$type = 'fixedheight';
42} elseif (substr($size, 0, 1) == 'w') {
43	$type = 'fixedwidth';
44} elseif ($height > $width) {
45	$type = 'fixedheight';
46} else {
47	$type = 'fixedwidth';
48}
49
50/* Calculate new dimensions */
51if ($type == 'fixedheight') {
52	$new_width = floor(str_replace('h','',$size) * $ratio);
53	$new_height = str_replace('h','',$size);
54} else {
55	$new_width = str_replace('w','',$size);
56	$new_height = floor(str_replace('w','',$size) / $ratio);
57}
58
59/* Resample */
60$new_image = imagecreatetruecolor($new_width, $new_height);
61$old_image = imagecreatefromjpeg($file);
62imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
63
64/* Output */
65header('Content-type: image/jpeg');
66imagejpeg($new_image, null, 100);
67exit();
68