/** * {@inheritdoc} */ public function offsetSet($offset, $image) { if (!$image instanceof Image) { throw new InvalidArgumentException('Only a Gmagick Image can be used as layer'); } if (null === $offset) { $offset = count($this) - 1; } else { if (!is_int($offset)) { throw new InvalidArgumentException('Invalid offset for layer, it must be an integer'); } if (count($this) < $offset || 0 > $offset) { throw new OutOfBoundsException(sprintf('Invalid offset for layer, it must be a value between 0 and %d, %d given', count($this), $offset)); } if (isset($this[$offset])) { unset($this[$offset]); $offset = $offset - 1; } } $frame = $image->getGmagick(); try { if (count($this) > 0) { $this->resource->setimageindex($offset); $this->resource->nextimage(); } $this->resource->addimage($frame); /** * ugly hack to bypass issue https://bugs.php.net/bug.php?id=64623 */ if (count($this) == 2) { $this->resource->setimageindex($offset + 1); $this->resource->nextimage(); $this->resource->addimage($frame); unset($this[0]); } } catch (\GmagickException $e) { throw new RuntimeException('Unable to set the layer', $e->getCode(), $e); } $this->layers = array(); }