🎨 Factorize some code and add the file deletion

This commit is contained in:
Clément 2021-02-25 17:33:40 +01:00
parent 7cfce96dd0
commit cd452fe0d1
5 changed files with 171 additions and 54 deletions

View File

@ -4,6 +4,7 @@ namespace Shikiryu\WebGobbler;
abstract class Assembler abstract class Assembler
{ {
protected $files = [];
/** /**
* @var \Shikiryu\WebGobbler\Pool * @var \Shikiryu\WebGobbler\Pool
*/ */
@ -26,6 +27,18 @@ abstract class Assembler
$this->config = $config; $this->config = $config;
} }
/**
* @return void
* @link https://php.net/manual/en/language.oop5.decon.php
*/
public function __destruct()
{
foreach ($this->files as $file) {
unlink($file);
}
}
/** /**
* @param string $file * @param string $file
* *

View File

@ -14,14 +14,51 @@ class Mosaic extends Assembler
*/ */
public function saveTo($file) public function saveTo($file)
{ {
try {
$final_image = new \Imagick(); $final_image = new \Imagick();
$final_image->setColorspace(\Imagick::COLORSPACE_RGB); $final_image->setColorspace(\Imagick::COLORSPACE_RGB);
$final_image->newImage($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey'), 'none'); $final_image->newImage($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey'), 'none');
$final_image = $this->prepareImage($final_image);
$final_image->writeImage($file);
} catch (\ImagickException $e) {
echo $e->getMessage();
}
}
/**
*
*/
public function display()
{
try {
$final_image = new \Imagick();
$final_image->setColorspace(\Imagick::COLORSPACE_RGB);
$final_image->newImage($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey'), 'none');
$final_image = $this->prepareImage($final_image);
header('Content-type: image/jpeg');
echo $final_image;
} catch (\ImagickException $e) {
echo $e->getMessage();
}
}
/**
* @param \Imagick $final_image
*
* @return \Imagick
* @throws \ImagickException
*/
private function prepareImage(\Imagick $final_image)
{
$image_size_x = $this->config->get('assembler.sizex') / $this->config->get('assembler.nbx'); $image_size_x = $this->config->get('assembler.sizex') / $this->config->get('assembler.nbx');
$image_size_y = $this->config->get('assembler.sizey') / $this->config->get('assembler.nby'); $image_size_y = $this->config->get('assembler.sizey') / $this->config->get('assembler.nby');
for ($y = 0; $y < $this->config->get('assembler.nby'); $y++) { for ($y = 0; $y < $this->config->get('assembler.nby'); $y++) {
for ($x = 0; $x < $this->config->get('assembler.nbx'); $x++) { for ($x = 0; $x < $this->config->get('assembler.nbx'); $x++) {
$image = new \Imagick($this->pool->getImage()); $file = $this->pool->getImage();
$this->files[] = $file;
$image = new \Imagick($file);
if ($image->getColorspace() !== \Imagick::COLORSPACE_RGB) { if ($image->getColorspace() !== \Imagick::COLORSPACE_RGB) {
$image->setColorspace(\Imagick::COLORSPACE_RGB); $image->setColorspace(\Imagick::COLORSPACE_RGB);
} }
@ -38,11 +75,7 @@ class Mosaic extends Assembler
if (true === $this->config->get('assembler.invert')) { if (true === $this->config->get('assembler.invert')) {
$final_image->negateImage(false); $final_image->negateImage(false);
} }
$final_image->writeImage($file);
}
public function display() return $final_image;
{
// TODO: Implement display() method.
} }
} }

View File

@ -12,7 +12,38 @@ class Simple extends Assembler
public function saveTo($file) public function saveTo($file)
{ {
try { try {
$image = new \Imagick($this->pool->getImage()); $image = $this->prepareImage();
$image->writeImage($file);
} catch (\ImagickException $e) {
echo $e->getMessage();
}
}
/**
* @param string $file
*/
public function display($file)
{
try {
$image = $this->prepareImage();
header('Content-type: image/jpeg');
echo $image;
} catch (\ImagickException $e) {
echo $e->getMessage();
}
}
/**
* @return \Imagick
* @throws \ImagickException
*/
private function prepareImage()
{
$file = $this->pool->getImage();
$this->files[] = $file;
$image = new \Imagick($file);
if ($image->getColorspace() !== \Imagick::COLORSPACE_RGB) { if ($image->getColorspace() !== \Imagick::COLORSPACE_RGB) {
$image->setColorspace(\Imagick::COLORSPACE_RGB); $image->setColorspace(\Imagick::COLORSPACE_RGB);
} }
@ -31,14 +62,7 @@ class Simple extends Assembler
if (true === $this->config->get('assembler.invert')) { if (true === $this->config->get('assembler.invert')) {
$image->flipImage(); $image->flipImage();
} }
$image->writeImage($file);
} catch (\ImagickException $e) {
echo $e->getMessage();
}
}
public function display() return $image;
{
// TODO: Implement display() method.
} }
} }

View File

@ -4,6 +4,8 @@ namespace Shikiryu\WebGobbler\Assembler;
use Imagick; use Imagick;
use Shikiryu\WebGobbler\Assembler; use Shikiryu\WebGobbler\Assembler;
use Shikiryu\WebGobbler\Config;
use Shikiryu\WebGobbler\Pool;
class Superpose extends Assembler class Superpose extends Assembler
{ {
@ -12,6 +14,22 @@ class Superpose extends Assembler
*/ */
private $current_image; private $current_image;
private $base_image;
/**
* Superpose constructor.
*
* @param \Shikiryu\WebGobbler\Pool $pool
* @param \Shikiryu\WebGobbler\Config $config
* @param string $from_file
*/
public function __construct(Pool $pool, Config $config, $from_file = null)
{
$this->base_image = $from_file;
parent::__construct($pool, $config);
}
/** /**
* @param string $file * @param string $file
* *
@ -21,20 +39,35 @@ class Superpose extends Assembler
*/ */
public function saveTo($file) public function saveTo($file)
{ {
$this->current_image = new \Imagick(); $this->prepareImage();
$this->current_image->setSize($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey'));
$this->current_image->newImage($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey'), 'none');
$nb_images = $this->config->get('assembler.superpose.min_num_images', 5);
for ($i = 0; $i < $nb_images; $i++) {
$this->current_image = $this->superpose();
}
$this->current_image->writeImage($file); $this->current_image->writeImage($file);
} }
/**
*
*/
public function display()
{
try {
$this->prepareImage();
header('Content-type: image/jpeg');
echo $this->current_image;
} catch (\ImagickException $e) {
echo $e->getMessage();
}
}
/**
* @return \Imagick
* @throws \ImagickException
*/
public function superpose() public function superpose()
{ {
return $this->superposeOneImage($this->current_image, new \Imagick($this->pool->getImage())); $file = $this->pool->getImage();
$this->files[] = $file;
return $this->superposeOneImage($this->current_image, new \Imagick($file));
} }
/** /**
@ -79,12 +112,6 @@ class Superpose extends Assembler
$image_x = $image_to_superpose->getImageWidth(); $image_x = $image_to_superpose->getImageWidth();
$image_y = $image_to_superpose->getImageHeight(); $image_y = $image_to_superpose->getImageHeight();
# Compensate for poorly-contrasted images on the web
/*try:
imageToSuperpose = ImageOps.autocontrast(imageToSuperpose)
except TypeError: # Aaron tells me that this exception occurs with PNG images.
raise BadImage*/
# Some image are too white. # Some image are too white.
# For example, the photo of a coin on a white background. # For example, the photo of a coin on a white background.
# These picture degrad the quality of the final image. # These picture degrad the quality of the final image.
@ -113,8 +140,8 @@ class Superpose extends Assembler
$image_to_superpose->negateImage(false); $image_to_superpose->negateImage(false);
} }
$paste_coords_x = random_int(-($image_x/2), $this->config->get('assembler.sizex')-$image_x); $paste_coords_x = random_int(-($image_x/2), max(0, $this->config->get('assembler.sizex')-$image_x));
$paste_coords_y = random_int(-($image_y/2), $this->config->get('assembler.sizey')-$image_y); $paste_coords_y = random_int(-($image_y/2), max(0, $this->config->get('assembler.sizey')-$image_y));
# Darken image borders # Darken image borders
$image_to_superpose = $this->darkenImageBorder($image_to_superpose); $image_to_superpose = $this->darkenImageBorder($image_to_superpose);
@ -175,8 +202,18 @@ class Superpose extends Assembler
return $image; return $image;
} }
public function display() /**
* @throws \ImagickException
*/
private function prepareImage()
{ {
// TODO: Implement display() method. $this->current_image = null === $this->base_image ? new \Imagick() : new \Imagick($this->base_image);
$this->current_image->setSize($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey'));
$this->current_image->newImage($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey'), 'none');
$nb_images = $this->config->get('assembler.superpose.min_num_images', 5);
for ($i = 0; $i < $nb_images; $i++) {
$this->current_image = $this->superpose();
}
} }
} }

View File

@ -4,6 +4,9 @@ namespace Shikiryu\WebGobbler;
class Pool class Pool
{ {
/**
* @var string[]
*/
protected $file_list = []; protected $file_list = [];
/** /**
@ -58,7 +61,7 @@ class Pool
$this->pool_directory = $pool_dir; $this->pool_directory = $pool_dir;
$this->nb_images = $pool_config['nb_images']; // FIXME $this->nb_images = $pool_config['nb_images'];
$this->prepareCollectors(); $this->prepareCollectors();
} }
@ -110,6 +113,11 @@ class Pool
return $file_list; return $file_list;
} }
/**
* Check if collector folder exists or create it
*
* Download images if configured to
*/
private function prepareCollectors() private function prepareCollectors()
{ {
foreach ($this->collectors as $collector) { foreach ($this->collectors as $collector) {
@ -117,6 +125,7 @@ class Pool
if (!is_dir($directory) && !mkdir($directory) && !is_dir($directory)) { if (!is_dir($directory) && !mkdir($directory) && !is_dir($directory)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory)); throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory));
} }
if ($this->config->get('pool.pre_download', false) === true) {
$images = glob($directory . '/*.{jpg,gif,png}', GLOB_BRACE); $images = glob($directory . '/*.{jpg,gif,png}', GLOB_BRACE);
if (count($images) < $this->nb_images) { if (count($images) < $this->nb_images) {
$collector->getRandomImages($this->nb_images - count($images)); $collector->getRandomImages($this->nb_images - count($images));
@ -124,3 +133,4 @@ class Pool
} }
} }
} }
}