Skip to content

Instantly share code, notes, and snippets.

@dahlia
Created October 28, 2010 17:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dahlia/651817 to your computer and use it in GitHub Desktop.
Save dahlia/651817 to your computer and use it in GitHub Desktop.
Simple Image Resizing API
<?php
# Simple Image Resizing API
#
# A simple HTTP API for resizing an image (given by the URL).
# This small and dirty ad hoc web application is entirely written in
# PHP 5.3+ with GD. The source code is distributed under Public License.
function is_url($string, $scheme = null) {
$url = parse_url($string);
if (!isset($url['scheme'])) return false;
return is_null($scheme) || $url['scheme'] == $scheme;
}
function resize_image($image, $width = null, $height = null) {
$img_width = imagesx($image);
$img_height = imagesy($image);
if ($width && is_null($height)) {
$height = $img_height * ($width / $img_width);
} else if (is_null($width) && $height) {
$width = $img_width * ($height / $img_height);
} else if (is_null($width) && is_null($height)) {
$width = $img_width;
$height = $img_height;
}
$new_image = imagecreatetruecolor($width, $height);
imagealphablending($new_image, true);
imagecopyresampled($new_image, $image,
0, 0, 0, 0,
$width, $height, $img_width, $img_height);
return $new_image;
}
$mimetypes = array('png' => 'image/png', 'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg', 'wbmp' => 'image/vnd.wap.wbmp',
'gif' => 'image/gif');
$formats = array('png' => 'imagepng', 'jpg' => 'imagejpeg',
'jpeg' => 'imagejpeg', 'wbmp' => 'imagewbmp',
'gif' => 'imagegif');
$url = isset($_GET['url']) ? trim($_GET['url']) : null;
$width = isset($_GET['width']) ? (int) trim($_GET['width']) : null;
$height = isset($_GET['height']) ? (int) trim($_GET['height']) : null;
$format = isset($_GET['format']) ? trim(strtolower($_GET['format'])) : 'png';
if (!$url) {
header('Content-Type: text/html; charset=utf-8');
$fd = fopen(__FILE__, 'r');
fseek($fd, __COMPILER_HALT_OFFSET__);
$first = true;
while (!feof($fd)) {
$buffer = fread($fd, 8192);
if ($first) {
$first = false;
$buffer = preg_replace('/\s*\?>/', '', $buffer);
}
echo $buffer;
}
fclose($fd);
exit;
} else if (!is_url($url, 'http') && !is_url($url, 'https')) {
header('HTTP/1.1 403 Forbidden');
header('Content-Type: text/plain; charset=ascii');
echo "Invalid HTTP URL: $url";
exit;
} else if (empty($mimetypes[$format])) {
header('HTTP/1.1 403 Forbidden');
header('Content-Type: text/plain; charset=ascii');
echo "Unsupported format: $format";
exit;
}
$image_data = file_get_contents($url);
$image = imagecreatefromstring($image_data);
$resized_image = resize_image($image, $width, $height);
imagedestroy($image);
header("Content-Type: $mimetypes[$format]");
$func = $formats[$format];
if ($func == 'imagepng') {
imagepng($resized_image, null, 9);
} else if ($func == 'imagejpeg') {
imagejpeg($resized_image, null, 100);
} else {
$func($resized_image);
}
imagedestroy($resized_image);
__halt_compiler();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Simple Image Resizing API</title>
<head>
<body>
<h1>Simple Image Resizing API</h1>
<p>A simple HTTP API for resizing an image (given by the URL).</p>
<h2>Query Parameters</h2>
<dl>
<dt><code>url</code></dt>
<dd>An image URL.</dd>
<dt><code>width</code></dt>
<dd>A number of the width to be resized in pixel.</dd>
<dt><code>height</code></dt>
<dd>A number of the height to be resized in pixel.</dd>
<dt><code>format</code></dt>
<dd>An output image format. Choose one in:
<ul><li><code>png</code> (default)</li>
<li><code>jpeg</code></li>
<li><code>gif</code></li></ul></dd>
</dl>
<h2>Examples</h2>
<ul>
<li><a href="?url=http%3A%2F%2Fbit.ly%2F9yK8wz&format=gif">
<code>?url=http%3A%2F%2Fbit.ly%2F9yK8wz&format=gif</code></a></li>
<li><a href="?url=http%3A%2F%2Fbit.ly%2F9yK8wz&width=200">
<code>?url=http%3A%2F%2Fbit.ly%2F9yK8wz&width=100</code></a></li>
<li><a href="?url=http%3A%2F%2Fbit.ly%2F9yK8wz&width=200&height=200">
<code>?url=http%3A%2F%2Fbit.ly%2F9yK8wz&width=100&height=200</code>
</a></li>
</ul>
<h2>Source Code</h2>
<p>This small and dirty ad hoc web application is entirely written in
PHP 5.3+ with GD.</p>
<p>You can checkout this code from GitHub Gist:
<a href="http://gist.github.com/651817">
http://gist.github.com/651817</a>.</p>
<p>The source code is distributed under Public License.</p>
</body>
</html>
@talhag3
Copy link

talhag3 commented Oct 8, 2020

is ok to use for production ?

@dahlia
Copy link
Author

dahlia commented Oct 9, 2020

@talhag3 I haven't used this in the last decade… so cannot guarantee if it would still work.

@talhag3
Copy link

talhag3 commented Oct 13, 2020

@dahlia
i tested it on locally its working fine, is it put images in tmp ?

@dahlia
Copy link
Author

dahlia commented Oct 13, 2020

@talhag3 No, it does not save any cache on the disk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment