/** * Crop the document * * $backgroundColor can be set transparent (but script could be long to execute) * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html * * @param string $unit * @param float $width * @param float $height * @param float $positionX * @param float $positionY * @param string $position * @param string $backgroundColor */ public function crop($unit = "pixel", $width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = "LT", $backgroundColor = "ffffff") { if ($unit == "pourcent") { $width = round($width / 100 * $this->width); $height = round($height / 100 * $this->height); $positionX = round($positionX / 100 * $this->width); $positionY = round($positionY / 100 * $this->height); } if ($width != $this->width || $positionX == 0 || ($height != $this->height || $positionY == 0)) { $layerTmp = new static(array('width' => $width, 'height' => $height, 'backgroundColor' => $backgroundColor)); $layerClone = new static(array('width' => $this->width, 'height' => $this->height)); imagedestroy($layerClone->image); $layerClone->image = $this->image; $layerTmp->addLayer(1, $layerClone, -$positionX, -$positionY, $position); $newPos = $layerTmp->getLayerPositions(); $layerNewPosX = $newPos[1]['x']; $layerNewPosY = $newPos[1]['y']; // update the layer $this->width = $layerTmp->getWidth(); $this->height = $layerTmp->getHeight(); $this->image = $layerTmp->getResult(); unset($layerTmp); unset($layerClone); $this->updateLayerPositionsAfterCropping($layerNewPosX, $layerNewPosY); } }
/** * Make a placeholder image from a file * @return placeholder */ public static function fromFile(UploadedFile $file) { $placeholder = new static(); $placeholder->file = $file; return $placeholder->fill(['name' => $placeholder->fileName(), 'path' => $placeholder->filePath(), 'thumbnail_path' => $placeholder->thumbnailPath(), 'width' => $placeholder->getWidth(), 'height' => $placeholder->getHeight()]); }