🎨 Factorize some code and add the file deletion
This commit is contained in:
		| @@ -4,6 +4,7 @@ namespace Shikiryu\WebGobbler; | ||||
|  | ||||
| abstract class Assembler | ||||
| { | ||||
|     protected $files = []; | ||||
|     /** | ||||
|      * @var \Shikiryu\WebGobbler\Pool | ||||
|      */ | ||||
| @@ -26,6 +27,18 @@ abstract class Assembler | ||||
|         $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 | ||||
|      * | ||||
|   | ||||
| @@ -14,14 +14,51 @@ class Mosaic extends Assembler | ||||
|      */ | ||||
|     public function saveTo($file) | ||||
|     { | ||||
|         $final_image = new \Imagick(); | ||||
|         $final_image->setColorspace(\Imagick::COLORSPACE_RGB); | ||||
|         $final_image->newImage($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey'), 'none'); | ||||
|         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); | ||||
|  | ||||
|             $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_y = $this->config->get('assembler.sizey') / $this->config->get('assembler.nby'); | ||||
|         for ($y = 0; $y < $this->config->get('assembler.nby'); $y++) { | ||||
|             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) { | ||||
|                     $image->setColorspace(\Imagick::COLORSPACE_RGB); | ||||
|                 } | ||||
| @@ -38,11 +75,7 @@ class Mosaic extends Assembler | ||||
|         if (true === $this->config->get('assembler.invert')) { | ||||
|             $final_image->negateImage(false); | ||||
|         } | ||||
|         $final_image->writeImage($file); | ||||
|     } | ||||
|  | ||||
|     public function display() | ||||
|     { | ||||
|         // TODO: Implement display() method. | ||||
|         return $final_image; | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -12,33 +12,57 @@ class Simple extends Assembler | ||||
|     public function saveTo($file) | ||||
|     { | ||||
|         try { | ||||
|             $image = new \Imagick($this->pool->getImage()); | ||||
|             if ($image->getColorspace() !== \Imagick::COLORSPACE_RGB) { | ||||
|                 $image->setColorspace(\Imagick::COLORSPACE_RGB); | ||||
|             } | ||||
|             $size = $image->getSize(); | ||||
|             $imagex = $size['columns']; | ||||
|             $imagey = $size['rows']; | ||||
|             if ($imagex !== $this->config->get('assembler.sizex') || $imagey !== $this->config->get('assembler.sizey')) { | ||||
|                 $image->thumbnailImage($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey')); | ||||
|             } | ||||
|             if (true === $this->config->get('assembler.mirror')) { | ||||
|                 $image->flopImage(); | ||||
|             } | ||||
|             if (true === $this->config->get('assembler.emboss')) { | ||||
|                 $image->embossImage(0, 1); | ||||
|             } | ||||
|             if (true === $this->config->get('assembler.invert')) { | ||||
|                 $image->flipImage(); | ||||
|             } | ||||
|             $image = $this->prepareImage(); | ||||
|  | ||||
|             $image->writeImage($file); | ||||
|         } catch (\ImagickException $e) { | ||||
|             echo $e->getMessage(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public function display() | ||||
|     /** | ||||
|      * @param string $file | ||||
|      */ | ||||
|     public function display($file) | ||||
|     { | ||||
|         // TODO: Implement display() method. | ||||
|         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) { | ||||
|             $image->setColorspace(\Imagick::COLORSPACE_RGB); | ||||
|         } | ||||
|         $size = $image->getSize(); | ||||
|         $imagex = $size['columns']; | ||||
|         $imagey = $size['rows']; | ||||
|         if ($imagex !== $this->config->get('assembler.sizex') || $imagey !== $this->config->get('assembler.sizey')) { | ||||
|             $image->thumbnailImage($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey')); | ||||
|         } | ||||
|         if (true === $this->config->get('assembler.mirror')) { | ||||
|             $image->flopImage(); | ||||
|         } | ||||
|         if (true === $this->config->get('assembler.emboss')) { | ||||
|             $image->embossImage(0, 1); | ||||
|         } | ||||
|         if (true === $this->config->get('assembler.invert')) { | ||||
|             $image->flipImage(); | ||||
|         } | ||||
|  | ||||
|         return $image; | ||||
|     } | ||||
| } | ||||
| @@ -4,6 +4,8 @@ namespace Shikiryu\WebGobbler\Assembler; | ||||
|  | ||||
| use Imagick; | ||||
| use Shikiryu\WebGobbler\Assembler; | ||||
| use Shikiryu\WebGobbler\Config; | ||||
| use Shikiryu\WebGobbler\Pool; | ||||
|  | ||||
| class Superpose extends Assembler | ||||
| { | ||||
| @@ -12,6 +14,22 @@ class Superpose extends Assembler | ||||
|      */ | ||||
|     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 | ||||
|      * | ||||
| @@ -21,20 +39,35 @@ class Superpose extends Assembler | ||||
|      */ | ||||
|     public function saveTo($file) | ||||
|     { | ||||
|         $this->current_image = new \Imagick(); | ||||
|         $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->prepareImage(); | ||||
|         $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() | ||||
|     { | ||||
|         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_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. | ||||
|         # For example, the photo of a coin on a white background. | ||||
|         # These picture degrad the quality of the final image. | ||||
| @@ -113,8 +140,8 @@ class Superpose extends Assembler | ||||
|             $image_to_superpose->negateImage(false); | ||||
|         } | ||||
|  | ||||
|         $paste_coords_x = random_int(-($image_x/2), $this->config->get('assembler.sizex')-$image_x); | ||||
|         $paste_coords_y = random_int(-($image_y/2), $this->config->get('assembler.sizey')-$image_y); | ||||
|         $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), max(0, $this->config->get('assembler.sizey')-$image_y)); | ||||
|  | ||||
|         # Darken image borders | ||||
|         $image_to_superpose = $this->darkenImageBorder($image_to_superpose); | ||||
| @@ -175,8 +202,18 @@ class Superpose extends Assembler | ||||
|         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(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										18
									
								
								src/Pool.php
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								src/Pool.php
									
									
									
									
									
								
							| @@ -4,6 +4,9 @@ namespace Shikiryu\WebGobbler; | ||||
|  | ||||
| class Pool | ||||
| { | ||||
|     /** | ||||
|      * @var string[] | ||||
|      */ | ||||
|     protected $file_list = []; | ||||
|  | ||||
|     /** | ||||
| @@ -58,7 +61,7 @@ class Pool | ||||
|  | ||||
|         $this->pool_directory = $pool_dir; | ||||
|  | ||||
|         $this->nb_images = $pool_config['nb_images']; // FIXME | ||||
|         $this->nb_images = $pool_config['nb_images']; | ||||
|  | ||||
|         $this->prepareCollectors(); | ||||
|     } | ||||
| @@ -110,6 +113,11 @@ class Pool | ||||
|         return $file_list; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Check if collector folder exists or create it | ||||
|      * | ||||
|      * Download images if configured to | ||||
|      */ | ||||
|     private function prepareCollectors() | ||||
|     { | ||||
|         foreach ($this->collectors as $collector) { | ||||
| @@ -117,9 +125,11 @@ class Pool | ||||
|             if (!is_dir($directory) && !mkdir($directory) && !is_dir($directory)) { | ||||
|                 throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory)); | ||||
|             } | ||||
|             $images = glob($directory.'/*.{jpg,gif,png}', GLOB_BRACE); | ||||
|             if (count($images) < $this->nb_images) { | ||||
|                 $collector->getRandomImages($this->nb_images - count($images)); | ||||
|             if ($this->config->get('pool.pre_download', false) === true) { | ||||
|                 $images = glob($directory . '/*.{jpg,gif,png}', GLOB_BRACE); | ||||
|                 if (count($images) < $this->nb_images) { | ||||
|                     $collector->getRandomImages($this->nb_images - count($images)); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user