diff --git a/dist/nano_photos_provider2.cfg b/dist/nano_photos_provider2.cfg index 56bb489..d98648c 100644 --- a/dist/nano_photos_provider2.cfg +++ b/dist/nano_photos_provider2.cfg @@ -1,5 +1,5 @@ [config] -fileExtensions="jpg|jpeg|png|gif" +fileExtensions="jpg|JPG|jpeg|png|gif" contentFolder="nano_photos_content" sortOrder="asc" titleDescSeparator="$$" @@ -15,6 +15,8 @@ jpegQuality=90 blurredImageQuality=3 allowedSizeValues="" ;allowedSizeValues="50|100|225|150|200|300|auto" +thumbnailHeight="200" +thumbnailWidth="200" [security] allowOrigins="*" diff --git a/dist/thumbnailcreator.php b/dist/thumbnailcreator.php new file mode 100755 index 0000000..b76d925 --- /dev/null +++ b/dist/thumbnailcreator.php @@ -0,0 +1,227 @@ +#!/usr/bin/php + filemtime($baseFolder.'/'.$imageFilename) ) { + // image file is older as the thumbnail file + $generateThumbnail = false; + } + } + + // Check if dominantColors exists + $generateDominantColors = true; + if( file_exists($baseFolder . '/_thumbnails/' . $dominantcolorFilename)) { + if( filemtime($baseFolder . '/_thumbnails/' . $dominantcolorFilename) > filemtime($baseFolder.'/'.$imageFilename) ) { + $generateDominantColors=false; + } + } + else { + } + + // Get ImageSize + $size = getimagesize($filepath); + $orgImage = null; + image_fix_orientation($orgImage, $size, $filepath); + + $width = $size[0]; + $height = $size[1]; + + if ($height == 0) { + echo "Image with zero height: $filepath\n"; + continue; + } + + $originalAspect = $width / $height; + $thumbAspect = $thumbWidth / $thumbHeight; + + // Check ImageSize against MaxSize + $generateMaxSize = false; + if ( $width > $config_values['images']['maxSize'] || $height > $config_values['images']['maxSize'] ) { + if ( file_exists($baseFolder . '/_thumbnails/' .$imageFilename) == false ) { + $generateMaxSize = true; + } + } + + // Generate Image + if( $generateThumbnail == true || $generateDominantColors == true || $generateMaxSize == true ) { + switch ($size['mime']) { + case 'image/jpeg': + $orgImage = imagecreatefromjpeg($filepath); + break; + case 'image/gif': + $orgImage = imagecreatefromgif($filepath); + break; + case 'image/png': + $orgImage = imagecreatefrompng($filepath); + break; + default: + return false; + break; + } + } + image_fix_orientation($orgImage, $size, $filepath); + + // Resize Image if >MaxSize + if( $generateMaxSize == true ) { + + // Calc new size + if ( $width > $height ) { + $MaxSizeWidth = $config_values['images']['maxSize']; + $MaxSizeHeight = $MaxSizeWidth / $originalAspect; + + } else { + $MaxSizeHeight = $config_values['images']['maxSize']; + $MaxSizeWidth = $MaxSizeHeight * $originalAspect; + } + + $MaxSize = imagecreatetruecolor($MaxSizeWidth, $MaxSizeHeight); + // Resize + imagecopyresampled($MaxSize, $orgImage, 0, 0, 0, 0, $MaxSizeWidth, $MaxSizeHeight, $width, $height); + + switch ($size['mime']) { + case 'image/jpeg': + imagejpeg($MaxSize, $baseFolder . '/_thumbnails/' . $imageFilename, $config_values['thumbnails']['jpegQuality'] ); + break; + case 'image/gif': + imagegif($MaxSize, $baseFolder . '/_thumbnails/' . $imageFilename); + break; + case 'image/png': + imagepng($MaxSize, $baseFolder . '/_thumbnails/' . $imageFilename, 1); + break; + } + } + + if ( $thumbWidth != 'auto' && $thumbHeight != 'auto' ) { + + // IMAGE CROP + // some inspiration found in donkeyGallery (from Gix075) https://github.com/Gix075/donkeyGallery + if ($originalAspect >= $thumbAspect) { + // If image is wider than thumbnail (in aspect ratio sense) + $newHeight = $thumbHeight; + $newWidth = $width / ($height / $thumbHeight); + } else { + // If the thumbnail is wider than the image + $newWidth = $thumbWidth; + $newHeight = $height / ($width / $thumbWidth); + } + + if( $generateThumbnail == true ) { + $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight); + // Resize and crop + imagecopyresampled($thumb, $orgImage, + 0 - ($newWidth - $thumbWidth) / 2, // dest_x: Center the image horizontally + 0 - ($newHeight - $thumbHeight) / 2, // dest-y: Center the image vertically + 0, 0, // src_x, src_y + $newWidth, $newHeight, $width, $height); + } + + } else { + // NO IMAGE CROP + if( $thumbWidth == 'auto' ) { + $newWidth = $width / $height * $thumbHeight; + $newHeight = $thumbHeight; + } + else { + $newHeight = $height / $width * $thumbWidth; + $newWidth = $thumbWidth; + } + + if( $generateThumbnail == true ) { + $thumb = imagecreatetruecolor($newWidth, $newHeight); + + // Resize + imagecopyresampled($thumb, $orgImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); + } + } + + if( $generateThumbnail == true ) { + switch ($size['mime']) { + case 'image/jpeg': + imagejpeg($thumb, $baseFolder . '/_thumbnails/' . $thumbnailFilename, $config_values['thumbnails']['jpegQuality'] ); + break; + case 'image/gif': + imagegif($thumb, $baseFolder . '/_thumbnails/' . $thumbnailFilename); + break; + case 'image/png': + imagepng($thumb, $baseFolder . '/_thumbnails/' . $thumbnailFilename, 1); + break; + } + } + + + if( $generateDominantColors == true ) { + // Dominant colorS -> GIF + $dc3 = imagecreate($config_values['thumbnails']['blurredImageQuality'], $config_values['thumbnails']['blurredImageQuality']); + imagecopyresampled($dc3, $orgImage, 0, 0, 0, 0, 3, 3, $width, $height); + ob_start(); + imagegif( $dc3 ); + $image_data = ob_get_contents(); + ob_end_clean(); + + // Dominant color -> HEX RGB + $pixel = imagecreatetruecolor(1, 1); + imagecopyresampled($pixel, $orgImage, 0, 0, 0, 0, 1, 1, $width, $height); + $rgb = imagecolorat($pixel, 0, 0); + $color = imagecolorsforindex($pixel, $rgb); + $hex=sprintf('#%02x%02x%02x', $color['red'], $color['green'], $color['blue']); + + // save to cache + $fdc = fopen($baseFolder . '/_thumbnails/' . $thumbnailFilename . '.data', 'w'); + if( $fdc ) { + fwrite($fdc, 'dc=' . $hex . "\n"); + fwrite($fdc, 'dcGIF=' . base64_encode( $image_data )); + fclose($fdc); + } + else { + } + } + } +} +?>