Create a cropped image if needed
@return string
12819
public function maybeCropImage($data)
    {
        if (!$data['mod_image_crop']) {
            return $data['mod_image_image']['sizes'][$data['mod_image_size']];
        }

        $imageSrc = wp_get_attachment_image_src(
            $data['mod_image_image']['ID'],
            apply_filters('Modularity/image/image',
                municipio_to_aspect_ratio('16:9', array($data['mod_image_crop_width'], $data['mod_image_crop_height']))
            )
        );

        return $imageSrc[0];
    }
93298
public function generateRawData()
    {
        $rawData = ['id' => $this->getId()];
        $rawData['data'] = [];
        $rawData['data']['image'] = [];
        if ($this->getCreatedAt()) {
            $rawData['created_at'] = $this->getCreatedAt();
        }
        if ($this->getStatusDescription() || $this->getStatusCode()) {
            $rawData['status'] = $this->getStatus();
        }
        if ($this->getImage()) {
            $rawData['data']['image'][$this->getImageMethod()] = $this->getImage();
        }
        if ($this->getCrop()) {
            $rawData['data']['image']['crop'] = $this->getCrop();
        }
        if ($this->getConcepts()) {
            $rawData['data']['concepts'] = [];
            foreach ($this->getConcepts() as $concept) {
                $rawData['data']['concepts'][] = $concept->generateRawData();
            }
        }
        if ($this->getMetaData()) {
            $rawData['data']['metadata'] = $this->getMetaData();
        }

        return $rawData;
    }
137357
protected function do_crop(\stdClass $src, $top, $left, $bottom, $right) {

		$dst = new \stdClass();
		$dst->width		= $src->width - $left - $right;
		$dst->height	= $src->height - $top - $bottom;
		$dst->resource	= imagecreatetruecolor($dst->width, $dst->height);

		if($this->mimeType == 'image/png' || $this->mimeType == 'image/gif') {
			imagealphablending($dst->resource, FALSE);
			imagesavealpha($dst->resource, TRUE);
		}

		imagecopy(
			$dst->resource,
			$src->resource,
			0, 0,
			$left, $top,
			$src->width - $left - $right, $src->height - $top - $bottom
		);

		return $dst;
	}
844859
public function cropImage($x, $y, $width, $height, $rotate)
    {
        $DS = DIRECTORY_SEPARATOR;
        $savePath = Yii::getAlias($this->_savePathAlias);
        $imageSrc = $this->owner->{$this->_imageAttribute};
        $fullImagePath = $savePath . $DS . $imageSrc;

        $imageComponent = static::getImageComponent();
        /* @var $image Image_GD|Image_Imagick */
        $image = $imageComponent->load($this->getOriginalImagePath());

        $image->crop($width, $height, $x, $y);
        $image->rotate($rotate);

        $image->save($fullImagePath);

        $sizes = $this->getImageSizes();
        unset($sizes['']);

        $pathParts = explode('/', str_replace('\\', '/', $imageSrc));
        $filename = array_pop($pathParts);

        // Make resize
        return static::resizeAndSave($savePath . $DS . implode($DS, $pathParts), $filename, $sizes);
    }
882809
public function crop($x, $y, $w, $h, $destFile = null) {
        $file = $this->file;
        if ($this->file) {
            $image = new \image ($this->file);
            $ow    = $image->imagesx();
            $oh    = $image->imagesy();
            if (empty ($w)) {
                $w = $ow - $x;
            } else if ($w < 0) {
                $w = $ow + $w - $x;
            }
            if ($w <= 0) {
                return $this->file;
            }
            if (empty ($h)) {
                $h = $oh - $y;
            } else if ($h < 0) {
                $h = $oh + $h - $y;
            }
            if ($h <= 0) {
                return $this->file;
            }

            $tx = $x;
            $ty = $y;
            $nw = $w;
            $nh = $h;

            $fx = new \image_fx_crop ($tx, $ty, $nw, $nh);
            $image->attach($fx);
            if (!$destFile) {
                $destFile = $this->file;
            }
            if ($image->save($destFile)) {
                $file = $destFile;
            } else {
                $file = null;
            }
            $image->destroyImage();
        }

        return $file;
    }
446059
def create_crop(self, name, file_obj,
                    x=None, x2=None, y=None, y2=None):
        

        if name not in self._registry:
            return

        file_obj.seek(0)
        im = Image.open(file_obj)
        config = self._registry[name]

        if x is not None and x2 and y is not None and y2 and not config.editable:
            # You can't ask for something special
            # for non editable images
            return

        im = config.rotate_by_exif(im)
        crop_spec = config.get_crop_spec(im, x=x, x2=x2, y=y, y2=y2)
        image = config.process_image(im, crop_spec=crop_spec)
        if image:
            crop_name = utils.get_size_filename(file_obj.name, name)
            self._save_file(image, crop_name)
            return crop_spec
962628
public function onLoadImageCropPopup()
    {
        $this->abortIfReadOnly();

        $path = Input::get('path');
        $path = MediaLibrary::validatePath($path);
        $cropSessionKey = md5(FormHelper::getSessionKey());
        $selectionParams = $this->getSelectionParams();

        $urlAndSize = $this->getCropEditImageUrlAndSize($path, $cropSessionKey);
        $width = $urlAndSize['dimensions'][0];
        $height = $urlAndSize['dimensions'][1] ?: 1;

        $this->vars['currentSelectionMode'] = $selectionParams['mode'];
        $this->vars['currentSelectionWidth'] = $selectionParams['width'];
        $this->vars['currentSelectionHeight'] = $selectionParams['height'];
        $this->vars['cropSessionKey'] = $cropSessionKey;
        $this->vars['imageUrl'] = $urlAndSize['url'];
        $this->vars['dimensions'] = $urlAndSize['dimensions'];
        $this->vars['originalRatio'] = round($width / $height, 5);
        $this->vars['path'] = $path;

        return $this->makePartial('image-crop-popup-body');
    }
1321849
protected function buildImageResize() {
    $image = Image::make($this->upload_path . $this->file_name)->orientate();

    $image->resize($this->preset->width, $this->preset->height , function ($constraint) {
      $constraint->aspectRatio();
      $constraint->upsize();
    });

    //if background_color is not set, do not resizeCanvas with black color, to avoid black zones in images. 
    if(isset($this->preset->background_color))
    {
      $image->resizeCanvas($this->preset->width, $this->preset->height, 'center', FALSE, $this->preset->background_color);
    }
    
    return $image;
  }
1153070
protected function createImage()
    {
        // create image
        $this->image = @imagecreatetruecolor($this->width, $this->height);

        // set mime type
        $this->mimeType = 'image/png';

        // set modified date
        $this->modified = time();
    }
15790
public function toArray()
	{
		return array(
			'type' 			=> self::TYPE_ID,
			'id' 			=> $this->imageId,
			'align' 		=> $this->align,
			'style' 		=> $this->style,

			'alternate_text'	=> $this->alternateText,
			'title' 		=> $this->title,
			'description' 	=> $this->description,

			'size_width' 	=> $this->width,
			'size_height' 	=> $this->height,
			'crop_top' 		=> $this->cropTop,
			'crop_left' 	=> $this->cropLeft,
			'crop_width' 	=> $this->cropWidth,
			'crop_height' 	=> $this->cropHeight,

			'size_name' 	=> $this->sizeName,
		);
	}
241541
public function make()
    {
        $this->image->encode($this->format, $this->quality);
        $data = $this->image->getEncoded();
        $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);
        $length = strlen($data);

        if (function_exists('app') && is_a($app = app(), 'Illuminate\Foundation\Application')) {

            $response = \Illuminate\Support\Facades\Response::make($data);
            $response->header('Content-Type', $mime);
            $response->header('Content-Length', $length);

        } else {

            header('Content-Type: ' . $mime);
            header('Content-Length: ' . $length);
            $response = $data;
        }

        return $response;
    }
1120885
public function getCroppableImageObject()
    {
        $requestID = Controller::curr()->request->requestVar('CroppableImageID');

        if ($requestID == '0' && !$this->Value()) {
            return;
        }

        if (!$this->linkObject) {
            $id = $this->Value() ? $this->Value() : $requestID;
            if ((int) $id) {
                $this->linkObject = SaltedCroppableImage::get()->byID($id);
            }
        }
        return $this->linkObject;
    }
1352494
public function onResizeImage()
    {
        $this->abortIfReadOnly();

        $cropSessionKey = Input::get('cropSessionKey');
        if (!preg_match('/^[0-9a-z]+$/', $cropSessionKey)) {
            throw new ApplicationException('Invalid input data');
        }

        $width = trim(Input::get('width'));
        if (!strlen($width) || !ctype_digit($width)) {
            throw new ApplicationException('Invalid input data');
        }

        $height = trim(Input::get('height'));
        if (!strlen($height) || !ctype_digit($height)) {
            throw new ApplicationException('Invalid input data');
        }

        $path = Input::get('path');
        $path = MediaLibrary::validatePath($path);

        $params = array(
            'width' => $width,
            'height' => $height
        );

        return $this->getCropEditImageUrlAndSize($path, $cropSessionKey, $params);
    }
353684
public function generateCrops()
    {
        $w = imagesx($this->oImg);
        $h = imagesy($this->oImg);
        $results = [];
        $minDimension = min($w, $h);
        $cropWidth = empty ($this->options ['cropWidth']) ? $minDimension : $this->options ['cropWidth'];
        $cropHeight = empty ($this->options ['cropHeight']) ? $minDimension : $this->options ['cropHeight'];
        for ($scale = $this->options ['maxScale']; $scale >= $this->options ['minScale']; $scale -= $this->options ['scaleStep'])
        {
            for ($y = 0; $y + $cropHeight * $scale <= $h; $y += $this->options ['step'])
            {
                for ($x = 0; $x + $cropWidth * $scale <= $w; $x += $this->options ['step'])
                {
                    $results [] = [
                        'x' => $x,
                        'y' => $y,
                        'width' => $cropWidth * $scale,
                        'height' => $cropHeight * $scale
                    ];
                }
            }
        }

        return $results;
    }
166612
protected function image()
    {
        $locator = Grav::instance()['locator'];

        $file = $this->get('filepath');

        // Use existing cache folder or if it doesn't exist, create it.
        $cacheDir = $locator->findResource('cache://images', true) ?: $locator->findResource('cache://images', true, true);

        // Make sure we free previous image.
        unset($this->image);

        $this->image = ImageFile::open($file)
            ->setCacheDir($cacheDir)
            ->setActualCacheDir($cacheDir)
            ->setPrettyName($this->getImagePrettyName());

        return $this;
    }
306640
public function crop()
    {
        if ($this->resizeWidth > $this->targetWidth) {
            $this->targetLeft = $this->getStartCrop($this->resizeWidth, $this->targetWidth, $this->focusLeft);
        }

        if ($this->resizeHeight > $this->targetHeight) {
            $this->targetTop = $this->getStartCrop($this->resizeHeight, $this->targetHeight, $this->focusTop);
        }

        return $this;
    }
