Skip to content

Instantly share code, notes, and snippets.

@nevali
Created July 16, 2009 13:10
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 nevali/148400 to your computer and use it in GitHub Desktop.
Save nevali/148400 to your computer and use it in GitHub Desktop.
Quick image resizing script
<?php
ini_set("memory_limit", "128M");
$target_path = 'full/';
$thumb_path = 'thumbs/';
$formats = array('jpg', 'gif', 'png');
/* Grab the extension of the uploaded file, and sanitise the rest of the
* basename, which should prevent any potential attacks.
*/
$ext = strtolower(pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION));
$name = strtolower(pathinfo($_FILES['Filedata']['name'], PATHINFO_FILENAME));
$name = preg_replace('/[^a-z0-9]/', '', $name) . '.' . $ext;
if(!in_array($ext, $formats))
{
die('File format not supported');
}
resizeImage($_FILES['Filedata']['tmp_name'], $ext, $target_path . $name, 900, 0, 80);
resizeImage($_FILES['Filedata']['tmp_name'], $ext, $thumb_path . $name, 300, 300, 100);
function loadImage($sourceFile, $sourceFormat, &$dimensions)
{
$img = null;
switch($sourceFormat)
{
case 'jpg':
$img = imagecreatefromjpeg($sourceFile);
break;
case 'png':
$img = imagecreatefrompng($sourceFile);
break;
case 'gif':
$img = imagecreatefromgif($sourceFile);
break;
}
if(!$img)
{
return null;
}
$dimensions[0] = imagesx($img);
$dimensions[1] = imagesy($img);
return $img;
}
function resizeImage($sourceFile, $sourceFormat, $destFile, $destWidth, $destHeight, $quality)
{
$dims = array();
if(!($source = loadImage($sourceFile, $sourceFormat, $dims)))
{
return false;
}
if(!$destWidth && !$destHeight)
{
/* No resizing required */
$destWidth = $dims[0];
$destHeight = $dims[1];
}
/* $destWidth and $destHeight are the size of the actual output image.
* $targetWidth and $targetHeight is what we resample the source to be.
* Where $dest... is smaller than $target..., cropping occurs.
*/
if(!$destWidth)
{
/* Destination will be whatever width is required to match height */
$targetHeight = $destHeight;
$targetWidth = $destWidth = $dims[0] * ($destHeight / $dims[1]);
}
else if(!$destHeight)
{
/* Destination will be whatever height is required to match width */
$targetWidth = $destWidth;
$targetHeight = $destHeight = $dims[1] * ($destWidth / $dims[0]);
}
else
{
/* Fixed output bounding box; find smallest side and resize
* proportionally, cropping as necessary.
*/
if($dims[1] > $dims[0])
{
/* Image is taller than it is wide */
$targetWidth = $destWidth;
$targetHeight = $dims[1] * ($destWidth / $dims[0]);
}
else
{
$targetHeight = $destHeight;
$targetWidth = $dims[0] * ($destHeight / $dims[1]);
}
}
$targetWidth = floor($targetWidth);
$targetHeight = floor($targetHeight);
$destWidth = floor($destWidth);
$destHeight = floor($destHeight);
echo "<p>Source is " . $dims[0] . " x " . $dims[1] . "<br />";
echo "Target is " . $targetWidth . " x " . $targetHeight . "<br />";
echo "...which will be cropped to " . $destWidth . " x " . $destHeight . "</p>";
$dest = imagecreatetruecolor($destWidth, $destHeight);
imagecolorallocate($dest, 0xff, 0xff, 0xff);
$dx = 0 - floor(($targetWidth - $destWidth) / 2);
$dy = 0 - floor(($targetHeight - $destHeight) / 2);
imagecopyresampled($dest, $source, $dx, $dy, 0, 0, $targetWidth, $targetHeight, $dims[0], $dims[1]);
imagejpeg($dest, $destFile, $quality);
imagedestroy($source);
imagedestroy($dest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment