1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-19 05:30:08 -07:00
2017-05-05 21:00:11 +02:00

87 lines
2.6 KiB
PHP

<?php
namespace AppBundle\Service\Loader;
use AppBundle\Entity\LDraw\Model;
use AppBundle\Service\Stl\StlRendererService;
use League\Flysystem\Filesystem;
class ImageLoader extends BaseLoader
{
/** @var Filesystem */
private $mediaFilesystem;
/** @var string */
private $rebrickableDownloadUrl;
/** @var StlRendererService */
private $stlRendererService;
public function __construct($mediaFilesystem, $rebrickableDownloadUrl, $stlRendererService)
{
$this->mediaFilesystem = $mediaFilesystem;
$this->rebrickableDownloadUrl = $rebrickableDownloadUrl;
$this->stlRendererService = $stlRendererService;
}
/**
* @param $color
* @param null $path
*/
public function loadColorFromRebrickable($color, $path = null)
{
if (!$path) {
$path = $this->rebrickableDownloadUrl."ldraw/parts_{$color}.zip";
}
$file = $this->downloadFile($path);
$zip = new \ZipArchive($file);
if ($zip->open($file) === true) {
$this->output->writeln([
"Extracting ZIP file into {$this->mediaFilesystem->getAdapter()->getPathPrefix()}images/{$color}",
]);
$zip->extractTo($this->mediaFilesystem->getAdapter()->getPathPrefix().'images'.DIRECTORY_SEPARATOR.$color);
$zip->close();
$this->output->writeln(['Done!']);
} else {
$this->output->writeln(['<error>Extraction of file failed!</error>']);
}
}
/**
* Load images of models.
*/
public function loadMissingModelImages()
{
$models = $this->em->getRepository(Model::class)->findAll();
$this->initProgressBar(count($models));
foreach ($models as $model) {
$this->progressBar->setMessage($model->getNumber());
if (!$this->mediaFilesystem->has('images'.DIRECTORY_SEPARATOR.'-1'.DIRECTORY_SEPARATOR.$model->getNumber().'.png')) {
try {
$this->loadModelImage($this->mediaFilesystem->getAdapter()->getPathPrefix().$model->getPath());
} catch (\Exception $e) {
dump($e->getMessage());
}
}
$this->progressBar->advance();
}
$this->progressBar->finish();
}
/**
* Render model and save image into co.
*
* @param $file
*/
public function loadModelImage($file)
{
$this->stlRendererService->render(
$file,
$this->mediaFilesystem->getAdapter()->getPathPrefix().'images'.DIRECTORY_SEPARATOR.'-1'.DIRECTORY_SEPARATOR
);
}
}