Skip to content

Commit

Permalink
Merge pull request #13 from Shulyaka/config_i18n_fix
Browse files Browse the repository at this point in the history
Performance improvements
  • Loading branch information
gentoomaniac committed Mar 12, 2015
2 parents 89dd3e0 + 2bf871a commit 064b700
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 59 deletions.
119 changes: 62 additions & 57 deletions src/getimage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,26 @@
require("config.php");
ini_set("memory_limit",$config['memory_limit']);

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) {
Expand All @@ -61,72 +59,74 @@ function create_thumb($filename, $extension, $outfile, $size = 1024, $keepratio
$height = $size;
$width = $size;

if (is_file($outfile)) {
if ($config['caching'] && is_file($outfile)) {
readfile($outfile); //Use the cache
return;
}

ob_start();

if ( in_array($extension, $config['supported_video_types']) ) {
passthru ("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o - -s " . escapeshellarg($size) . " -c jpeg -f" . ($size<=$thumbthreshold?" -a" : ""));
// Video thumbnail
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);
// Image thumbnail
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" || $extension == "png")
ImageJPEG($target,null,90);
else if ($extension == "gif")
ImageGIF($target,null,90);
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" || $extension == "png")
ImageJPEG($target,null,90);
else if ($extension == "gif")
ImageGIF($target,null,90);
imagedestroy($target);
}
}

if($config['caching']){
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'])) {
Expand Down Expand Up @@ -164,10 +164,15 @@ function create_thumb($filename, $extension, $outfile, $size = 1024, $keepratio
}

// Create paths for different picture versions
$md5sum = md5($_GET['filename']);
$thumbnail = $config['cache_path'] . "/" . $md5sum . "_" . $_GET['size'] . "." . $cleanext;
if(!file_exists($config['cache_path']) && $config['caching'])
mkdir($config['cache_path']);
$thumbnail = null;

if($config['caching']) {
$md5sum = md5($_GET['filename']);
$thumbnail = $config['cache_path'] . "/" . $md5sum . "_" . $_GET['size'] . "." . $cleanext;
if(!file_exists($config['cache_path']))
mkdir($config['cache_path']);
}

create_thumb($_GET['filename'], $extension, $thumbnail, $_GET['size'], ($_GET['format'] != 'square'));

?>
4 changes: 2 additions & 2 deletions src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function checkpermissions($file) {
$config['small_size'] . "' rel='lightbox[billeder]' title='" . $img_captions[$file] .
"'><span></span><img src='" . GALLERY_ROOT . "getimage.php?filename=" . $thumbdir .
"/" . $file . "&amp;size=" . $config['thumb_size'] . "&amp;format=square' alt='" .
$i18n['label_loading'] . "' /></a></li>"
$i18n['label_loading'] . "' /></a><em>" . padstring($file, $label_max_length) . "</em></li>"
);

} else if (in_array($extension, $config['supported_video_types'])) {
Expand All @@ -242,7 +242,7 @@ function checkpermissions($file) {
"html" => "<li><a href='" . $currentdir . "/" . $file . "' rel='lightbox[billeder]' title='" .
$img_captions[$file]."'><span></span><img src='" . GALLERY_ROOT . "getimage.php?filename=" .
$thumbdir . "/" . $file . "&amp;size=" . $config['thumb_size'] . "&amp;format=square' alt='" .
$i18n['label_loading'] . "' /></a></li>"
$i18n['label_loading'] . "' /></a><em>" . padstring($file, $label_max_length) . "</em></li>"
);
}
// Other filetypes
Expand Down

0 comments on commit 064b700

Please sign in to comment.