477574
public function resizeCrop()
    {
        $sourceDimension = $this->sourceImageFile->getDimension();
        $resampleDimension = $this->resampleImageFile->getDimension();

        if ($resampleDimension->getOrientation() === 'SQUARE') {
            if ($sourceDimension->getOrientation() === 'LANDSCAPE') {
                $sourceSquareSize = $sourceDimension->getHeight();

                $sourceDimension = new Dimension(
                    $sourceSquareSize,
                    $sourceSquareSize,
                    ($sourceDimension->getWidth() - $sourceDimension->getHeight()) / 2,
                    0
                );
            } elseif ($sourceDimension->getOrientation() === 'PORTRAIT') {
                $sourceSquareSize = $sourceDimension->getWidth();

                $sourceDimension = new Dimension(
                    $sourceSquareSize,
                    $sourceSquareSize,
                    0,
                    ($sourceDimension->getHeight() - $sourceDimension->getWidth()) / 2
                );
            }
        } else {

            //try max width first...
            $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth();
            $resizeWidth = $resampleDimension->getWidth();
            $resizeHeight = $sourceDimension->getHeight() * $resizeRatio;

            //if that didn't work
            if ($resizeHeight > $resampleDimension->getHeight()) {
                $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight();
                $resizeHeight = $resampleDimension->getHeight();
                $resizeWidth = $sourceDimension->getWidth() * $resizeRatio;
            }

            switch ($resampleDimension->getFocus()) {
                default:
                case 'CENTER':

                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        ($sourceDimension->getWidth() / 2) - ($resizeWidth / 2),
                        ($sourceDimension->getHeight() / 2) - ($resizeHeight / 2)
                    );

                    break;
                case 'NORTH':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        ($sourceDimension->getWidth() - $resizeWidth) / 2,
                        0
                    );
                    break;
                case 'NORTHWEST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        0,
                        0
                    );
                    break;
                case 'NORTHEAST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        $sourceDimension->getWidth() - $resizeWidth,
                        0
                    );
                    break;
                case 'SOUTH':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        ($sourceDimension->getWidth() - $resizeWidth) / 2,
                        $sourceDimension->getHeight() - $resizeHeight
                    );
                    break;
                case 'SOUTHWEST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        0,
                        $sourceDimension->getHeight() - $resizeHeight
                    );
                    break;
                case 'SOUTHEAST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        $sourceDimension->getWidth() - $resizeWidth,
                        $sourceDimension->getHeight() - $resizeHeight
                    );
                    break;
                case 'WEST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        0,
                        ($sourceDimension->getHeight() - $resizeHeight) / 2
                    );
                    break;
                case 'EAST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        $sourceDimension->getWidth() - $resizeWidth,
                        ($sourceDimension->getHeight() - $resizeHeight) / 2
                    );
                    break;
            }
        }

        $resampleAxis = $this->resampleImageFile->getDimension()->getAxis();
        $sourceAxis = $sourceDimension->getAxis();

        if (function_exists('imagecreatetruecolor')) {
            $this->resampleImageResource = imagecreatetruecolor(
                $resampleDimension->getWidth(),
                $resampleDimension->getHeight()
            );

            imagealphablending($this->resampleImageResource, false);
            imagesavealpha($this->resampleImageResource, true);

            $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127);
            imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(),
                $resampleDimension->getHeight(), $transparent);

            return imagecopyresampled(
                $this->resampleImageResource,
                $this->sourceImageResource,
                $resampleAxis->getX(),
                $resampleAxis->getY(),
                $sourceAxis->getX(),
                $sourceAxis->getY(),
                $resampleDimension->getWidth(),
                $resampleDimension->getHeight(),
                $sourceDimension->getWidth(),
                $sourceDimension->getHeight()
            );
        } else {
            $this->resampleImageResource = imagecreate($resampleDimension->getWidth(),
                $resampleDimension->getHeight());

            imagealphablending($this->resampleImageResource, false);
            imagesavealpha($this->resampleImageResource, true);

            $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127);
            imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(),
                $resampleDimension->getHeight(), $transparent);

            return imagecopyresized(
                $this->resampleImageResource,
                $this->sourceImageResource,
                $resampleAxis->getX(),
                $resampleAxis->getY(),
                $sourceAxis->getX(),
                $sourceAxis->getY(),
                $resampleDimension->getWidth(),
                $resampleDimension->getHeight(),
                $sourceDimension->getWidth(),
                $sourceDimension->getHeight()
            );
        }
    }