/** * Merge a sublayer with another sublayer below it in the stack * Note: the result layer will conserve the given id * Return true if success or false if layer isn't found or doesn't have a layer under it in the stack * * @param integer $layerId * * @return boolean */ public function mergeDown($layerId) { // if the layer exists in document if ($this->isLayerInIndex($layerId)) { $layerLevel = $this->getLayerLevel($layerId); $layerPositions = $this->getLayerPositions($layerId); $layer = $this->getLayer($layerId); $layerWidth = $layer->getWidth(); $layerHeight = $layer->getHeight(); $layerPositionX = $this->layerPositions[$layerId]["x"]; $layerPositionY = $this->layerPositions[$layerId]["y"]; if ($layerLevel > 1) { $underLayerId = $this->layerLevels[$layerLevel - 1]; $underLayer = $this->getLayer($underLayerId); $underLayerWidth = $underLayer->getWidth(); $underLayerHeight = $underLayer->getHeight(); $underLayerPositionX = $this->layerPositions[$underLayerId]["x"]; $underLayerPositionY = $this->layerPositions[$underLayerId]["y"]; $totalWidthLayer = $layerWidth + $layerPositionX; $totalHeightLayer = $layerHeight + $layerPositionY; $totalWidthUnderLayer = $underLayerWidth + $underLayerPositionX; $totalHeightUnderLayer = $underLayerHeight + $underLayerPositionY; $minLayerPositionX = $layerPositionX; if ($layerPositionX > $underLayerPositionX) { $minLayerPositionX = $underLayerPositionX; } $minLayerPositionY = $layerPositionY; if ($layerPositionY > $underLayerPositionY) { $minLayerPositionY = $underLayerPositionY; } if ($totalWidthLayer > $totalWidthUnderLayer) { $layerTmpWidth = $totalWidthLayer - $minLayerPositionX; } else { $layerTmpWidth = $totalWidthUnderLayer - $minLayerPositionX; } if ($totalHeightLayer > $totalHeightUnderLayer) { $layerTmpHeight = $totalHeightLayer - $minLayerPositionY; } else { $layerTmpHeight = $totalHeightUnderLayer - $minLayerPositionY; } $layerTmp = new self(array("width" => $layerTmpWidth, "height" => $layerTmpHeight)); $layerTmp->addLayer(1, $underLayer, $underLayerPositionX - $minLayerPositionX, $underLayerPositionY - $minLayerPositionY); $layerTmp->addLayer(2, $layer, $layerPositionX - $minLayerPositionX, $layerPositionY - $minLayerPositionY); // Update layers $layerTmp->mergeAll(); $this->layers[$underLayerId] = clone $layerTmp; $this->layerPositions[$underLayerId]["x"] = $minLayerPositionX; $this->layerPositions[$underLayerId]["y"] = $minLayerPositionX; } else { $layerTmp = new self(array("imageVar" => $this->image)); $layerTmp->addLayer(1, $layer, $layerPositionX, $layerPositionY); // Update background image $this->image = $layerTmp->getResult(); } unset($layerTmp); // Remove the merged layer from the stack $this->remove($layerId); return true; } return false; }