Advertisement
Guest User

Untitled

a guest
Feb 26th, 2018
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.34 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Nox\Command;
  4.  
  5. use Nox\Container;
  6. use Nox\Entity\ImageCache;
  7. use Nox\Files;
  8.  
  9. class CacheUpdate 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\ImageCache');
  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($args)
  28.     {
  29.         isset($args[0]) && $args[0] === "thumbs" ?
  30.             $this->thumbs() : $this->thumbs($this->images());
  31.     }
  32.  
  33.     private function prepareSha512($path) {
  34.         $file = new \SplFileInfo($this->imageRoot.$path);
  35.         $sha512 = hash_file('sha512', $file->getPathname());
  36.         printf("sha512\t%s\t%s\n", $sha512, $path);
  37.         ob_flush();
  38.         return compact('file', 'sha512', 'path');
  39.     }
  40.  
  41.     private function filterDups($array) {
  42.         return array_intersect_key($array,
  43.             array_unique(array_column($array, 'sha512'))
  44.         );
  45.     }
  46.  
  47.     private function filterNull($file) {
  48.         return $file != NULL;
  49.     }
  50.  
  51.     private function putImages($file) {
  52.         if (!isset($file)) return;
  53.         $dt = new \DateTime('@'.$file['file']->getMTime());
  54.         $image = new ImageCache();
  55.         $image->setImage($file['path']);
  56.         $image->setSha512($file['sha512']);
  57.         $image->setTime($dt);
  58.         $this->ci->em->persist($image);
  59.         $this->ci->em->flush();
  60.         $this->ci->em->clear();
  61.         printf("image\t%s\n", $file['path']);
  62.         ob_flush();
  63.         return $image;
  64.     }
  65.  
  66.     public function images()
  67.     {
  68.         $images = $this->repo->findAll();
  69.         $files = Files::find($this->imageRoot, $this->ignore, $this->imageExts);
  70.         $list = array();
  71.         foreach ($images as $image) array_push($list, $image->getImage());
  72.         $diff = array_diff($files, $list);
  73.         return array_map(array($this, "putImages"),
  74.             array_filter(
  75.                 $this->filterDups(
  76.                     array_map(array($this, "prepareSha512"), $diff)
  77.                 ),
  78.                 array($this, "filterNull")
  79.             )
  80.         );
  81.     }
  82.  
  83.     public function thumbs($images = NULL)
  84.     {
  85.         if ($images === NULL) $images = $this->repo->findAll();
  86.         $files = Files::find($this->thumbRoot);
  87.         foreach ($images as $image) {
  88.             foreach (["medium" => "1000x1000", "small" => "250x250"]
  89.                 as $name => $size) {
  90.                 $thumb = $image->id . "_" . $name . ".png";
  91.                 $key = array_search($thumb, $files);
  92.                 if ($key !== false) continue;
  93.                 $imagePathname = escapeshellarg($this->imageRoot
  94.                     . $image->image);
  95.                 $thumbPathname = escapeshellarg($this->thumbRoot . $thumb);
  96.                 printf("thumb\t%s\t%s\n", $image->image, $thumb);
  97.                 ob_flush();
  98.                 exec(sprintf("convert -limit memory 128M -thumbnail %s\> %s[0] %s",
  99.                     $size, $imagePathname, $thumbPathname));
  100.                 ob_flush();
  101.             }
  102.         }
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement