Image Crop

@param   integer  $top     Top point to crop from
@param   integer  $right   Right point to crop from
@param   integer  $bottom  Bottom point to crop from
@param   integer  $left    Left point to crop from
@return  void
347588
public function crop($top, $right = 0, $bottom = 0, $left = 0)
	{
		if (empty($this->resource))
		{
			return false;
		}
		$width      = imagesx($this->resource);
		$height     = imagesy($this->resource);
		$new_width  = $width - ($left + $right);
		$new_height = $height - ($top + $bottom);

		$resource = imagecreatetruecolor($new_width, $new_height);
		imagecopy($resource, $this->resource, 0, 0, $left, $top, $new_width, $new_height);

		imagedestroy($this->resource);
		$this->resource = $resource;
	}
1073275
protected function crop($image, $options)
    {
        return $image->crop(new Point($options['point'][0], $options['point'][1]), new Box($options['box'][0], $options['box'][1]));
    }
573580
static function cropPicture($sourcefile, $destfile, $fw, $fh, $jpegquality = 80)
    {
        list($ow, $oh, $from_type) = getimagesize($sourcefile);
        switch($from_type)
        {
            case 1: // GIF
                $srcImage = imageCreateFromGif($sourcefile) or die('Impossible de convertir cette image');
                break;
            case 2: // JPG
                $srcImage = imageCreateFromJpeg($sourcefile) or die('Impossible de convertir cette image');
                break;
            case 3: // PNG
                $srcImage = imageCreateFromPng($sourcefile) or die('Impossible de convertir cette image');
                break;
            default:
                return;
        }
        if (($fw / $ow) > ($fh / $oh))
        {
            $tempw = $fw;
            $temph = ($fw / $ow) * $oh;
        }
        else {
            $tempw = ($fh / $oh) * $ow;
            $temph = $fh;
        }
        $tempImage = imageCreateTrueColor($fw, $fh);
        //    imageAntiAlias($tempImage, true);
        imagecopyresampled($tempImage, $srcImage, ($fw - $tempw) / 2, ($fh - $temph) / 2, 0, 0, $tempw, $temph, $ow, $oh);
        imageJpeg($tempImage, $destfile, $jpegquality);
        return getimagesize($destfile);
    }
8156
public function process()
    {
        parent::process();

        $data = $this->getForm()->getFormData();

        $this->crop_width = $data[$this->id . '_width'];
        $this->crop_height = $data[$this->id . '_height'];
        $this->crop_left = $data[$this->id . '_x'];
        $this->crop_top = $data[$this->id . '_y'];

        // deprecated aliases
        $this->crop_box_width = $this->crop_width;
        $this->crop_box_height = $this->crop_height;
        $this->crop_box_left = $this->crop_left;
        $this->crop_box_top = $this->crop_top;
    }
1170353
private void calculateCropping(CmsPositionBean position) {

        m_croppingParam.setCropHeight((int)Math.round(m_heightRatio * position.getHeight()));
        m_croppingParam.setCropWidth((int)Math.round(m_widthRatio * position.getWidth()));
        m_croppingParam.setCropY((int)Math.round(m_heightRatio * position.getTop()));
        m_croppingParam.setCropX((int)Math.round(m_widthRatio * position.getLeft()));
    }
1177782
public function setPosition($left, $top)
    {
        $params = [
            'x' => intval($left),
            'y' => intval($top)
        ];
        $command = $this->driver->factoryCommand(
            'window/' . $this->windowHandle . '/position',
            WebDriver_Command::METHOD_POST,
            $params
        );
        return $this->driver->curl($command)['value'];
    }
414413
public function cropAction ()
    {
        $this->validateRequest();
        
        $imageManager = $this->container->get('thrace_media.imagemanager');
        $name = $this->getRequest()->get('name');
        $options = array();
        
        $options['x'] = $this->getRequest()->get('x', false);
        $options['y'] = $this->getRequest()->get('y', false);
        $options['w'] = $this->getRequest()->get('w', false);
        $options['h'] = $this->getRequest()->get('h', false);
        
        if (!$imageManager->crop($name, $options)){
            return new JsonResponse(array(
                'success' => false,
                'err_msg' => $this->container->get('translator')
                    ->trans('error.image_crop', array('name' => $name), 'ThraceMediaBundle')
            ));
        }
       
        $hash = $imageManager->checksumTemporaryFileByName($name);
        
        return new JsonResponse(array(
            'success' => true,
            'name'    => $name,
            'hash'    => $hash
        ));
    }
809368
final public function crop($x = 0, $y = 0, $width = null, $height = null)
	{
		$this->_image_handle = imagecrop(
			$this->_image_handle,
			array_merge(
				array(
					'x' => 0,
					'y' => 0,
					'width' => $this->imageWidth(),
					'height' => $this->imageHeight()
				),
				array_filter(
					array_combine(
						array('x', 'y', 'width', 'height'),
						array($x, $y, $width, $height)
					)
				)
			)
		);

		return $this;
	}
