Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Nox\Command;
- use Nox\Container;
- use Nox\Entity\ImageCache;
- use Nox\Files;
- class CacheUpdate extends Container
- {
- protected $ci;
- protected $repo;
- protected $imageDir;
- protected $thumbDir;
- protected $ignore;
- public function __construct($ci)
- {
- parent::__construct($ci);
- $this->repo = $ci->em->getRepository('\Nox\Entity\ImageCache');
- $this->imageRoot = $ci->settings['image_root'];
- $this->thumbRoot = $ci->settings['thumb_root'];
- $this->imageExts = $ci->settings['image_exts'];
- $this->ignore = $ci->settings['image_ignore'];
- }
- public function command($args)
- {
- isset($args[0]) && $args[0] === "thumbs" ?
- $this->thumbs() : $this->thumbs($this->images());
- }
- private function prepareSha512($path) {
- $file = new \SplFileInfo($this->imageRoot.$path);
- $sha512 = hash_file('sha512', $file->getPathname());
- printf("sha512\t%s\t%s\n", $sha512, $path);
- ob_flush();
- return compact('file', 'sha512', 'path');
- }
- private function filterDups($array) {
- return array_intersect_key($array,
- array_unique(array_column($array, 'sha512'))
- );
- }
- private function filterNull($file) {
- return $file != NULL;
- }
- private function putImages($file) {
- if (!isset($file)) return;
- $dt = new \DateTime('@'.$file['file']->getMTime());
- $image = new ImageCache();
- $image->setImage($file['path']);
- $image->setSha512($file['sha512']);
- $image->setTime($dt);
- $this->ci->em->persist($image);
- $this->ci->em->flush();
- $this->ci->em->clear();
- printf("image\t%s\n", $file['path']);
- ob_flush();
- return $image;
- }
- public function images()
- {
- $images = $this->repo->findAll();
- $files = Files::find($this->imageRoot, $this->ignore, $this->imageExts);
- $list = array();
- foreach ($images as $image) array_push($list, $image->getImage());
- $diff = array_diff($files, $list);
- return array_map(array($this, "putImages"),
- array_filter(
- $this->filterDups(
- array_map(array($this, "prepareSha512"), $diff)
- ),
- array($this, "filterNull")
- )
- );
- }
- public function thumbs($images = NULL)
- {
- if ($images === NULL) $images = $this->repo->findAll();
- $files = Files::find($this->thumbRoot);
- foreach ($images as $image) {
- foreach (["medium" => "1000x1000", "small" => "250x250"]
- as $name => $size) {
- $thumb = $image->id . "_" . $name . ".png";
- $key = array_search($thumb, $files);
- if ($key !== false) continue;
- $imagePathname = escapeshellarg($this->imageRoot
- . $image->image);
- $thumbPathname = escapeshellarg($this->thumbRoot . $thumb);
- printf("thumb\t%s\t%s\n", $image->image, $thumb);
- ob_flush();
- exec(sprintf("convert -limit memory 128M -thumbnail %s\> %s[0] %s",
- $size, $imagePathname, $thumbPathname));
- ob_flush();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement