Example #1
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);
     }
 }