GdThumb::crop PHP Method

crop() public method

Vanilla Cropping - Crops from x,y with specified width and height
public crop ( integer $startX, integer $startY, integer $cropWidth, integer $cropHeight ) : GdThumb
$startX integer
$startY integer
$cropWidth integer
$cropHeight integer
return GdThumb
    public function crop($startX, $startY, $cropWidth, $cropHeight)
    {
        // validate input
        if (!is_numeric($startX)) {
            throw new InvalidArgumentException('$startX must be numeric');
        }
        if (!is_numeric($startY)) {
            throw new InvalidArgumentException('$startY must be numeric');
        }
        if (!is_numeric($cropWidth)) {
            throw new InvalidArgumentException('$cropWidth must be numeric');
        }
        if (!is_numeric($cropHeight)) {
            throw new InvalidArgumentException('$cropHeight must be numeric');
        }
        // do some calculations
        $cropWidth = $this->currentDimensions['width'] < $cropWidth ? $this->currentDimensions['width'] : $cropWidth;
        $cropHeight = $this->currentDimensions['height'] < $cropHeight ? $this->currentDimensions['height'] : $cropHeight;
        // ensure everything's in bounds
        if ($startX + $cropWidth > $this->currentDimensions['width']) {
            $startX = $this->currentDimensions['width'] - $cropWidth;
        }
        if ($startY + $cropHeight > $this->currentDimensions['height']) {
            $startY = $this->currentDimensions['height'] - $cropHeight;
        }
        if ($startX < 0) {
            $startX = 0;
        }
        if ($startY < 0) {
            $startY = 0;
        }
        // create the working image
        if (function_exists('imagecreatetruecolor')) {
            $this->workingImage = imagecreatetruecolor($cropWidth, $cropHeight);
        } else {
            $this->workingImage = imagecreate($cropWidth, $cropHeight);
        }
        $this->preserveAlpha();
        imagecopyresampled($this->workingImage, $this->oldImage, 0, 0, $startX, $startY, $cropWidth, $cropHeight, $cropWidth, $cropHeight);
        $this->oldImage = $this->workingImage;
        $this->currentDimensions['width'] = $cropWidth;
        $this->currentDimensions['height'] = $cropHeight;
        return $this;
    }

Usage Example

コード例 #1
0
ファイル: Image.php プロジェクト: bkwld/croppa
 /**
  * Trim the source before applying the crop with offset percentages
  *
  * @param  array $coords Cropping instructions as percentages
  * @return $this
  */
 public function trimPerc($coords)
 {
     list($x1, $y1, $x2, $y2) = $coords;
     $size = (object) $this->thumb->getCurrentDimensions();
     // Convert percentage values to what GdThumb expects
     $x = round($x1 * $size->width);
     $y = round($y1 * $size->height);
     $width = round($x2 * $size->width - $x);
     $height = round($y2 * $size->height - $y);
     $this->thumb->crop($x, $y, $width, $height);
     return $this;
 }