From e0879fd1d9f552db3c12de38940dd2504fa259d9 Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Wed, 11 Mar 2015 00:40:22 +0300 Subject: [PATCH 1/2] performance improvements Signed-off-by: Denis Shulyaka Conflicts: src/createthumb.php --- src/config.php | 1 - src/config_default.php | 6 ++ src/getimage.php | 144 +++++++++++++++++++---------------------- 3 files changed, 72 insertions(+), 79 deletions(-) diff --git a/src/config.php b/src/config.php index 1e7e484..710bfe2 100644 --- a/src/config.php +++ b/src/config.php @@ -45,7 +45,6 @@ //THUMBNAIL CACHING $caching = true; -$ignorecacheonerror = false; //SMALL CACHE $small_enabled = true; diff --git a/src/config_default.php b/src/config_default.php index b278c05..6c6c3ef 100644 --- a/src/config_default.php +++ b/src/config_default.php @@ -42,5 +42,11 @@ $cache_path = "/tmp/imagecache"; //Cache for resized images (thumbnails, previews). 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; +$supported_image_types = array("jpg", "jpeg", "png", "gif"); //List of supported image extensions +$supported_video_types = array("mp4", "mts", "mov", "m4v", "m4a", "aiff", "avi", "caf", "dv", "qtz", "flv"); //List of supported video extensions + +//THUMBNAIL CACHING +$caching = true; + $ffmpegthumbnailer = "/usr/bin/ffmpegthumbnailer"; ?> diff --git a/src/getimage.php b/src/getimage.php index bd651fb..1599c79 100644 --- a/src/getimage.php +++ b/src/getimage.php @@ -25,28 +25,26 @@ include("config.php"); -function rotate_image($filename) { +function rotate_image($filename, $target) { // Rotate JPG pictures - if (preg_match("/\.jpg$|\.jpeg$/i", $filename)) { - if (function_exists('exif_read_data') && function_exists('imagerotate')) { - $exif = exif_read_data($filename); - if (array_key_exists('IFD0', $exif)) { - $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) return imagerotate($target, $degrees, 0); + if (preg_match("/\.jpg$|\.jpeg$/i", $filename) && function_exists('exif_read_data') && function_exists('imagerotate')) { + $exif = exif_read_data($filename); + if (array_key_exists('IFD0', $exif)) { + $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) return imagerotate($target, $degrees, 0); } } -return ""; +return $target; } function create_thumb($filename, $extension, $outfile, $size = 1024, $keepratio = true) { @@ -58,72 +56,76 @@ function create_thumb($filename, $extension, $outfile, $size = 1024, $keepratio $height = $size; $width = $size; + ob_start(); + if ( in_array($extension, $supported_video_types) ) { - if($outfile == null) - passthru ("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o - -s " . escapeshellarg($size) . " -c jpeg -a -f"); - else - exec("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o " . escapeshellarg($outfile) . " -s " . escapeshellarg($size) . " -c jpeg -a -f"); - return; + passthru ("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o - -s " . escapeshellarg($size) . " -c jpeg -f" . ($keepratio? "" : " -a")); } else { - // load source image - if ($extension == "jpg" || $extension == "jpeg") - $source = ImageCreateFromJPEG($filename); - else if ($extension == "gif") - $source = ImageCreateFromGIF($filename); - else if ($extension == "png") - $source = ImageCreateFromPNG($filename); + list($width_orig, $height_orig) = GetImageSize($filename); if ($keepratio) { // 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 { + if ($width_orig > $height_orig) { $height = $width/$ratio_orig; + } else { + $width = $height*$ratio_orig; } - $target = ImageCreatetruecolor($width,$height); - imagecopyresampled($target, $source, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); - } else { + } else { // square thumbnail - $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 + 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-$width)/2); - $height = $width; + $yoord = ceil(($height_orig-$width_orig)/2); + $height_orig = $width_orig; } - $target = ImageCreatetruecolor($size,$size); - imagecopyresampled($target,$source,0,0,$xoord,$yoord,$size,$size,$width,$height); } - imagedestroy($source); - - if ($extension == "jpg" || $extension == "jpeg") - ImageJPEG($target,$outfile,90); - else if ($extension == "gif") - ImageGIF($target,$outfile,90); - else if ($extension == "png") - ImageJPEG($target,$outfile,90); // Using ImageJPEG on purpose - imagedestroy($target); + + if($keepratio && $size > $height_orig && $size > $width_orig) { + readfile($filename); + $outfile = null; //don't cache images that are equal to originals + } else { + // load source image + if ($extension == "jpg" || $extension == "jpeg") + $source = ImageCreateFromJPEG($filename); + else if ($extension == "gif") + $source = ImageCreateFromGIF($filename); + else if ($extension == "png") + $source = ImageCreateFromPNG($filename); + + $target = ImageCreatetruecolor($width,$height); + imagecopyresampled($target,$source, 0,0, $xoord,$yoord, $width,$height, $width_orig,$height_orig); + imagedestroy($source); + + if ($extension == "jpg" || $extension == "jpeg") + ImageJPEG($target,null,90); + else if ($extension == "gif") + ImageGIF($target,null,90); + else if ($extension == "png") + ImageJPEG($target,null,90); // Using ImageJPEG on purpose + imagedestroy($target); + } } + + if($outfile) + file_put_contents($outfile,ob_get_contents()); + + ob_end_flush(); } $_GET['filename'] = "./" . $_GET['filename']; $_GET['size']=filter_var($_GET['size'], FILTER_VALIDATE_INT); -if ($_GET['size'] == false) $_GET['size'] = 120; +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); - imagedestroy($errorimg); + imagedestroy($errorimage); exit; } @@ -132,7 +134,7 @@ function create_thumb($filename, $extension, $outfile, $size = 1024, $keepratio header('Content-type: image/jpeg'); $errorimage = ImageCreateFromJPEG('images/cannotopen.jpg'); ImageJPEG($errorimage,null,90); - imagedestroy($errorimg); + imagedestroy($errorimage); exit; } @@ -149,7 +151,7 @@ function create_thumb($filename, $extension, $outfile, $size = 1024, $keepratio header('Content-type: image/jpeg'); $errorimage = ImageCreateFromJPEG('images/cannotopen.jpg'); ImageJPEG($errorimage,null,90); - imagedestroy($errorimg); + imagedestroy($errorimage); exit; } @@ -159,24 +161,10 @@ function create_thumb($filename, $extension, $outfile, $size = 1024, $keepratio if(!file_exists($cache_path) && $caching) mkdir($cache_path); -if (!is_file($thumbnail) && $caching) { - create_thumb($_GET['filename'], $extension, $thumbnail, $_GET['size'], ($_GET['format'] != 'square')); -} - -if ( $cleanext == 'gif') { - $img = ImageCreateFromGIF($thumbnail); - if(!$img) { - create_thumb($_GET['filename'], $extension, null, $_GET['size']); - exit; - } - ImageGIF($img,null,90); +if (is_file($thumbnail) && $caching) { + readfile($thumbnail); } else { - $img = ImageCreateFromJPEG($thumbnail); - if(!$img) { - create_thumb($_GET['filename'], $extension, null, $_GET['size']); - exit; - } - ImageJPEG($img,null,90); + create_thumb($_GET['filename'], $extension, $caching ? $thumbnail : null, $_GET['size'], ($_GET['format'] != 'square')); } -imagedestroy($img); + ?> From 47a45332a8f3eb10703e6d1e1b17ed29b5720652 Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Wed, 11 Mar 2015 17:09:41 +0300 Subject: [PATCH 2/2] restore image names (regression) Conflicts: src/index.php --- src/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.php b/src/index.php index c408f0e..3217cb9 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 = "";