Насильно вписываем изображение без учета пропорций в указанные рамки

@param \Intervention\Image\Image $img
@param                           $w
@param                           $h

@return \Intervention\Image\Image
482491
static function resizeIgnoringAspectRatio(\Intervention\Image\Image $img, $w, $h) {
        return $img->resize($w, $h);
    }
485396
public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null){
        if(empty($this->img)) E('没有可以被裁剪的图像资源');

        //设置保存尺寸
        empty($width)  && $width  = $w;
        empty($height) && $height = $h;

        //裁剪图片
        if('gif' == $this->info['type']){
            $img = $this->img->coalesceImages();
            $this->img->destroy(); //销毁原图

            //循环裁剪每一帧
            do {
                $this->_crop($w, $h, $x, $y, $width, $height, $img);
            } while ($img->nextImage());
            
            //压缩图片
            $this->img = $img->deconstructImages();
            $img->destroy(); //销毁零时图片
        } else {
            $this->_crop($w, $h, $x, $y, $width, $height);
        }
    }
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;
    }
435667
public function image($image, $width=null, $height=null, $alt=null, $class=null, $link=null, $caption=null, $srcset=null) {
		$image = $this->getLink($image);
		$attributes = $this->getHtmlAttributes(array(
			'width'=>$width,
			'height'=>$height,
			'alt'=>$alt,
			'caption'=>$caption,
			'class'=>$class,
			'srcset'=>$srcset
		));

		$img = "<img src='$image'$attributes />";
		if (!is_null($link)) {
			$link = $this->getLink($link);
			return "<a href='$link'>$img</a>";
		}

		return $img;
	}
952986
public function createLogoWatermark($imagePath, $width, $height, $prefix = 'wm_') {
		$imagick = new Imagick($this->dir.$this->filename);
	 	
	 	$wmImage = new Imagick($imagePath);
	 		
	 	//scale the size of the watermark image to be proportional to the size of the target image
	 	$wmWidth = $imagick->getImageWidth() * 0.4;
	 	$wmHeight = $imagick->getImageHeight() * 0.4;
	 	$wmImage->scaleImage($wmWidth, $wmHeight, true);
	 		
 		//set position of watermark on target image
 		$wmX = $imagick->getImageWidth() - $wmWidth;
 		$wmY = $imagick->getImageHeight() - $wmHeight;
 		
 		$imagick->compositeImage($wmImage, Imagick::COMPOSITE_BUMPMAP, $wmX, $wmY, Imagick::CHANNEL_ALPHA | Imagick::CHANNEL_OPACITY);

	 	//if image width or height is smaller than requested dimensions, then use the source image size
	 	$width = min($width, $imagick->getImageWidth());
	 	$height = min($height, $imagick->getImageHeight());
	 	if($width < 0 && $height < 0) {
	 		$width = $imagick->getImageWidth();
	 		$height = $imagick->getImageHeight();
	 	}
	 	
 		$bestfit = true;
		if ($this->defaultDimension == 'width') {
			$height = 0;
			$bestfit = false;
		} else if($this->defaultDimension == 'height') {
			$width = 0;
			$bestfit = false;
		}
		
		$result = $imagick->thumbnailImage($width, $height, $bestfit);
		if ($result) {
			$thumbName = $this->createThumbName($this->filename, $prefix);
			$imagick->writeImage($this->dir.$thumbName);
			$imagick->clear();
			$imagick->destroy();
		
			return $thumbName;
		}
		
		return false;
	}
846649
private function getMinTargetSize($w, $h)
    {
        extract($this->getInfo());

        if ($w > $width or $h > $height) {
            $w = $width;
            $h = $height;
        }

        if ($w > $h) {
            extract($this->getFilesize($w, 0));
        } else {
            extract($this->getFilesize(0, $h));
        }

        $this->targetSize = compact('width', 'height');
    }
362097
public static BufferedImage getSubimage( BufferedImage image, int x, int y, int w, int h ) {
		BufferedImage newImage = new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB );
		Graphics2D g = newImage.createGraphics();
		g.drawRenderedImage( image, AffineTransform.getTranslateInstance(-x, -y) );
		g.dispose();
		return newImage;
	}
271004
public function createImage($width, $height, $format)
    {
        $image = new \Imagick();
        $pixel = new \ImagickPixel('none');
        $image->newImage(intval($width), intval($height), $pixel);
        $image->setImageFormat($format);
        $this->colorNumber = $image->getImageColors();
        $this->pixelNumber = 0;
        $content = $image->getimageblob();
        $image->destroy();
        unset($image);
        return $content;
    }
1222741
public function setImage($image)
    {
        if (getimagesize($image)) {
            if (strpos($image, 'http') === false) {
                loader()->loadHelper('url');
                $image = images_url($image);
            }

            $this->setObject('image', $image);
        }

        return $this;
    }
703437
public function constructAmpImg($oneImgWithContent, $height, $width, $imgSrc, $imgAlt) {
        $ampImgTagOpen = "<amp-img height=$height width=$width src='$imgSrc' alt='$imgAlt' media='(min-width: 775px)' >"; // fully constructed amp img opening tag
        
        $ampImgTagOpenResponsive = "<amp-img height=$height width=$width src='$imgSrc' alt='$imgAlt' media='(max-width: 775px)' layout='responsive'>"; // fully constructed amp img opening tag - RESPONSIVE version
        
        // Fully replace the img tag with the constructed amp-img tag
        $oneAmpImgWithContent = preg_replace("/<img(.*?)>/", "$ampImgTagOpen</amp-img>$ampImgTagOpenResponsive</amp-img>", $oneImgWithContent);
        
        return $oneAmpImgWithContent;
    }
458287
protected function resizeExact(ImageInterface $image, $width, $height)
    {
        return $image->resize(new Box($width, $height));
    }
680326
function image($img_url, $x, $y, $w, $h, $resolution = "normal") {
    $w = (int)$w;
    $h = (int)$h;

    $img_type = Image_Cache::detect_type($img_url);
    $img_ext  = Image_Cache::type_to_ext($img_type);

    if ( isset($this->_imgs[$img_url]) ) {
      $img = $this->_imgs[$img_url];
    }
    else {
      $img = $this->_imgs[$img_url] = $this->_pdf->load_image($img_type, $img_url, "");
    }

    $y = $this->y($y) - $h;
    $this->_pdf->fit_image($img, $x, $y, "boxsize=\{$w $h\} fitmethod=entire");
  }
602335
public function addImage($image)
    {
        if (is_array($image)) {
            $this->images = array_merge($this->images, $image);
        } elseif (is_string($image)) {
            $this->images[] = $image;
        }

        return $this;
    }
796673
public function actionCrop($file_id, $x, $y, $w, $h)
    {
        if(Yii::$app->request->isAjax)
        {
            // find file by Id
            $oneFile = OneFile::findOne($file_id);
            // load model for path to file and image sizes
            $modelName = $oneFile->model;
            $model = $modelName::findOne($oneFile->parent_id);
            
            $path = $model->getFilePath($oneFile->subdir, '', true);
            $file = $oneFile->name;
            // load original image
            $image = new SimpleImage($path . 'original/' . $file);
            // crop
            $image->crop($w, $h, $x, $y);
            // choice images with type main or with param crop = true
            $sizes = [];
            foreach($model->sizes as $type => $params) {
                if($type == 'main' || (isset($params['crop']) && $params['crop']))
                    $sizes[$type] = $params;
            }
            // resize & save
            $image->resizeSave($path, $file, $sizes);

            // return the image and it's id that you just added
            echo json_encode(['files' => [[
                "name" => $oneFile->original, 
                "url" => $model->getImageByName($oneFile->name, $oneFile->subdir, ''), 
                "thumbnailUrl" => $model->getImageByName($oneFile->name, $oneFile->subdir, 'thumb'), 
                'id' => $oneFile->id,
            ]]]);
        }
    }
146925
public function addImage(Image $image)
    {
        // Find the image based on UUID inside the internal state.
        $existingImage = $this->images->findImageByUUID($image->getMediaObjectId());

        if ($existingImage === null) {
            $this->apply(
                $this->createImageAddedEvent($image)
            );
        }
    }
181368
protected function fitInBounds($w, $h, $width, $height)
    {
        if ($width >= $w or $height >= $h) {

            $ratA = $this->ratio($width, $height);
            $ratM = min($ratA, $this->ratio($w, $h));

            $isR = $ratM === $ratA;

            $valW = (int)round($h * $ratM);
            $valH = (int)round($w / $ratA);

            list($width, $height) = $width <= $height ? [$valW, $isR ? $h : $valH] : [$isR ? $w : $valW, $valH];
        }

        return compact('width', 'height');
    }
1168856
public function cropAndResize($filePath, $x, $y, $h, $w, array $format)
    {
        $image = $this->imagickFactory->create($filePath);
        $image->cropImage($w, $h, $x, $y);
        $image = $this->resizeImage($format, $image);

        $pathInfo = pathinfo($filePath);
        $croppedFilePath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . time() . $pathInfo['basename'];

        $this->saveImage($croppedFilePath, $image, $format['compression_quality']);

        return $croppedFilePath;
    }