|
|
|
@ -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'); |
|
|
|
|
$this->prepareImage(); |
|
|
|
|
$this->current_image->writeImage($file); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$nb_images = $this->config->get('assembler.superpose.min_num_images', 5); |
|
|
|
|
for ($i = 0; $i < $nb_images; $i++) { |
|
|
|
|
$this->current_image = $this->superpose(); |
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
public function display() |
|
|
|
|
{ |
|
|
|
|
try { |
|
|
|
|
$this->prepareImage(); |
|
|
|
|
|
|
|
|
|
header('Content-type: image/jpeg'); |
|
|
|
|
echo $this->current_image; |
|
|
|
|
} catch (\ImagickException $e) { |
|
|
|
|
echo $e->getMessage(); |
|
|
|
|
} |
|
|
|
|
$this->current_image->writeImage($file); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @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(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |