2013-03-23 17:35:44 +01:00
|
|
|
|
<?php
|
|
|
|
|
/*
|
2015-07-07 14:01:47 +02:00
|
|
|
|
* MinigalNano, a PHP/HTML/CSS KISS image gallery
|
|
|
|
|
*
|
|
|
|
|
* Licence : This script and included files are subject to licensing from
|
|
|
|
|
* Creative Commons (http://creativecommons.org/licenses/by-sa/3.0/)).
|
|
|
|
|
* Authors : Based on the original work of Tomas Rybak (2010, www.minigal.dk),
|
|
|
|
|
* this current version is hosted at https://github.com/sebsauvage/MinigalNano
|
|
|
|
|
* and is maintained by the MinigalNano community.
|
|
|
|
|
* Usage : <img src="createthumb.php?filename=photo.jpg&size=100">
|
2015-07-02 00:08:46 +02:00
|
|
|
|
*/
|
2013-03-23 18:46:00 +01:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
error_reporting(0);
|
|
|
|
|
|
|
|
|
|
$get_filename = $_GET['filename'];
|
|
|
|
|
$get_size = @$_GET['size'];
|
2015-07-02 00:08:46 +02:00
|
|
|
|
if (empty($get_size)) {
|
|
|
|
|
$get_size = 120;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (preg_match("/^\/.*/i", $get_filename)) {
|
|
|
|
|
die("Unauthorized access !");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (preg_match("/.jpe?g$/i", $get_filename)) {
|
|
|
|
|
$get_filename_type = "JPG";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (preg_match("/.gif$/i", $get_filename)) {
|
|
|
|
|
$get_filename_type = "GIF";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (preg_match("/.png$/i", $get_filename)) {
|
|
|
|
|
$get_filename_type = "PNG";
|
|
|
|
|
}
|
2014-11-25 03:09:30 +01:00
|
|
|
|
|
2015-06-30 15:32:08 +02:00
|
|
|
|
/**
|
|
|
|
|
* Vertical flip
|
|
|
|
|
* @author http://stackoverflow.com/questions/8232722/how-to-flip-part-of-an-image-horizontal-with-php-gd
|
|
|
|
|
*/
|
2014-06-04 16:41:21 +02:00
|
|
|
|
function flipVertical(&$img) {
|
|
|
|
|
$size_x = imagesx($img);
|
|
|
|
|
$size_y = imagesy($img);
|
|
|
|
|
$temp = imagecreatetruecolor($size_x, $size_y);
|
2015-07-02 00:08:46 +02:00
|
|
|
|
$x = imagecopyresampled($temp, $img, 0, 0, 0, ($size_y - 1), $size_x, $size_y, $size_x, 0 - $size_y);
|
|
|
|
|
if ($x) {
|
2014-06-04 16:41:21 +02:00
|
|
|
|
$img = $temp;
|
|
|
|
|
} else {
|
|
|
|
|
die("Unable to flip image");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-30 15:32:08 +02:00
|
|
|
|
/**
|
|
|
|
|
* Horizontal flip
|
|
|
|
|
* @author http://stackoverflow.com/questions/8232722/how-to-flip-part-of-an-image-horizontal-with-php-gd
|
|
|
|
|
*/
|
2014-06-04 16:41:21 +02:00
|
|
|
|
function flipHorizontal(&$img) {
|
|
|
|
|
$size_x = imagesx($img);
|
|
|
|
|
$size_y = imagesy($img);
|
|
|
|
|
$temp = imagecreatetruecolor($size_x, $size_y);
|
2015-07-02 00:08:46 +02:00
|
|
|
|
$x = imagecopyresampled($temp, $img, 0, 0, ($size_x - 1), 0, $size_x, $size_y, 0 - $size_x, $size_y);
|
|
|
|
|
if ($x) {
|
2014-06-04 16:41:21 +02:00
|
|
|
|
$img = $temp;
|
|
|
|
|
} else {
|
|
|
|
|
die("Unable to flip image");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-30 15:32:08 +02:00
|
|
|
|
/**
|
|
|
|
|
* Sanitizing (this allows for collisions)
|
|
|
|
|
*/
|
2014-11-24 20:59:30 +01:00
|
|
|
|
function sanitize($name) {
|
|
|
|
|
return preg_replace("/[^[:alnum:]_.-]/", "_", $name);
|
2013-03-23 18:46:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure the "thumbs" directory exists.
|
2015-07-02 00:08:46 +02:00
|
|
|
|
if (!is_dir('thumbs') && is_writable('.')) {
|
|
|
|
|
mkdir('thumbs', 0700);
|
2014-05-04 20:28:06 +02:00
|
|
|
|
}
|
2013-03-23 18:46:00 +01:00
|
|
|
|
|
|
|
|
|
// Thumbnail file name and path.
|
|
|
|
|
// (We always put thumbnails in jpg for simplification)
|
2015-07-02 00:08:46 +02:00
|
|
|
|
$thumbname = 'thumbs/' . sanitize($get_filename) . '.jpg';
|
2013-03-23 18:46:00 +01:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
if (file_exists($thumbname)) // If thumbnail exists, serve it.
|
2013-03-23 18:46:00 +01:00
|
|
|
|
{
|
2014-05-04 20:28:06 +02:00
|
|
|
|
$fd = fopen($thumbname, "r");
|
2014-11-25 03:09:30 +01:00
|
|
|
|
$cacheContent = fread($fd, filesize($thumbname));
|
2014-05-04 20:28:06 +02:00
|
|
|
|
fclose($fd);
|
|
|
|
|
header('Content-type: image/jpeg');
|
2015-07-02 00:08:46 +02:00
|
|
|
|
echo ($cacheContent);
|
2014-11-25 03:09:30 +01:00
|
|
|
|
exit;
|
2013-03-23 18:46:00 +01:00
|
|
|
|
}
|
2013-03-23 17:35:44 +01:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
// Display error image if file isn't found
|
|
|
|
|
if (!is_file($get_filename)) {
|
|
|
|
|
header('Content-type: image/jpeg');
|
|
|
|
|
$errorimage = imagecreatefromjpeg('images/questionmark.jpg');
|
|
|
|
|
imagejpeg($errorimage, null, 90);
|
|
|
|
|
imagedestroy($errorimage);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
2013-03-23 17:35:44 +01:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
// Display error image if file exists, but can't be opened
|
|
|
|
|
if (!is_readable($get_filename)) {
|
|
|
|
|
header('Content-type: image/jpeg');
|
|
|
|
|
$errorimage = imagecreatefromjpeg('images/cannotopen.jpg');
|
|
|
|
|
imagejpeg($errorimage, null, 90);
|
|
|
|
|
imagedestroy($errorimage);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
2014-05-04 20:28:06 +02:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
// otherwise, generate thumbnail, send it and save it to file.
|
2014-06-04 16:41:21 +02:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
$target = "";
|
|
|
|
|
$xoord = 0;
|
|
|
|
|
$yoord = 0;
|
2013-03-23 17:35:44 +01:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
$imgsize = getimagesize($get_filename);
|
|
|
|
|
$width = $imgsize[0];
|
|
|
|
|
$height = $imgsize[1];
|
2015-07-02 00:08:46 +02:00
|
|
|
|
// If the width is greater than the height it’s a horizontal picture
|
|
|
|
|
if ($width > $height) {
|
|
|
|
|
$xoord = ceil(($width - $height) / 2);
|
|
|
|
|
// Then we read a square frame that equals the width
|
|
|
|
|
$width = $height;
|
|
|
|
|
} else {
|
|
|
|
|
$yoord = ceil(($height - $width) / 2);
|
2014-11-25 03:09:30 +01:00
|
|
|
|
$height = $width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rotate JPG pictures
|
2015-02-23 16:17:21 +01:00
|
|
|
|
// for more info on orientation see
|
|
|
|
|
// http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/
|
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
$degrees = 0;
|
|
|
|
|
$flip = '';
|
2015-02-23 16:17:21 +01:00
|
|
|
|
if (preg_match("/.jpg$|.jpeg$/i", $_GET['filename'])) {
|
|
|
|
|
if (function_exists('exif_read_data') && function_exists('imagerotate')) {
|
2015-07-02 00:08:46 +02:00
|
|
|
|
$exif = exif_read_data($_GET['filename'], 0, true);
|
|
|
|
|
$ort = $exif['IFD0']['Orientation'];
|
|
|
|
|
switch ($ort) {
|
|
|
|
|
case 3: // 180 rotate right
|
|
|
|
|
$degrees = 180;
|
|
|
|
|
break;
|
|
|
|
|
case 6: // 90 rotate right
|
|
|
|
|
$degrees = 270;
|
|
|
|
|
break;
|
|
|
|
|
case 8: // 90 rotate left
|
|
|
|
|
$degrees = 90;
|
|
|
|
|
break;
|
|
|
|
|
case 2: // flip vertical
|
|
|
|
|
$flip = 'vertical';
|
|
|
|
|
break;
|
|
|
|
|
case 7: // flipped
|
|
|
|
|
$degrees = 90;
|
|
|
|
|
$flip = 'vertical';
|
|
|
|
|
break;
|
|
|
|
|
case 5: // flipped
|
|
|
|
|
$degrees = 270;
|
|
|
|
|
$flip = 'vertical';
|
|
|
|
|
break;
|
|
|
|
|
case 4: // flipped
|
|
|
|
|
$degrees = 180;
|
|
|
|
|
$flip = 'vertical';
|
|
|
|
|
break;
|
2015-02-23 16:17:21 +01:00
|
|
|
|
}
|
2014-05-04 20:28:06 +02:00
|
|
|
|
}
|
2014-11-25 03:09:30 +01:00
|
|
|
|
}
|
2014-05-04 20:28:06 +02:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
$target = imagecreatetruecolor($get_size, $get_size);
|
2014-06-04 16:41:21 +02:00
|
|
|
|
|
2015-06-30 15:19:34 +02:00
|
|
|
|
// if the picture can be transparent, add a white background
|
2015-07-02 00:08:46 +02:00
|
|
|
|
if (in_array($get_filename_type, array("GIF", "PNG"))) {
|
2014-11-25 03:09:30 +01:00
|
|
|
|
$backgroundColor = imagecolorallocate($target, 255, 255, 255);
|
|
|
|
|
imagefill($target, 0, 0, $backgroundColor);
|
|
|
|
|
}
|
2014-05-04 20:28:06 +02:00
|
|
|
|
|
2015-07-02 00:08:46 +02:00
|
|
|
|
if ($get_filename_type == "JPG") {
|
|
|
|
|
$source = imagecreatefromjpeg($get_filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($get_filename_type == "GIF") {
|
|
|
|
|
$source = imagecreatefromgif($get_filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($get_filename_type == "PNG") {
|
|
|
|
|
$source = imagecreatefrompng($get_filename);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
imagecopyresampled($target, $source, 0, 0, $xoord, $yoord, $get_size, $get_size, $width, $height);
|
|
|
|
|
imagedestroy($source);
|
2014-06-04 16:41:21 +02:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
//proper rotation by jan niggemann
|
2015-07-02 00:08:46 +02:00
|
|
|
|
if ($degrees != 0) {
|
2014-11-25 03:09:30 +01:00
|
|
|
|
$target = imagerotate($target, $degrees, 0);
|
|
|
|
|
}
|
2014-06-04 16:41:21 +02:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
//proper mirror (aka flip) by jan niggemann
|
2015-07-02 00:08:46 +02:00
|
|
|
|
if ($flip == 'vertical') {
|
2014-11-25 03:09:30 +01:00
|
|
|
|
//only in php >= 5.5.0 ImageJPEG(imageflip($target, IMG_FLIP_VERTICAL),null,80);
|
|
|
|
|
flipVertical($target);
|
|
|
|
|
flipHorizontal($target);
|
|
|
|
|
flipVertical($target);
|
|
|
|
|
}
|
2014-06-04 16:41:21 +02:00
|
|
|
|
|
2014-11-25 03:09:30 +01:00
|
|
|
|
ob_start(); // Start output buffering.
|
|
|
|
|
header('Content-type: image/jpeg'); // We always render the thumbnail in JPEG even if the source is GIF or PNG.
|
|
|
|
|
imagejpeg($target, null, 80);
|
|
|
|
|
imagedestroy($target);
|
|
|
|
|
$cachedImage = ob_get_contents(); // Get the buffer content.
|
|
|
|
|
ob_end_flush(); // End buffering
|
2014-05-04 20:28:06 +02:00
|
|
|
|
|
2015-07-02 00:08:46 +02:00
|
|
|
|
if (is_writable(dirname($thumbname))) {
|
2014-11-25 03:09:30 +01:00
|
|
|
|
$fd = fopen($thumbname, "w"); // Save buffer to disk
|
2015-07-02 00:08:46 +02:00
|
|
|
|
if ($fd) {
|
|
|
|
|
fwrite($fd, $cachedImage);
|
2014-11-25 03:09:30 +01:00
|
|
|
|
fclose($fd);
|
2014-05-04 20:28:06 +02:00
|
|
|
|
}
|
2015-07-02 00:08:46 +02:00
|
|
|
|
}
|