Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Nox\Command;
  4.  
  5. use Nox\Container;
  6. use Nox\Entity\Image;
  7. use Nox\Files;
  8.  
  9. class ImageUpdate extends Container
  10. {
  11.     protected $ci;
  12.     protected $repo;
  13.     protected $imageDir;
  14.     protected $thumbDir;
  15.     protected $ignore;
  16.  
  17.     public function __construct($ci)
  18.     {
  19.         parent::__construct($ci);
  20.         $this->repo = $ci->em->getRepository('\Nox\Entity\Image');
  21.         $this->imageRoot = $ci->settings['image_root'];
  22.         $this->thumbRoot = $ci->settings['thumb_root'];
  23.         $this->imageExts = $ci->settings['image_exts'];
  24.         $this->ignore = $ci->settings['image_ignore'];
  25.     }
  26.  
  27.     public function command()
  28.     {
  29.         $this->addImages();
  30.         $this->createThumbs();
  31.     }
  32.  
  33.     public function addImages()
  34.     {
  35.         $images = $this->repo->findAll();
  36.         $files = Files::find($this->imageRoot, $this->ignore, $this->imageExts);
  37.         $list = array();
  38.         foreach ($images as $image) array_push($list, $image->getImage());
  39.         $diff = array_diff($files, $list);
  40.         foreach ($diff as $path) {
  41.             $file = new \SplFileInfo($this->imageRoot.$path);
  42.             $sha512Hash = hash_file('sha512', $file->getPathname());
  43.             if ($dup = $this->repo->findOneBy(['sha512' => $sha512Hash])) {
  44.                 printf("duplicate\t%s\t%s\n", $dup->image, $path);
  45.                 ob_flush();
  46.     //            continue;
  47.             }
  48.             $dt = new \DateTime('@'.$file->getMTime());
  49.             $image = new Image();
  50.             $image->setImage($path);
  51.             $image->setSha512($sha512Hash);
  52.             $image->setTime($dt);
  53.             $this->ci->em->persist($image);
  54.             $this->ci->em->flush();
  55.             $this->ci->em->clear();
  56.             printf("image\t%s\n", $path);
  57.             ob_flush();
  58.         }
  59.     }
  60.  
  61.     public function createThumbs()
  62.     {
  63.         $images = $this->repo->findAll();
  64.         $files = Files::find($this->thumbRoot);
  65.         foreach ($images as $image) {
  66.             foreach (["medium" => "1000x1000", "small" => "250x250"]
  67.                 as $name => $size) {
  68.                 $thumb = $image->id . "_" . $name . ".png";
  69.                 $key = array_search($thumb, $files);
  70.                 if ($key !== false) continue;
  71.                 $imagePathname = escapeshellarg($this->imageRoot
  72.                     . $image->image);
  73.                 $thumbPathname = escapeshellarg($this->thumbRoot . $thumb);
  74.                 printf("thumb\t%s\t%s\n", $image->image, $thumb);
  75.                 ob_flush();
  76.                 exec(sprintf("convert -thumbnail %s\> %s[0] %s",
  77.                     $size, $imagePathname, $thumbPathname));
  78.                 ob_flush();
  79.             }
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement