Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createthumbnail improvements #6

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/config_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
?>
134 changes: 0 additions & 134 deletions src/createsmall.php

This file was deleted.

130 changes: 67 additions & 63 deletions src/createthumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,75 @@ function create_thumb($filename, $outfile, $size = 120) {
$target = "";
$xoord = 0;
$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 -a -f");
else
exec("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o " . escapeshellarg($outfile) . " -s " . escapeshellarg($size) . " -c jpeg -a -f");
return;
}
ob_start();

$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 (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 {
$yoord = ceil(($height-$width)/2);
$height = $width;
}
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 {
$ratio_orig = $width_orig/$height_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($size,$size);
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);
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();
}


Expand All @@ -90,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;
}

Expand All @@ -99,11 +119,10 @@ 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;
}

// $extension = preg_replace('.*\.\w*$', '', $_GET['filename']);

if (preg_match("/.gif$/i", $_GET['filename'])) {
header('Content-type: image/gif');
Expand All @@ -115,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;
}

Expand All @@ -125,24 +144,9 @@ 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 ( $cleanext == 'gif') {
$img = ImageCreateFromGIF($thumbnail);
if(!$img) {
create_thumb($_GET['filename'], null, $_GET['size']);
exit;
}
ImageGIF($img,null,90);
if (is_file($thumbnail)) {
readfile($thumbnail); //Use the cache
} 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);
?>
10 changes: 5 additions & 5 deletions src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function checkpermissions($file) {
// JPG, GIF and PNG
if (preg_match("/.jpg$|.gif$|.png$/i", $file))
{
$img_captions[$file] .= "<a href=\"createsmall.php?filename=" . $currentdir . "/" . $file . "&amp;size=$small_size\">small</a>&nbsp;\n";
$img_captions[$file] .= "<a href=\"createthumb.php?filename=" . $currentdir . "/" . $file . "&amp;size=$small_size\">small</a>&nbsp;\n";
$img_captions[$file] .= "<a href=\"" . $currentdir . "/" . $file . "\">original</a>\n";
//Read EXIF
if ($display_exif == 1) $img_captions[$file] .= "<br />" .readEXIF($currentdir . "/" . $file);
Expand All @@ -216,7 +216,7 @@ function checkpermissions($file) {
"name" => $file,
"date" => filemtime($currentdir . "/" . $file),
"size" => filesize($currentdir . "/" . $file),
"html" => "<li><a href='createsmall.php?filename=" . $currentdir . "/" . $file . "&amp;size=$small_size' rel='lightbox[billeder]' title='$img_captions[$file]'><span></span><img src='" . GALLERY_ROOT . "createthumb.php?filename=" . $thumbdir . "/" . $file . "&amp;size=$thumb_size' alt='$label_loading' /></a></li>");
"html" => "<li><a href='createthumb.php?filename=" . $currentdir . "/" . $file . "&amp;size=$small_size' rel='lightbox[billeder]' title='$img_captions[$file]'><span></span><img src='" . GALLERY_ROOT . "createthumb.php?filename=" . $thumbdir . "/" . $file . "&amp;size=$thumb_size' alt='$label_loading' /></a><em>" . padstring($file, $label_max_length) . "</em></li>");
}
// MP4
else if (preg_match("/.mp4$/i", $file))
Expand All @@ -227,7 +227,7 @@ function checkpermissions($file) {
"name" => $file,
"date" => filemtime($currentdir . "/" . $file),
"size" => filesize($currentdir . "/" . $file),
"html" => "<li><a href='" . $currentdir . "/" . $file . "' rel='lightbox[billeder]' title='$img_captions[$file]'><span></span><img src='" . GALLERY_ROOT . "createthumb.php?filename=" . $thumbdir . "/" . $file . "&amp;size=$thumb_size' alt='$label_loading' /></a></li>");
"html" => "<li><a href='" . $currentdir . "/" . $file . "' rel='lightbox[billeder]' title='$img_captions[$file]'><span></span><img src='" . GALLERY_ROOT . "createthumb.php?filename=" . $thumbdir . "/" . $file . "&amp;size=$thumb_size' alt='$label_loading' /></a><em>" . padstring($file, $label_max_length) . "</em></li>");
}
// Other filetypes
$extension = "";
Expand Down Expand Up @@ -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 .= "<a href='createsmall.php?filename=" . $currentdir . "/" . $files[$y]["name"] . "&amp;size=" . $small_size . "' rel='lightbox[billeder]' class='hidden' title='" . $img_captions[$files[$y]["name"]] . "'></a>";
$breadcrumb_navigation .= "<a href='createthumb.php?filename=" . $currentdir . "/" . $files[$y]["name"] . "&amp;size=" . $small_size . "' rel='lightbox[billeder]' class='hidden' title='" . $img_captions[$files[$y]["name"]] . "'></a>";
}

//-----------------------
Expand Down Expand Up @@ -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 .= "<a href='createsmall.php?filename=" . $currentdir . "/" . $files[$y]["name"] . "&amp;size=" . $small_size . "' rel='lightbox[billeder]' class='hidden' title='" . $img_captions[$files[$y]["name"]] . "'></a>";
$page_navigation .= "<a href='createthumb.php?filename=" . $currentdir . "/" . $files[$y]["name"] . "&amp;size=" . $small_size . "' rel='lightbox[billeder]' class='hidden' title='" . $img_captions[$files[$y]["name"]] . "'></a>";
}

//-----------------------
Expand Down