IngmarBoddington
1/25/2014 - 5:42 PM

Simple image resizer (requires GD)

Simple image resizer (requires GD)

<?php

//Needs injection protection / validation / extention for other image types

header('Content-type: image/jpeg');
ini_set('memory_limit', '50M');

if (isset($_GET['f'])) {

	$file = $_GET['f'];
    
	list($width, $height) = @getimagesize($file);
    
	//Set new dimensions (defaults to old if no w parameter given)
	if (isset($_GET['w'])) {
		$newWidth = $_GET['w'];
		$newHeight = ($newWidth * $height) / $width;
	} else {
		$newWidth = $width;
		$newHeight = $height;
	}
	//Set new canvas and source
	$canvas = imagecreatetruecolor($newWidth, $newHeight);
	$source = imagecreatefromjpeg($file);
    
	//Create Image
	imagecopyresampled($canvas, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
	imagejpeg($canvas, null, 100);
    
}