Skip to content

Instantly share code, notes, and snippets.

@zacscott
Last active December 29, 2015 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacscott/7655122 to your computer and use it in GitHub Desktop.
Save zacscott/7655122 to your computer and use it in GitHub Desktop.
Simple utility which dynamically creates thumbnail/preview images.
<?php
namespace \net\zeddev\util;
/**
* A simple utility to dynamically create thumbnail images. Images can be
* stored on disk, or served on the fly (depending how constructed). For
* example:
*
* $ondisk = new Thumbnailer('thumbnail/dir/path');
* $onfly = new Thumbnailer(); // i.e. no thumbnail directory
*
* Thumbnails can then be produced like so:
*
* $inst->thumbit('my/image.jpg'); // see thumbit() for all parameters
*
* **NOTE:** This file is released as a stand-alone utility. The original
* file and the associated unit test can be found on GitHub Gist -
* [here](https://gist.github.com/zscott92/7655122).
*
* @author Zachary Scott <zscott.dev@gmail.com>
*/
class Thumbnailer {
private $dir;
/**
* @param $dir The thumbnail directory. If `null` thumbnails are output
* directly, which is handy if you want to serve thumbnails on the fly
* (or just don't have write permission).
*/
public function __construct($dir = null) {
assert($dir == null || is_readable($dir));
$this->dir = $dir;
}
// opens an image with GD based on the mime type
private function readImage($file) {
$size = getimagesize($file);
switch ($size["mime"]) {
case "image/jpeg":
return imagecreatefromjpeg($file); //jpeg file
case "image/gif":
return imagecreatefromgif($file); //gif file
case "image/png":
return imagecreatefrompng($file); //png file
default:
return false;
}
}
// export image as jpeg
private function writeImage($img, $path = null) {
assert($img != null);
// add jpeg extension, if not already
if ($path != null && !preg_match('/.jpg$/i', $path))
$path = "$path.jpg";
// export to jpeg
imagejpeg($img, $path, 90);
return $path;
}
/**
* Creates a thumbnail of given size.
*
* @param $img The image to make a thumbnail of.
* @param $width The width of the produced thumbnail. A value of < 0
* maintains aspect ration based on height.
* @param $height The height of hte produced thumbnail. Again < 0 keeps
* aspect ratio based on width.
*/
public function thumbit($img, $width = 120, $height = 0) {
assert(is_readable($img));
assert($width > 0 || $height > 0);
list($srcw, $srch) = getimagesize($img);
// scale image height, keeping aspect ratio
if ($width > 0 && $height < 1) { // scale x
$height = ($width / $srcw) * $srch;
} else if ($height > 0 && $width < 1) { // scale y
$width = ($height / $srch) * $srcw;
} // else scale both
$thumb = imagecreatetruecolor($width, $height);
$source = $this->readImage($img);
// resize thumbnail
imagecopyresized(
$thumb, $source,
0, 0, 0, 0,
$width, $height, $srcw, $srch
);
// set path to save
if ($this->dir == null) {
$thumbPath = null;
} else {
$thumbPath = $this->dir.'/'.basename($img);
}
// export image
$thumbPath = $this->writeImage($thumb, $thumbPath);
imagedestroy($thumb);
return $thumbPath;
}
}
?>
<?php
require_once dirname(__FILE__).'/code/class.Thumbnailer.php';
// handle form submission
if (isset($_POST['ThumbForm'])) {
$t = new Thumbnailer(
$_POST['thumb']
);
// create thumbnail image
$thumbPath = $t->thumbit(
$_POST['image'],
$_POST['width'],
$_POST['height']
);
}
// form values
$image = isset($_POST['image']) ? $_POST['image'] : 'upload/path.jpg';
$thumb = isset($_POST['thumb']) ? $_POST['thumb'] : 'thumbs';
$width = isset($_POST['width']) ? $_POST['width'] : '120';
$height = isset($_POST['height']) ? $_POST['height'] : '0';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Thumbnailer Test</title>
<style type="text/css">
input {
display: block;
}
</style>
</head>
<body>
<h1>Thumbnailer Test</h1>
<?php
// put thumbnail preview
if (isset($thumbPath)) {
echo "<a href='$image' target='_thumb'>";
echo "<img src='$thumbPath'/>";
echo "</a>\n";
}
?>
<form action="thumbnail.php" method="post">
<label for="image">Image path</label>
<input type="text" name="image" value="<?php echo $image ?>"/>
<label for="thumb">Thumbnail path</label>
<input type="text" name="thumb" value="<?php echo $thumb ?>"/>
<label for="width">Width</label>
<input type="number" name="width" value="<?php echo $width ?>"/>
<label for="height">Height</label>
<input type="number" name="height" value="<?php echo $height ?>"/>
<input type="submit" name="ThumbForm" value="Thumbit"/>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment