From 033587571c729d11acffc23498918d8783a4473e Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Tue, 10 Mar 2015 23:44:49 +0300 Subject: [PATCH 1/4] merged createsmall.php script into createthumb.php Signed-off-by: Denis Shulyaka --- src/config_default.php | 1 - src/createsmall.php | 134 ----------------------------------------- src/createthumb.php | 42 +++++++++---- src/index.php | 8 +-- 4 files changed, 33 insertions(+), 152 deletions(-) delete mode 100644 src/createsmall.php diff --git a/src/config_default.php b/src/config_default.php index f6c3363..97d62ae 100644 --- a/src/config_default.php +++ b/src/config_default.php @@ -40,7 +40,6 @@ $thumb_size = 120; //Thumbnail height/width (square thumbs). Changing this will most likely require manual altering of the template file to make it look properly! $small_size = 1024; //Size (width) of the images displayed in the gallery. Images are scaled proportionally $thumb_path = "/tmp/thumbnails"; //Thumbnail path. Must be writable by the httpd user. -$small_path = "/tmp/small"; //Preview image path. Must be writable by the httpd user. $label_max_length = 30; //Maximum chars of a folder name that will be displayed on the folder thumbnail $display_exif = 0; ?> diff --git a/src/createsmall.php b/src/createsmall.php deleted file mode 100644 index 1946a29..0000000 --- a/src/createsmall.php +++ /dev/null @@ -1,134 +0,0 @@ - -*/ -// error_reporting(E_ALL); - -require("config_default.php"); -include("config.php"); - -function create_thumb($filename, $outfile, $size = 1024) { - // Define variables - $target = ""; - $height = $size; - $width = $size; - - // Get new dimensions - list($width_orig, $height_orig) = getimagesize($filename); - - $ratio_orig = $width_orig/$height_orig; - - if ($width/$height > $ratio_orig) { - $width = $height*$ratio_orig; - } else { - $height = $width/$ratio_orig; - } - - // Rotate JPG pictures - if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) { - if (function_exists('exif_read_data') && function_exists('imagerotate')) { - $exif = exif_read_data($filename); - $ort = $exif['IFD0']['Orientation']; - $degrees = 0; - switch($ort) { - case 6: // 90 rotate right - $degrees = 270; - break; - case 8: // 90 rotate left - $degrees = 90; - break; - } - if ($degrees != 0) $target = imagerotate($target, $degrees, 0); - } - } - - $target = ImageCreatetruecolor($width,$height); - if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) $source = ImageCreateFromJPEG($filename); - if (preg_match("/\.gif$/i", $filename)) $source = ImageCreateFromGIF($filename); - if (preg_match("/\.png$/i", $filename)) $source = ImageCreateFromPNG($filename); - imagecopyresampled($target, $source, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); - imagedestroy($source); - - if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) ImageJPEG($target,$outfile,90); - if (preg_match("/\.gif$/i", $filename)) ImageGIF($target,$outfile,90); - if (preg_match("/\.png$/i", $filename)) ImageJPEG($target,$outfile,90); // Using ImageJPEG on purpose - imagedestroy($target); -} - -$_GET['filename'] = "./" . $_GET['filename']; -$_GET['size']=filter_var($_GET['size'], FILTER_VALIDATE_INT); -if ($_GET['size'] == false) $_GET['size'] = 1024; - -// Display error image if file isn't found -if (preg_match("/\.\.\//i", $_GET['filename']) || !is_file($_GET['filename'])) { - header('Content-type: image/jpeg'); - $errorimage = ImageCreateFromJPEG('images/questionmark.jpg'); - ImageJPEG($errorimage,null,90); -} - -// 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) { - header('Content-type: image/jpeg'); - $errorimage = ImageCreateFromJPEG('images/cannotopen.jpg'); - ImageJPEG($errorimage,null,90); -} - -$extension = preg_replace('.*\.\w*$', '', $_GET['filename']); - -if (preg_match("/\.jpg$|\.jpeg|\.png$$/i", $_GET['filename'])) { - header('Content-type: image/jpeg'); - $cleanext = 'jpeg'; -} -if (preg_match("/\.gif$/i", $_GET['filename'])) { - header('Content-type: image/gif'); - $cleanext = 'gif'; -} - -// Create paths for different picture versions -$md5sum = md5($_GET['filename']); -$small = $small_path . "/" . $md5sum . "_" . $_GET['size'] . "." . $cleanext; -if(!file_exists($small_path)) - mkdir($small_path); - - -if (!is_file($small)) { - create_thumb($_GET['filename'], $small, $_GET['size']); -} - -if ( $cleanext == 'jpeg') { - $img = ImageCreateFromJPEG($small); - if(!$img) { - create_thumb($_GET['filename'], null, $_GET['size']); - exit; - } - ImageJPEG($img,null,90); -} else if ( $cleanext == 'gif') { - $img = ImageCreateFromGIF($small); - if(!$img) { - create_thumb($_GET['filename'], null, $_GET['size']); - exit; - } - ImageGIF($img,null,90); -} else { - $img = ImageCreateFromJPEG('images/cannotopen.jpg'); - ImageJPEG($img,null,90); -} -imagedestroy($img); -?> diff --git a/src/createthumb.php b/src/createthumb.php index 969b70d..1189485 100644 --- a/src/createthumb.php +++ b/src/createthumb.php @@ -29,24 +29,41 @@ function create_thumb($filename, $outfile, $size = 120) { $target = ""; $xoord = 0; $yoord = 0; + $height = $size; + $width = $size; if (preg_match("/\.mp4$|\.mts$|\.mov$|\.m4v$|\.m4a$|\.aiff$|\.avi$|\.caf$|\.dv$|\.qtz$|\.flv$/i", $filename)) { if($outfile == null) - passthru ("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o - -s " . escapeshellarg($size) . " -c jpeg -a -f"); + passthru ("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o - -s " . escapeshellarg($size) . " -c jpeg -f" . ($size<=512?" -a" : "")); else - exec("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o " . escapeshellarg($outfile) . " -s " . escapeshellarg($size) . " -c jpeg -a -f"); + exec("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o " . escapeshellarg($outfile) . " -s " . escapeshellarg($size) . " -c jpeg -f" . ($size<=512?" -a" : "")); return; } - $imgsize = GetImageSize($filename); - $width = $imgsize[0]; - $height = $imgsize[1]; - if ($width > $height) { // If the width is greater than the height it’s a horizontal picture - $xoord = ceil(($width-$height)/2); - $width = $height; // Then we read a square frame that equals the width + list($width_orig, $height_orig) = GetImageSize($filename); + + if($size<=512) { + if ($width_orig > $height_orig) { // If the width is greater than the height it’s a horizontal picture + $xoord = ceil(($width_orig-$height_orig)/2); + $width_orig = $height_orig; // Then we read a square frame that equals the width + } else { + $yoord = ceil(($height_orig-$width_orig)/2); + $height_orig = $width_orig; + } } else { - $yoord = ceil(($height-$width)/2); - $height = $width; + $ratio_orig = $width_orig/$height_orig; + + if ($width_orig > $height_orig) { + $height = $width/$ratio_orig; + } else { + $width = $height*$ratio_orig; + } + + if($height>=$height_orig) { + //todo: Return the original image file unaltered + $height = $height_orig; + $width = $width_orig; + } } // Rotate JPG pictures @@ -67,11 +84,11 @@ function create_thumb($filename, $outfile, $size = 120) { } } - $target = ImageCreatetruecolor($size,$size); + $target = ImageCreatetruecolor($width,$height); if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) $source = ImageCreateFromJPEG($filename); if (preg_match("/\.gif$/i", $filename)) $source = ImageCreateFromGIF($filename); if (preg_match("/\.png$/i", $filename)) $source = ImageCreateFromPNG($filename); - imagecopyresampled($target,$source,0,0,$xoord,$yoord,$size,$size,$width,$height); + imagecopyresampled($target,$source,0,0,$xoord,$yoord,$width,$height,$width_orig, $height_orig); imagedestroy($source); if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) ImageJPEG($target,$outfile,90); @@ -103,7 +120,6 @@ function create_thumb($filename, $outfile, $size = 120) { exit; } -// $extension = preg_replace('.*\.\w*$', '', $_GET['filename']); if (preg_match("/.gif$/i", $_GET['filename'])) { header('Content-type: image/gif'); diff --git a/src/index.php b/src/index.php index c9ea980..2794083 100644 --- a/src/index.php +++ b/src/index.php @@ -207,7 +207,7 @@ function checkpermissions($file) { // JPG, GIF and PNG if (preg_match("/.jpg$|.gif$|.png$/i", $file)) { - $img_captions[$file] .= "small \n"; + $img_captions[$file] .= "small \n"; $img_captions[$file] .= "original\n"; //Read EXIF if ($display_exif == 1) $img_captions[$file] .= "
" .readEXIF($currentdir . "/" . $file); @@ -216,7 +216,7 @@ function checkpermissions($file) { "name" => $file, "date" => filemtime($currentdir . "/" . $file), "size" => filesize($currentdir . "/" . $file), - "html" => "
  • $label_loading
  • "); + "html" => "
  • $label_loading
  • "); } // MP4 else if (preg_match("/.mp4$/i", $file)) @@ -340,7 +340,7 @@ function checkpermissions($file) { //Include hidden links for all images BEFORE current page so lightbox is able to browse images on different pages for ($y = 0; $y < $offset_start - sizeof($dirs); $y++) { - $breadcrumb_navigation .= ""; + $breadcrumb_navigation .= ""; } //----------------------- @@ -372,7 +372,7 @@ function checkpermissions($file) { //Include hidden links for all images AFTER current page so lightbox is able to browse images on different pages for ($y = $i; $y < sizeof($files); $y++) { - $page_navigation .= ""; + $page_navigation .= ""; } //----------------------- From 4c298fca243c9f1f20eaa4d7892929b9b2e4858b Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Tue, 10 Mar 2015 23:59:23 +0300 Subject: [PATCH 2/4] avoid double compression of thumbnails Signed-off-by: Denis Shulyaka --- src/createthumb.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/createthumb.php b/src/createthumb.php index 1189485..7537423 100644 --- a/src/createthumb.php +++ b/src/createthumb.php @@ -141,10 +141,13 @@ function create_thumb($filename, $outfile, $size = 120) { if(!file_exists($thumb_path)) mkdir($thumb_path); -if (!is_file($thumbnail)) { - create_thumb($_GET['filename'], $thumbnail, $_GET['size']); +if (is_file($thumbnail)) { + readfile($thumbnail); //Use the cache + exit; } +create_thumb($_GET['filename'], $thumbnail, $_GET['size']); + if ( $cleanext == 'gif') { $img = ImageCreateFromGIF($thumbnail); if(!$img) { From 06ee4f4d8d309a2bfc1630e81a1813a99229bfe1 Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Wed, 11 Mar 2015 00:40:22 +0300 Subject: [PATCH 3/4] performance improvements Signed-off-by: Denis Shulyaka --- src/createthumb.php | 135 ++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 75 deletions(-) diff --git a/src/createthumb.php b/src/createthumb.php index 7537423..33238c2 100644 --- a/src/createthumb.php +++ b/src/createthumb.php @@ -31,70 +31,73 @@ function create_thumb($filename, $outfile, $size = 120) { $yoord = 0; $height = $size; $width = $size; + $thumbthreshold = 512; - if (preg_match("/\.mp4$|\.mts$|\.mov$|\.m4v$|\.m4a$|\.aiff$|\.avi$|\.caf$|\.dv$|\.qtz$|\.flv$/i", $filename)) { - if($outfile == null) - passthru ("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o - -s " . escapeshellarg($size) . " -c jpeg -f" . ($size<=512?" -a" : "")); - else - exec("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o " . escapeshellarg($outfile) . " -s " . escapeshellarg($size) . " -c jpeg -f" . ($size<=512?" -a" : "")); - return; - } - - list($width_orig, $height_orig) = GetImageSize($filename); + ob_start(); - if($size<=512) { - if ($width_orig > $height_orig) { // If the width is greater than the height it’s a horizontal picture - $xoord = ceil(($width_orig-$height_orig)/2); - $width_orig = $height_orig; // Then we read a square frame that equals the width - } else { - $yoord = ceil(($height_orig-$width_orig)/2); - $height_orig = $width_orig; - } + if (preg_match("/\.mp4$|\.mts$|\.mov$|\.m4v$|\.m4a$|\.aiff$|\.avi$|\.caf$|\.dv$|\.qtz$|\.flv$/i", $filename)) { + passthru ("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o - -s " . escapeshellarg($size) . " -c jpeg -f" . ($size<=$thumbthreshold?" -a" : "")); } else { - $ratio_orig = $width_orig/$height_orig; - - if ($width_orig > $height_orig) { - $height = $width/$ratio_orig; + list($width_orig, $height_orig) = GetImageSize($filename); + + if($size<=$thumbthreshold) { + if ($width_orig > $height_orig) { // If the width is greater than the height it’s a horizontal picture + $xoord = ceil(($width_orig-$height_orig)/2); + $width_orig = $height_orig; // Then we read a square frame that equals the width + } else { + $yoord = ceil(($height_orig-$width_orig)/2); + $height_orig = $width_orig; + } } else { - $width = $height*$ratio_orig; - } + $ratio_orig = $width_orig/$height_orig; - if($height>=$height_orig) { - //todo: Return the original image file unaltered - $height = $height_orig; - $width = $width_orig; + if ($width_orig > $height_orig) { + $height = $width/$ratio_orig; + } else { + $width = $height*$ratio_orig; + } } - } - // Rotate JPG pictures - if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) { - if (function_exists('exif_read_data') && function_exists('imagerotate')) { - $exif = exif_read_data($filename); - $ort = $exif['IFD0']['Orientation']; - $degrees = 0; - switch($ort) { - case 6: // 90 rotate right - $degrees = 270; - break; - case 8: // 90 rotate left - $degrees = 90; - break; + if($size > $thumbthreshold && $size > $height_orig && $size > $width_orig) { + readfile($filename); + $outfile = null; + } else { + // Rotate JPG pictures + if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) { + if (function_exists('exif_read_data') && function_exists('imagerotate')) { + $exif = exif_read_data($filename); + $ort = $exif['IFD0']['Orientation']; + $degrees = 0; + switch($ort) { + case 6: // 90 rotate right + $degrees = 270; + break; + case 8: // 90 rotate left + $degrees = 90; + break; + } + if ($degrees != 0) $target = imagerotate($target, $degrees, 0); + } } - if ($degrees != 0) $target = imagerotate($target, $degrees, 0); + + $target = ImageCreatetruecolor($width,$height); + if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) $source = ImageCreateFromJPEG($filename); + if (preg_match("/\.gif$/i", $filename)) $source = ImageCreateFromGIF($filename); + if (preg_match("/\.png$/i", $filename)) $source = ImageCreateFromPNG($filename); + imagecopyresampled($target,$source,0,0,$xoord,$yoord,$width,$height,$width_orig, $height_orig); + imagedestroy($source); + + if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) ImageJPEG($target,null,90); + if (preg_match("/\.gif$/i", $filename)) ImageGIF($target,null,90); + if (preg_match("/\.png$/i", $filename)) ImageJPEG($target,null,90); // Using ImageJPEG on purpose + imagedestroy($target); } } - $target = ImageCreatetruecolor($width,$height); - if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) $source = ImageCreateFromJPEG($filename); - if (preg_match("/\.gif$/i", $filename)) $source = ImageCreateFromGIF($filename); - if (preg_match("/\.png$/i", $filename)) $source = ImageCreateFromPNG($filename); - imagecopyresampled($target,$source,0,0,$xoord,$yoord,$width,$height,$width_orig, $height_orig); - imagedestroy($source); - - if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) ImageJPEG($target,$outfile,90); - if (preg_match("/\.gif$/i", $filename)) ImageGIF($target,$outfile,90); - if (preg_match("/\.png$/i", $filename)) ImageJPEG($target,$outfile,90); // Using ImageJPEG on purpose - imagedestroy($target); + if($outfile) + file_put_contents($outfile,ob_get_contents()); + + ob_end_flush(); } @@ -107,7 +110,7 @@ function create_thumb($filename, $outfile, $size = 120) { header('Content-type: image/jpeg'); $errorimage = ImageCreateFromJPEG('images/questionmark.jpg'); ImageJPEG($errorimage,null,90); - imagedestroy($errorimg); + imagedestroy($errorimage); exit; } @@ -116,7 +119,7 @@ function create_thumb($filename, $outfile, $size = 120) { header('Content-type: image/jpeg'); $errorimage = ImageCreateFromJPEG('images/cannotopen.jpg'); ImageJPEG($errorimage,null,90); - imagedestroy($errorimg); + imagedestroy($errorimage); exit; } @@ -131,7 +134,7 @@ function create_thumb($filename, $outfile, $size = 120) { header('Content-type: image/jpeg'); $errorimage = ImageCreateFromJPEG('images/cannotopen.jpg'); ImageJPEG($errorimage,null,90); - imagedestroy($errorimg); + imagedestroy($errorimage); exit; } @@ -143,25 +146,7 @@ function create_thumb($filename, $outfile, $size = 120) { if (is_file($thumbnail)) { readfile($thumbnail); //Use the cache - exit; -} - -create_thumb($_GET['filename'], $thumbnail, $_GET['size']); - -if ( $cleanext == 'gif') { - $img = ImageCreateFromGIF($thumbnail); - if(!$img) { - create_thumb($_GET['filename'], null, $_GET['size']); - exit; - } - ImageGIF($img,null,90); } else { - $img = ImageCreateFromJPEG($thumbnail); - if(!$img) { - create_thumb($_GET['filename'], null, $_GET['size']); - exit; - } - ImageJPEG($img,null,90); + create_thumb($_GET['filename'], $thumbnail, $_GET['size']); //Or create a new thumbnail, write into file and output it simultaneously } -imagedestroy($img); ?> From e29cdfc8218b488d68edcbe9e87cf2077758cb7d Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Wed, 11 Mar 2015 17:09:41 +0300 Subject: [PATCH 4/4] restore image names (regression) --- src/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.php b/src/index.php index 2794083..5496153 100644 --- a/src/index.php +++ b/src/index.php @@ -216,7 +216,7 @@ function checkpermissions($file) { "name" => $file, "date" => filemtime($currentdir . "/" . $file), "size" => filesize($currentdir . "/" . $file), - "html" => "
  • $label_loading
  • "); + "html" => "
  • $label_loading" . padstring($file, $label_max_length) . "
  • "); } // MP4 else if (preg_match("/.mp4$/i", $file)) @@ -227,7 +227,7 @@ function checkpermissions($file) { "name" => $file, "date" => filemtime($currentdir . "/" . $file), "size" => filesize($currentdir . "/" . $file), - "html" => "
  • $label_loading
  • "); + "html" => "
  • $label_loading" . padstring($file, $label_max_length) . "
  • "); } // Other filetypes $extension = "";