Skip to content

Instantly share code, notes, and snippets.

@cimmwolf
Last active October 15, 2016 06:55
Show Gist options
  • Save cimmwolf/c927eb1d5db5e3914132aaae3b3781c1 to your computer and use it in GitHub Desktop.
Save cimmwolf/c927eb1d5db5e3914132aaae3b3781c1 to your computer and use it in GitHub Desktop.
<?php
/**
* @author: Denis Beliaev
*/
$matches = [];
if (preg_match('#^(.*?)@(\d+|-)x(\d+|-)\.(gif|jpe?g|png)$#', $_SERVER['REQUEST_URI'], $matches)) {
if (($newW = $matches[2]) == '-')
$newW = -1;
if (($newH = $matches[3]) == '-')
$newH = -1;
$filePath = __DIR__ . $matches[1] . '.' . $matches[4];
switch ($matches[4]) {
case 'gif':
header('Content-Type: image/gif');
break;
case 'jpg':
header('Content-Type: image/jpeg');
break;
case 'jpeg':
header('Content-Type: image/jpeg');
break;
case 'png':
header('Content-Type: image/png');
$image = imagecreatefrompng($filePath);
$image = imagescale($image, $newW, $newH);
imagealphablending($image, true);
imagesavealpha($image, true);
imagepng($image);
break;
}
if (!isset($image)) {
$image = imagecreatefromjpeg($filePath);
$image = imagescale($image, $newW, $newH);
imagejpeg($image);
}
imagedestroy($image);
} else
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment