Newscoop\Image\ImageService::calculateCutout PHP Метод

calculateCutout() публичный статический Метод

Calculates dimensions of cutout in image.
public static calculateCutout ( $srcWidth, $srcHeight, $left, $top, $newWidth, $newHeight ) : array
Результат array
    public static function calculateCutout($srcWidth, $srcHeight, $left, $top, $newWidth, $newHeight)
    {
        if (substr($newWidth, -1) === '%') {
            $newWidth = round($srcWidth / 100 * $newWidth);
        }
        if (substr($newHeight, -1) === '%') {
            $newHeight = round($srcHeight / 100 * $newHeight);
        }
        if (substr($left, -1) === '%') {
            $left = round(($srcWidth - $newWidth) / 100 * $left);
        }
        if (substr($top, -1) === '%') {
            $top = round(($srcHeight - $newHeight) / 100 * $top);
        }
        if ($left < 0) {
            $newWidth += $left;
            $left = 0;
        }
        if ($top < 0) {
            $newHeight += $top;
            $top = 0;
        }
        $newWidth = min((int) $newWidth, $srcWidth - $left);
        $newHeight = min((int) $newHeight, $srcHeight - $top);
        return array($left, $top, $newWidth, $newHeight);
    }

Usage Example

Пример #1
0
 /**
  * Generate image
  *
  * @param string $imagePath
  * @return Imagine\Gd\Image
  */
 public function generateImage($imagePath)
 {
     $path = is_file(APPLICATION_PATH . '/../' . $imagePath) ? APPLICATION_PATH . '/../' . $imagePath : $imagePath;
     $imagine = ImageService::getImagine();
     $image = $imagine->open($path);
     $imageSize = $image->getSize();
     if ($this->isCrop()) {
         $cropSpecs = explode('_', $this->getSpecs());
         if (count($cropSpecs) === 1) {
             list($width, $height) = ImageService::calculateSize($imageSize->getWidth(), $imageSize->getHeight(), $this->width, $this->height, $this->getFlags());
             $image->resize(new Box($width, $height));
             list($left, $top, $width, $height) = ImageService::calculateCutout($width, $height, '50%', '50%', $this->width, $this->height);
             $image->crop(new Point($left, $top), new Box($width, $height));
         } else {
             list(, $x0, $y0, $x1, $y1) = $cropSpecs;
             $image->crop(new Point($x0, $y0), new Box($x1 - $x0, $y1 - $y0));
             $imageSize = $image->getSize();
             list($width, $height) = ImageService::calculateSize($imageSize->getWidth(), $imageSize->getHeight(), $this->width, $this->height, $this->getFlags());
             $image->resize(new Box($width, $height));
         }
     } else {
         list($width, $height) = ImageService::calculateSize($imageSize->getWidth(), $imageSize->getHeight(), $this->width, $this->height, $this->getFlags());
         $image->resize(new Box($width, $height));
     }
     return $image;
 }