659255
public function cropImage($original, $path, $width, $height)
    {
        $original = $this->getFullPath($original);

        // Create folder if it doesn't exist
        $pathArray = explode('/', $path);
        array_pop($pathArray);
        $folder = implode('/', $pathArray);
        if (! file_exists($folder)) {
            mkdir($folder, 0777, true);
        }

        $file = \File::get($original);

        $img = \Image::make($file)->crop($width, $height);

        $img->save($path);

        return $this->showFile($path);
    }
776597
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;
	}
379144
public static function cropImg($image, $newImg, $width, $height, array $start = [0, 0])
    {
        return YiiImage::crop($image, $width, $height, $start)
            ->interlace(ImageInterface::INTERLACE_LINE)
            ->save($newImg, ['quality' => 100]);
    }
1073840
private function calculateCrop(array $parameters, array $imageSize) {
        $focalX = $parameters['poi'][0];
        $focalY  = $parameters['poi'][1];

        $sourceWidth = $imageSize['width'];
        $sourceHeight = $imageSize['height'];
        $sourceRatio  = $sourceWidth / $sourceHeight;

        $targetWidth = $parameters['width'];
        $targetHeight = $parameters['height'];
        $targetRatio  = $targetWidth / $targetHeight;

        $growFactor = $this->getGrowFactor($parameters['closeness']);
        $sourcePortionThreshold = $this->getSourcePercentageThreshold($parameters['closeness']);

        if ($sourceRatio >= $targetRatio) {
            // Image is wider than needed, crop from the sides
            $cropWidth = (int) ceil(
                $targetRatio * max(
                    min($sourceHeight, $targetHeight * $growFactor),
                    $sourceHeight * $sourcePortionThreshold
                )
            );
            $cropHeight = (int) floor($cropWidth / $targetRatio);
        } else {
            // Image is taller than needed, crop from the top/bottom
            $cropHeight = (int) ceil(
                max(
                    min($sourceWidth, $targetWidth * $growFactor),
                    $sourceWidth * $sourcePortionThreshold
                ) / $targetRatio
            );
            $cropWidth = (int) floor($cropHeight * $targetRatio);
        }

        $cropTop = (int) ($focalY - floor($cropHeight / 2));
        $cropLeft = (int) ($focalX - floor($cropWidth / 2));

        // Make sure that we're not cropping outside the image on the x axis
        if ($cropLeft < 0) {
            $cropLeft = 0;
        } else if ($cropLeft + $cropWidth > $sourceWidth) {
            $cropLeft = $sourceWidth - $cropWidth;
        }

        // Make sure that we're not cropping outside the image on the y axis
        if ($cropTop < 0) {
            $cropTop = 0;
        } else if ($cropTop + $cropHeight > $sourceHeight) {
            $cropTop = $sourceHeight - $cropHeight;
        }

        return [
            'width' => $cropWidth,
            'height' => $cropHeight,
            'x' => $cropLeft,
            'y' => $cropTop,
        ];
    }
1015108
public boolean setMargins(float left, float right, float top, float bottom) {
        rtfDoc.getDocumentHeader().getPageSetting().setMarginLeft((int) (left * RtfElement.TWIPS_FACTOR));
        rtfDoc.getDocumentHeader().getPageSetting().setMarginRight((int) (right * RtfElement.TWIPS_FACTOR));
        rtfDoc.getDocumentHeader().getPageSetting().setMarginTop((int) (top * RtfElement.TWIPS_FACTOR));
        rtfDoc.getDocumentHeader().getPageSetting().setMarginBottom((int) (bottom * RtfElement.TWIPS_FACTOR));
        return true;
    }
792430
def crop_resize_image(image: np.ndarray, size) -> np.ndarray:
    
    width, height = image.size
    if width > height:
        left = (width - height) / 2
        right = width - left
        top = 0
        bottom = height
    else:
        top = (height - width) / 2
        bottom = height - top
        left = 0
        right = width
    image = image.crop((left, top, right, bottom))
    image = image.resize(size, Image.ANTIALIAS)
    return image
994592
public void setWindowPaddingTopRightBottomLeft(final int top, final int right,
            final int bottom, final int left) {
        JsArrayNumber array = JavaScriptObject.createArray(4).cast();
        array.push(top);
        array.push(right);
        array.push(bottom);
        array.push(left);
        attrMixin.setAttribute(WINDOW_PADDING, JsonUtils.stringify(array));
    }
539739
public function margins(int $top, int $right, int $bottom, int $left)
    {
        $this->margins = compact('top', 'right', 'bottom', 'left');
        return $this;
    }
82775
function crop(stream, config, cb) {
  PNGCrop.cropToStream(stream, config, function(err, outputStream) {
    var buf = concat(function(data) {
      cb(err, data);
    });
    outputStream.pipe(buf);
  });
}