Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Nox\Command;
- use Nox\Container;
- use Nox\Entity\Image;
- use Nox\Files;
- class ImageUpdate 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\Image');
- $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()
- {
- $this->addImages();
- $this->createThumbs();
- }
- public function addImages()
- {
- $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);
- foreach ($diff as $path) {
- $file = new \SplFileInfo($this->imageRoot.$path);
- $sha512Hash = hash_file('sha512', $file->getPathname());
- if ($dup = $this->repo->findOneBy(['sha512' => $sha512Hash])) {
- printf("duplicate\t%s\t%s\n", $dup->image, $path);
- ob_flush();
- // continue;
- }
- $dt = new \DateTime('@'.$file->getMTime());
- $image = new Image();
- $image->setImage($path);
- $image->setSha512($sha512Hash);
- $image->setTime($dt);
- $this->ci->em->persist($image);
- $this->ci->em->flush();
- $this->ci->em->clear();
- printf("image\t%s\n", $path);
- ob_flush();
- }
- }
- public function createThumbs()
- {
- $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 -thumbnail %s\> %s[0] %s",
- $size, $imagePathname, $thumbPathname));
- ob_flush();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement