Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * Resize the layer
  *
  * @param string $unit
  * @param float $pourcentWidth
  * @param float $pourcentHeight
  * @param boolean $converseProportion
  * @param integer $positionX
  * @param integer $positionY
  * @param string $position
  * 
  * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
  * 
  * $positionX, $positionY, $position can be ignored unless you choose a new width AND a new height AND to conserve proportion.
  */
 public function resize($unit = "pixel", $newWidth = null, $newHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM')
 {
     if ($newWidth || $newHeight) {
         if ($unit == 'pourcent') {
             if ($newWidth) {
                 $newWidth = round($newWidth / 100 * $this->width);
             }
             if ($newHeight) {
                 $newHeight = round($newHeight / 100 * $this->height);
             }
         }
         if ($converseProportion) {
             // Proportion are conserved
             if ($newWidth && $newHeight) {
                 // Proportions + $newWidth + $newHeight
                 if ($this->getWidth() > $this->getHeight()) {
                     $this->resizeInPixel($newWidth, null, true);
                     if ($this->getHeight() > $newHeight) {
                         $this->resizeInPixel(null, $newHeight, true);
                     }
                 } else {
                     $this->resizeInPixel(null, $newHeight, true);
                     if ($this->getWidth() > $newWidth) {
                         $this->resizeInPixel($newWidth, null, true);
                     }
                 }
                 if ($this->getWidth() != $newWidth || $this->getHeight() != $newHeight) {
                     $layerTmp = new self(array('width' => $newWidth, 'height' => $newHeight));
                     $layerTmp->addLayer(1, $this, round($positionX * ($newWidth / 100)), round($positionY * ($newHeight / 100)), $position);
                     $this->width = $layerTmp->getWidth();
                     $this->height = $layerTmp->getHeight();
                     unset($this->image);
                     unset($this->layerLevels);
                     unset($this->layerPositions);
                     unset($this->layers);
                     $this->image = $layerTmp->getImage();
                     $this->layerLevels = $layerTmp->getLayerLevels();
                     $this->layerPositions = $layerTmp->getLayerPositions();
                     $this->layers = $layerTmp->getLayers();
                     unset($layerTmp);
                 }
                 return;
             } elseif ($newWidth) {
                 $widthResizePourcent = $newWidth / ($this->width / 100);
                 $newHeight = round($widthResizePourcent / 100 * $this->height);
                 $heightResizePourcent = $widthResizePourcent;
             } elseif ($newHeight) {
                 $heightResizePourcent = $newHeight / ($this->height / 100);
                 $newWidth = round($heightResizePourcent / 100 * $this->width);
                 $widthResizePourcent = $heightResizePourcent;
             }
         } elseif ($newWidth && !$newHeight || !$newWidth && $newHeight) {
             // New width OR new height is given
             if ($newWidth) {
                 $widthResizePourcent = $newWidth / ($this->width / 100);
                 $heightResizePourcent = 100;
                 $newHeight = $this->height;
             } else {
                 $heightResizePourcent = $newHeight / ($this->height / 100);
                 $widthResizePourcent = 100;
                 $newWidth = $this->width;
             }
         } else {
             // New width AND new height are given
             $widthResizePourcent = $newWidth / ($this->width / 100);
             $heightResizePourcent = $newHeight / ($this->height / 100);
         }
         // Update the layer positions in the stack
         foreach ($this->layerPositions as $layerId => $layerPosition) {
             $newPosX = round($widthResizePourcent / 100 * $layerPosition['x']);
             $newPosY = round($heightResizePourcent / 100 * $layerPosition['y']);
             $this->layerPositions[$layerId] = array("x" => $newPosX, "y" => $newPosY);
         }
         // Resize layers in the stack
         $layers = $this->layers;
         foreach ($layers as $key => $layer) {
             $layer->resizeInPourcent($widthResizePourcent, $heightResizePourcent);
             $this->layers[$key] = $layer;
         }
         // Resize the layer
         $this->resizeBackground($newWidth, $newHeight);
     }
 }