/** * Apply a border on the image. * * @param \Imagick|null $sourceImage If provided will apply effects on this resource * @param array $extraParameters Contain extraParameters. Key: `border` * * @return \Imagick * @throws ChateauLeTapImageServerBundleLogicException */ public function run($sourceImage = null, array $extraParameters = null) { if (null !== $extraParameters && true === is_array($extraParameters) && 0 < count($extraParameters) && true === array_key_exists('rounded', $extraParameters)) { $this->effectParam = $extraParameters['border']; } $color = 'black'; $size = null; $width = 2; $height = 2; if (true === is_string($this->effectParam)) { $this->effectParam = strtolower(trim($this->effectParam)); if (0 < strpos($this->effectParam, '.')) { $params = explode('.', $this->effectParam); $color = array_shift($params); $size = array_shift($params); } elseif (0 < strpos($this->effectParam, 'x')) { $size = $this->effectParam; } else { $color = $this->effectParam; } } if (null !== $size) { if (0 < strpos($size, 'x')) { $sizeParams = explode('x', strtolower(trim($size))); $width = strtolower(trim(array_shift($sizeParams))); $height = strtolower(trim(array_shift($sizeParams))); } } $image = parent::run($sourceImage, $extraParameters); $image->borderimage($color, $width, $height); // TODO: logInfo //$this->logInfo("[ImageController::roundedImage] rounding [".$rounding."]"); $this->result = $image; return $image; }
/** * Apply a rounded effect. * * @param \Imagick|null $sourceImage If provided will apply effects on this resource * @param array $extraParameters Contain extraParameters. Key: `rounded` * * @return \Imagick * @throws ChateauLeTapImageServerBundleLogicException */ public function run($sourceImage = null, array $extraParameters = null) { if (null !== $extraParameters && true === is_array($extraParameters) && 0 < count($extraParameters) && true === array_key_exists('rounded', $extraParameters)) { $this->effectParam = $extraParameters['rounded']; } if (true === is_array($this->effectParam)) { $this->effectParam = 0; } $roundingX = $roundingY = $rounding = (int) str_replace('rounded', '', $this->effectParam); if (0 < strpos($rounding, 'x')) { list($roundingX, $roundingY) = explode('x', $rounding); } $image = parent::run($sourceImage, $extraParameters); $image->roundCorners($roundingX, $roundingY); // TODO: logInfo //$this->logInfo("[ImageController::roundedImage] rounding [".$rounding."]"); $this->result = $image; return $image; }
/** * * Apply a blur effect. * * @param \Imagick|null $sourceImage If provided will apply effects on this resource * @param array $extraParameters Contain extraParameters. Key: `blur` * * @return \Imagick * @throws ChateauLeTapImageServerBundleLogicException */ public function run($sourceImage = null, array $extraParameters = null) { if (null !== $extraParameters && true === is_array($extraParameters) && 0 < count($extraParameters) && true === array_key_exists('rounded', $extraParameters)) { $this->effectParam = $extraParameters['blur']; } if (true === is_array($this->effectParam)) { $this->effectParam = 0; } $image = parent::run($sourceImage, $extraParameters); $imageWidth = (int) $image->getimagewidth(); $imageHeight = (int) $image->getimageheight(); $radius = (int) max([$imageWidth, $imageHeight]); $sigma = (int) str_replace('blur', '', $this->effectParam); $image->blurimage($radius, $sigma); // TODO: logInfo //$this->logInfo("[ImageController::roundedImage] rounding [".$rounding."]"); $this->result = $image; return $image; }
/** * Apply a default effect depending on: * - Upscale ? * - Respect ratio ? * - Width wanted * - Height wanted * - Image filter * * @param \Imagick|null $image If provided will apply effects on this resource * @param array $extraParameters Contain extraParameters. Key: `default` * * @return \Imagick * @throws ChateauLeTapImageServerBundleLogicException */ public function run($image = null, array $extraParameters = null) { if (null !== $extraParameters && true === is_array($extraParameters) && 0 < count($extraParameters) && true === array_key_exists('default', $extraParameters)) { $this->effectParam = $extraParameters['default']; } if (false === is_array($this->effectParam)) { $newEffectParam = []; if (0 < strpos(trim($this->effectParam), 'x')) { list($widthWanted, $heightWanted) = explode('x', trim($this->effectParam)); $newEffectParam['widthWanted'] = $widthWanted; $newEffectParam['heightWanted'] = $heightWanted; } $this->effectParam = $newEffectParam; } if (true === array_key_exists('noUpscale', $this->effectParam) && true === is_bool($this->effectParam['noUpscale'])) { $noUpscale = (bool) $this->effectParam['noUpscale']; } else { $noUpscale = $this->getNoUpscale(); } if (true === array_key_exists('respectRatio', $this->effectParam) && true === is_bool($this->effectParam['respectRatio'])) { $respectRatio = (bool) $this->effectParam['respectRatio']; } else { $respectRatio = $this->getRespectRatio(); } if (true === array_key_exists('widthWanted', $this->effectParam)) { $outputWidth = (int) $this->effectParam['widthWanted']; } else { $outputWidth = $this->getWidthWanted(); } if (true === array_key_exists('heightWanted', $this->effectParam)) { $outputHeight = (int) $this->effectParam['heightWanted']; } else { $outputHeight = $this->getHeightWanted(); } if (true === array_key_exists('imageFilter', $this->effectParam)) { $imageFilter = $this->effectParam['imageFilter']; } else { $imageFilter = $this->getImageFilter(); } $image = parent::run($image, $extraParameters); $sourceImageWidth = (int) $image->getImageWidth(); $sourceImageHeight = (int) $image->getImageHeight(); $imageRatio = $sourceImageWidth / $sourceImageHeight; //check if given size is not bigger than source image sizes if (true === $noUpscale) { if ($sourceImageWidth < $outputWidth) { $outputWidth = $sourceImageWidth; } if ($sourceImageHeight < $outputHeight) { $outputHeight = $sourceImageHeight; } } //check if ratio is respected if (0 >= $outputWidth || 0 >= $outputHeight) { if (0 >= $outputWidth && 0 < $outputHeight) { $outputWidth = $outputHeight * $imageRatio; } if (0 >= $outputHeight && 0 < $outputWidth) { $outputHeight = $outputWidth / $imageRatio; } if (0 >= $outputWidth && 0 >= $outputHeight) { $outputWidth = $sourceImageWidth; $outputHeight = $sourceImageHeight; } } else { $outputImageRatio = $outputWidth / $outputHeight; if ($outputImageRatio === $imageRatio || false === $respectRatio) { // Nothing more to do } else { $potentialHeight = $outputWidth / $imageRatio; $potentialWidth = $outputHeight * $imageRatio; if ($potentialHeight > $outputHeight) { $outputWidth = $potentialWidth; } else { $outputHeight = $potentialHeight; } } } $image->resizeImage($outputWidth, $outputHeight, $imageFilter, 0); $this->result = $image; return $image; }
/** * Apply a crop effect depending on: * - Width wanted * - Height wanted * * @param \Imagick|null $sourceImage If provided will apply effects on this resource * @param array $extraParameters Contain extraParameters. Key: `crop` * * @return \Imagick * @throws ChateauLeTapImageServerBundleLogicException */ public function run($sourceImage = null, array $extraParameters = null) { if (null !== $extraParameters && true === is_array($extraParameters) && 0 < count($extraParameters) && true === array_key_exists('crop', $extraParameters)) { $this->effectParam = $extraParameters['crop']; } if (false === is_array($this->effectParam)) { if (0 < strpos(trim($this->effectParam), '.')) { list($position, $size) = explode('.', trim($this->effectParam)); $this->effectParam = ['xPosition' => 0, 'yPosition' => 0, 'widthWanted' => 0, 'heightWanted' => 0]; if (0 < strpos(trim($position), 'x')) { list($xPosition, $yPosition) = explode('x', trim($position)); $this->effectParam['xPosition'] = $xPosition; $this->effectParam['yPosition'] = $yPosition; } if (0 < strpos(trim($size), 'x')) { list($widthWanted, $heightWanted) = explode('x', trim($size)); $this->effectParam['widthWanted'] = $widthWanted; $this->effectParam['heightWanted'] = $heightWanted; } } elseif (0 < strpos(trim($this->effectParam), 'x')) { list($widthWanted, $heightWanted) = explode('x', trim($this->effectParam)); $this->effectParam = ['xPosition' => 0, 'yPosition' => 0, 'widthWanted' => 0, 'heightWanted' => 0]; $this->effectParam['widthWanted'] = $widthWanted; $this->effectParam['heightWanted'] = $heightWanted; } } if (true === array_key_exists('widthWanted', $this->effectParam) && 0 < (int) $this->effectParam['widthWanted']) { $outputWidth = (int) $this->effectParam['widthWanted']; } else { $outputWidth = $this->getWidthWanted(); } if (true === array_key_exists('heightWanted', $this->effectParam) && 0 < (int) $this->effectParam['heightWanted']) { $outputHeight = (int) $this->effectParam['heightWanted']; } else { $outputHeight = $this->getHeightWanted(); } if (true === array_key_exists('xPosition', $this->effectParam)) { $x = (int) $this->effectParam['xPosition']; } else { $x = 0; } if (true === array_key_exists('yPosition', $this->effectParam)) { $y = (int) $this->effectParam['yPosition']; } else { $y = 0; } $image = parent::run($sourceImage, $extraParameters); $sourceImageWidth = (int) $image->getImageWidth(); $sourceImageHeight = (int) $image->getImageHeight(); if (0 >= $outputWidth || $sourceImageWidth < $outputWidth) { $outputWidth = $sourceImageWidth; } if (0 >= $outputHeight || $sourceImageHeight < $outputHeight) { $outputHeight = $sourceImageHeight; } if (0 >= $x || $sourceImageHeight <= $x) { $x = 0; } if (0 >= $y || $sourceImageHeight <= $y) { $y = 0; } $image->cropImage($outputWidth, $outputHeight, $x, $y); // TODO: logInfo //$this->logInfo("[ImageController::cropImage] width [".$outputWidth."] height [".$outputHeight."] x [".$x."] y [".$y."]"); $this->result = $image; return $image; }