Skip to content

Instantly share code, notes, and snippets.

@IngmarBoddington
Created January 25, 2014 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IngmarBoddington/c0fb3248d9b5e80055a0 to your computer and use it in GitHub Desktop.
Save IngmarBoddington/c0fb3248d9b5e80055a0 to your computer and use it in GitHub Desktop.
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment