public function crop() { # source $cropFrom = Request::get('crop-from'); $cropTo = Request::get('crop-to'); $fromPath = $this->getFullPath($cropFrom); $toPath = $this->getFullPath($cropTo); $layer = ImageWorkshop::initFromPath($fromPath); # cropping $newWidth = Request::get('width'); // px $newHeight = Request::get('height'); // px $positionX = Request::get('x'); // left translation of 0px $positionY = Request::get('y'); // top translation of 0px $position = "LT"; // from Left Top corner $layer->cropInPixel($newWidth, $newHeight, $positionX, $positionY, $position); # saving $fileName = basename($toPath); $dirName = dirname($toPath); $createFolders = false; $backgroundColor = null; // transparent, only for PNG (otherwise it will be white if set null) $imageQuality = 95; // useless for GIF, usefull for PNG and JPEG (0 to 100%) $layer->save($dirName, $fileName, $createFolders, $backgroundColor, $imageQuality); return Response::json($cropTo); }
/** * 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 mixed $width (integer or float) * @param mixed $height (integer or float) * @param mixed $positionX (integer or float) * @param mixed $positionY (integer or float) * @param string $position */ public function crop($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT') { if ($width < 0 || $height < 0) { throw new ImageWorkshopLayerException('You can\'t use negative $width or $height for "' . __METHOD__ . '" method.', static::ERROR_NEGATIVE_NUMBER_USED); } if ($unit == self::UNIT_PERCENT) { $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)) { if ($width == 0) { $width = 1; } if ($height == 0) { $height = 1; } $layerTmp = ImageWorkshop::initVirginLayer($width, $height); $layerClone = ImageWorkshop::initVirginLayer($this->width, $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); } }