Merge pull request #70 from bifek/cleangetvars
simplify $_GET usage (move to beginning for easy escape if need be).
This commit is contained in:
commit
14cdaf7dd1
1 changed files with 118 additions and 141 deletions
117
createthumb.php
117
createthumb.php
|
@ -20,15 +20,18 @@ Version 0.3.5 modified by Sebastien SAUVAGE (sebsauvage.net):
|
||||||
|
|
||||||
USAGE EXAMPLE:
|
USAGE EXAMPLE:
|
||||||
File: createthumb.php
|
File: createthumb.php
|
||||||
Example: <img src="createthumb.php?filename=photo.jpg&width=100&height=100">
|
Example: <img src="createthumb.php?filename=photo.jpg&size=100">
|
||||||
*/
|
*/
|
||||||
// error_reporting(E_ALL);
|
|
||||||
error_reporting(0);
|
error_reporting(0);
|
||||||
/*
|
|
||||||
if (preg_match("/.jpg$|.jpeg$/i", $_GET['filename'])) header('Content-type: image/jpeg');
|
$get_filename = $_GET['filename'];
|
||||||
if (preg_match("/.gif$/i", $_GET['filename'])) header('Content-type: image/gif');
|
$get_size = @$_GET['size'];
|
||||||
if (preg_match("/.png$/i", $_GET['filename'])) header('Content-type: image/png');
|
if (empty($get_size)) $get_size = 120;
|
||||||
*/
|
|
||||||
|
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";
|
||||||
|
|
||||||
// flip functions from http://stackoverflow.com/questions/8232722/how-to-flip-part-of-an-image-horizontal-with-php-gd
|
// flip functions from http://stackoverflow.com/questions/8232722/how-to-flip-part-of-an-image-horizontal-with-php-gd
|
||||||
function flipVertical(&$img) {
|
function flipVertical(&$img) {
|
||||||
|
@ -90,7 +93,7 @@ if (!is_dir('thumbs') && is_writable('.'))
|
||||||
|
|
||||||
// Thumbnail file name and path.
|
// Thumbnail file name and path.
|
||||||
// (We always put thumbnails in jpg for simplification)
|
// (We always put thumbnails in jpg for simplification)
|
||||||
$thumbname = 'thumbs/'.sanitize($_GET['filename']).'.jpg';
|
$thumbname = 'thumbs/'.sanitize($get_filename).'.jpg';
|
||||||
|
|
||||||
if (file_exists($thumbname)) // If thumbnail exists, serve it.
|
if (file_exists($thumbname)) // If thumbnail exists, serve it.
|
||||||
{
|
{
|
||||||
|
@ -99,34 +102,34 @@ if (file_exists($thumbname))// If thumbnail exists, serve it.
|
||||||
fclose($fd);
|
fclose($fd);
|
||||||
header('Content-type: image/jpeg');
|
header('Content-type: image/jpeg');
|
||||||
echo($cacheContent);
|
echo($cacheContent);
|
||||||
|
exit;
|
||||||
}
|
}
|
||||||
else // otherwise, generate thumbnail, send it and save it to file.
|
|
||||||
{
|
|
||||||
|
|
||||||
// Display error image if file isn't found
|
// Display error image if file isn't found
|
||||||
if (!is_file($_GET['filename'])) {
|
if (!is_file($get_filename)) {
|
||||||
header('Content-type: image/jpeg');
|
header('Content-type: image/jpeg');
|
||||||
$errorimage = ImageCreateFromJPEG('images/questionmark.jpg');
|
$errorimage = imagecreatefromjpeg('images/questionmark.jpg');
|
||||||
ImageJPEG($errorimage,null,90);
|
imagejpeg($errorimage, null, 90);
|
||||||
|
imagedestroy($errorimage);
|
||||||
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display error image if file exists, but can't be opened
|
// Display error image if file exists, but can't be opened
|
||||||
if (substr(decoct(fileperms($_GET['filename'])), -1, strlen(fileperms($_GET['filename']))) < 4 OR substr(decoct(fileperms($_GET['filename'])), -3,1) < 4) {
|
if (!is_readable($get_filename)) {
|
||||||
header('Content-type: image/jpeg');
|
header('Content-type: image/jpeg');
|
||||||
$errorimage = ImageCreateFromJPEG('images/cannotopen.jpg');
|
$errorimage = imagecreatefromjpeg('images/cannotopen.jpg');
|
||||||
ImageJPEG($errorimage,null,90);
|
imagejpeg($errorimage, null, 90);
|
||||||
|
imagedestroy($errorimage);
|
||||||
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define variables
|
// otherwise, generate thumbnail, send it and save it to file.
|
||||||
|
|
||||||
$target = "";
|
$target = "";
|
||||||
$xoord = 0;
|
$xoord = 0;
|
||||||
$yoord = 0;
|
$yoord = 0;
|
||||||
|
|
||||||
if ($_GET['size'] == "")
|
$imgsize = getimagesize($get_filename);
|
||||||
{
|
|
||||||
$_GET['size'] = 120;
|
|
||||||
}
|
|
||||||
$imgsize = GetImageSize($_GET['filename']);
|
|
||||||
$width = $imgsize[0];
|
$width = $imgsize[0];
|
||||||
$height = $imgsize[1];
|
$height = $imgsize[1];
|
||||||
if ($width > $height) // If the width is greater than the height it’s a horizontal picture
|
if ($width > $height) // If the width is greater than the height it’s a horizontal picture
|
||||||
|
@ -140,78 +143,49 @@ else // otherwise, generate thumbnail, send it and save it to file.
|
||||||
$height = $width;
|
$height = $width;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rotate JPG pictures
|
||||||
$degrees = 0;
|
$degrees = 0;
|
||||||
$flip = '';
|
$flip = '';
|
||||||
// Rotate JPG pictures
|
if ($get_filename_type == "JPG")
|
||||||
if (preg_match("/.jpg$|.jpeg$/i", $_GET['filename']))
|
|
||||||
{
|
{
|
||||||
if (function_exists('exif_read_data') && function_exists('imagerotate'))
|
if (function_exists('exif_read_data') && function_exists('imagerotate'))
|
||||||
{
|
{
|
||||||
$exif = exif_read_data($_GET['filename'], 0, true);
|
$exif = exif_read_data($get_filename, 0, true);
|
||||||
if (isset($exif['IFD0']) && isset($exif['IFD0']['Orientation']))
|
if (isset($exif['IFD0']) && isset($exif['IFD0']['Orientation']))
|
||||||
$ort = $exif['IFD0']['Orientation'];
|
$ort = $exif['IFD0']['Orientation'];
|
||||||
else
|
else
|
||||||
$ort = 0;
|
$ort = 0;
|
||||||
switch($ort)
|
// for more info on orientation see
|
||||||
{
|
// http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/
|
||||||
case 3: // 180 rotate right
|
$ort2deg = array(3=>180, 4=>180, 5=>270, 6=>270, 7=>90, 8=>90);
|
||||||
$degrees = 180;
|
$degrees = in_array($ort, $ort2deg) ? $ort2deg[$ort] : 0;
|
||||||
break;
|
$ort2flip = array(2, 4, 5, 7);
|
||||||
case 6: // 90 rotate right
|
$flip = in_array($ort, $flip2deg) ? 'vertical' : '';
|
||||||
$degrees = 270;
|
|
||||||
break;
|
|
||||||
case 8: // 90 rotate left
|
|
||||||
$degrees = 90;
|
|
||||||
break;
|
|
||||||
case 2: // flip vertical
|
|
||||||
$flip = 'vertical';
|
|
||||||
break;
|
|
||||||
// see http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/ for more info on orientation
|
|
||||||
case 7: // flipped
|
|
||||||
$degrees = 90;
|
|
||||||
$flip = 'vertical';
|
|
||||||
break;
|
|
||||||
case 5: // flipped
|
|
||||||
$degrees = 270;
|
|
||||||
$flip = 'vertical';
|
|
||||||
break;
|
|
||||||
case 4: // flipped
|
|
||||||
$degrees = 180;
|
|
||||||
$flip = 'vertical';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$target = ImageCreatetruecolor($_GET['size'],$_GET['size']);
|
$target = imagecreatetruecolor($get_size, $get_size);
|
||||||
|
|
||||||
// if the picture can be transparent, add a white background instead a black
|
// if the picture can be transparent, add a white background instead a black
|
||||||
if (preg_match("/.gif$/i", $_GET['filename']) || preg_match("/.png$/i", $_GET['filename']))
|
if (in_array($get_filename_type, array("GIF", "PNG")))
|
||||||
{
|
{
|
||||||
$backgroundColor = imagecolorallocate($target, 255, 255, 255);
|
$backgroundColor = imagecolorallocate($target, 255, 255, 255);
|
||||||
imagefill($target, 0, 0, $backgroundColor);
|
imagefill($target, 0, 0, $backgroundColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (preg_match("/.jpg$/i", $_GET['filename'])) $source = ImageCreateFromJPEG($_GET['filename']);
|
if ($get_filename_type == "JPG") $source = imagecreatefromjpeg($get_filename);
|
||||||
if (preg_match("/.gif$/i", $_GET['filename'])) $source = ImageCreateFromGIF($_GET['filename']);
|
if ($get_filename_type == "GIF") $source = imagecreatefromgif($get_filename);
|
||||||
if (preg_match("/.png$/i", $_GET['filename'])) $source = ImageCreateFromPNG($_GET['filename']);
|
if ($get_filename_type == "PNG") $source = imagecreatefrompng($get_filename);
|
||||||
imagecopyresampled($target,$source,0,0,$xoord,$yoord,$_GET['size'],$_GET['size'],$width,$height);
|
imagecopyresampled($target, $source, 0, 0, $xoord, $yoord, $get_size, $get_size, $width, $height);
|
||||||
imagedestroy($source);
|
imagedestroy($source);
|
||||||
|
|
||||||
//if (preg_match("/.jpg$/i", $_GET['filename'])) ImageJPEG($target,null,90);
|
|
||||||
//if (preg_match("/.gif$/i", $_GET['filename'])) ImageGIF($target,null,90);
|
|
||||||
//if (preg_match("/.png$/i", $_GET['filename'])) ImageJPEG($target,null,90); // Using ImageJPEG on purpose
|
|
||||||
|
|
||||||
//proper rotation by jan niggemann
|
//proper rotation by jan niggemann
|
||||||
if ($degrees != 0)
|
if ($degrees != 0)
|
||||||
{
|
{
|
||||||
$target = imagerotate($target, $degrees, 0);
|
$target = imagerotate($target, $degrees, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
//proper mirror (aka flip) by jan niggemann
|
//proper mirror (aka flip) by jan niggemann
|
||||||
if ($flip == 'vertical')
|
if ($flip == 'vertical')
|
||||||
{
|
{
|
||||||
|
@ -220,13 +194,16 @@ else // otherwise, generate thumbnail, send it and save it to file.
|
||||||
flipHorizontal($target);
|
flipHorizontal($target);
|
||||||
flipVertical($target);
|
flipVertical($target);
|
||||||
}
|
}
|
||||||
ImageJPEG($target,null,80);
|
|
||||||
|
|
||||||
|
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);
|
imagedestroy($target);
|
||||||
|
|
||||||
$cachedImage = ob_get_contents(); // Get the buffer content.
|
$cachedImage = ob_get_contents(); // Get the buffer content.
|
||||||
ob_end_flush(); // End buffering
|
ob_end_flush(); // End buffering
|
||||||
if (is_writable(dirname($thumbname))) {
|
|
||||||
|
if (is_writable(dirname($thumbname)))
|
||||||
|
{
|
||||||
$fd = fopen($thumbname, "w"); // Save buffer to disk
|
$fd = fopen($thumbname, "w"); // Save buffer to disk
|
||||||
if ($fd)
|
if ($fd)
|
||||||
{
|
{
|
||||||
|
@ -234,6 +211,6 @@ else // otherwise, generate thumbnail, send it and save it to file.
|
||||||
fclose($fd);
|
fclose($fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
Loading…
Reference in a new issue