Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit dad7961911c9572364864b7ac343e1b7b07daec4 @Cifro committed Oct 16, 2011
Showing with 5,660 additions and 0 deletions.
  1. +7 −0 generate
  2. +8 −0 generate.bat
  3. +162 −0 libs/Thumbnailer.php
  4. +5,360 −0 libs/nette.min.php
  5. +30 −0 license.txt
  6. +32 −0 readme.md
  7. +12 −0 thumbnailer
  8. +1 −0 thumbnailer.bat
  9. +48 −0 thumbnailer.php
@@ -0,0 +1,7 @@
+read -p "Which folder?" folder
+read -p "Thumbnail folder?" tnfolder
+read -p "Width: " width
+read -p "Height: " height
+read -p "Quality: " quality
+
+bash thumbnailer -s $folder -t $tnfolder -w $width -h $height -q $quality
@@ -0,0 +1,8 @@
+@echo off
+set /p folder=Which folder?
+set /p tnfolder=Tn folder?
+set /p width=Width:
+set /p height=Height:
+set /p quality=Quality:
+
+thumbnailer.bat -s %folder% -t %tnfolder% -w %width% -h %height% -q %quality%
@@ -0,0 +1,162 @@
+<?php
+
+/**
+ * Thumbnailer - image thumbnail generator
+ *
+ * Copyright (c) 2011 Cifro Nix (http://about.me/Cifro)
+ *
+ * This source file is subject to the "New BSD License"
+ */
+
+class Thumbnailer
+{
+
+ public static $version = "0.1";
+
+ /** @var string */
+ private $imagesFolder;
+
+ /** @var string */
+ private $thumbnailsFolder;
+
+ /** @var int */
+ private $width;
+
+ /** @var int */
+ private $height;
+
+ /** @var string */
+ private $logoFile;
+
+ /** @var int */
+ private $logoPosition = 3;
+
+ /** @var Finder */
+ private $images;
+
+ /** @var bool */
+ private $coverPrefix;
+
+ /** @var int */
+ private $quality = 80;
+
+ /** @var float|int */
+ private $topCropOffset = 0.05;
+
+
+ public function __construct(array $options)
+ {
+ if(realpath($options['s']) !== false){
+ $this->imagesFolder = rtrim(realpath($options['s']), '/\\');
+ }else{
+ echo "[Error] Folder '$options[s]' doesn't exists.\n";
+ exit;
+ }
+
+ $this->width = (int) $options['w'];
+ $this->height = (int) $options['h'];
+
+
+ if(!isset($options['t'])){
+ $this->thumbnailsFolder = $this->imagesFolder . '/tn';
+ }else{
+ if(realpath($options['t']) !== false){
+ $this->thumbnailsFolder = realpath($options['t']);
+ }else{
+ echo "[Error] Folder '$options[t]' doesn't exists.\n";
+ exit;
+ }
+ }
+ if(isset($options['q'])){
+ $this->quality = (int) $options['q'];
+ }
+
+ if(isset($options['o'])){
+ $this->topCropOffset = (float) $options['o'];
+ }
+
+ if(isset($options['l'])){
+ if(realpath($options['l']) !== false){
+ $this->logoFile = realpath($options['l']);
+ }else{
+ echo "[Error] Logo file '$options[l]' doesn't exists.\n";
+ exit;
+ }
+
+ if(isset($options['p'])){
+ $this->logoPosition = (int) $options['p'];
+ }
+ }
+
+ if(!isset($options['c'])){
+ $this->images = Finder::findFiles("*.jpg")->in($this->imagesFolder);
+ }else{
+
+ $this->coverPrefix = $options['c'];
+
+ $images = iterator_to_array(Finder::findFiles("*.jpg")->in($this->imagesFolder)->getIterator());
+
+ shuffle($images);
+ $c = count($images);
+ $this->images = array_slice($images, 0, $c >= 5 ? 5 : $c);
+ unset($images);
+ }
+
+ }
+
+
+ /**
+ * Generates thumbnail images
+ *
+ */
+ public function generate()
+ {
+ if(!is_dir($this->thumbnailsFolder)){
+ umask(0000);
+ $dir = @mkdir($this->thumbnailsFolder, 0777, 1); // make /tn/ dir
+
+ if(!$dir){
+ exit("[Error] Can't create '$dir' directory.\n");
+ }
+
+ }
+
+ foreach($this->images as $image){
+
+ $im = Image::fromFile($image->getRealPath());
+
+ $im->resize($this->width, null, Image::FIT | Image::FILL);
+
+ $width = $im->getWidth();
+ $height = $im->getHeight();
+
+ if($height > $this->height){
+ $left = 0;
+ $top = (($width - $this->height) / 2) * $this->topCropOffset;
+
+ $im->crop($left, $top, $this->width, $this->height);
+ }
+
+ if($this->coverPrefix === null){
+ $thumbnailFile = "{$this->thumbnailsFolder}/{$image->getFilename()}";
+ }else{
+ $thumbnailFile = "{$this->thumbnailsFolder}/{$this->coverPrefix}{$image->getFilename()}";
+ }
+
+ $wasSaved = $im->save($thumbnailFile, $this->quality, Image::JPEG);
+
+ if(!$wasSaved){
+ echo "[Error] Image '$thumbnailFile' can't be saved.\n";
+ }else{
+ echo "[OK] Image '$thumbnailFile' was successfuly saved\n {$width}x{$height} -> {$im->getWidth()}x{$im->getHeight()}\n";
+ }
+ }
+ }
+
+}
+
+
+class ThumbnailerException extends Exception
+{
+
+}
Oops, something went wrong.

0 comments on commit dad7961

Please sign in to comment.