复制并调整图片的一部分
@param Image $srcImage 本图
@param int $srcX
@param int $srcY
@param int $x
@param int $y
@param int $srcWidth 如果是0则取原图的宽
@param int $srcHeight 如果是0则取原图的高
@param int $width 如果是0则取本图的宽
@param int $height 如果是0则取本图的高
@return bool
976838
public function copyFromWithResize(Image $srcImage, $srcX = 0, $srcY = 0, $x = 0, $y = 0, $srcWidth = 0, $srcHeight = 0, $width = 0, $height = 0) {
		if (empty($srcWidth)) {
			$srcWidth = $srcImage->getWidth();
		}
		if (empty($srcHeight)) {
			$srcHeight = $srcImage->getHeight();
		}
		if (empty($width)) {
			$width = $this->getWidth();
		}
		if (empty($height)) {
			$height = $this->getHeight();
		}
		return imagecopyresized($this->image, $srcImage->image, $x, $y, $srcX, $srcY, $width, $height, $srcWidth, $srcHeight);
	}
674163
public function resize($width, $height, $ratio = false, $interlace = false) {

		$image = ['width' => imagesx($this -> image), 'height' => imagesy($this -> image)];

		//Set width and height
		if($height == 0 && $width > 0) {
			$height = round($image['height'] / ($image['width'] / $width), 0);
		}
		elseif($width == 0 && $height > 0) {
			$width = round($image['width'] / ($image['height'] / $height), 0);
		}

		$x = 0;
		$y = 0;

		//Ratio
		if(true === $ratio) {

			$ratio 		= [$image['width'] / $image['height'], $width / $height];
			$tmp_width 	= $image['width'];
			$tmp_height = $image['height'];

			if($ratio[0] > $ratio[1]) {

				$image['width'] = $image['height'] * $ratio[1];
				$x = ($tmp_width - $image['width']) / 2;
			}
			elseif($ratio[0] < $ratio[1]) {

				$image['height'] = $image['width'] / $ratio[1];
				$y = ($tmp_height - $image['height']) / 2;
			}
		}

		//Resizing
		return $this -> createImage($x, $y, $image['width'], $image['height'], $width, $height, $interlace);
	}
1055223
public function createThumbnail($width, $height, $prefix = 'thumb_') {
		//imagecreatefromjpeg may run out of memory and cause a fatal error, check if image will fit in memory before processing
		$this->checkMemory();
		
		//determine image type
	 	$imagetype = exif_imagetype($this->dir . $this->filename);
	 	if($imagetype == IMAGETYPE_JPEG) {
	 		$srcImg = imagecreatefromjpeg($this->dir . $this->filename);	
	 	} else if($imagetype == IMAGETYPE_GIF) {
	 		$srcImg = imagecreatefromgif($this->dir . $this->filename);
	 	} else if($imagetype == IMAGETYPE_PNG) {
	 		$srcImg = imagecreatefrompng($this->dir . $this->filename);
	 	} else {
	 		throw new Exception('Unsupported imagetype code ' . $imagetype);
	 	}
	 	
	 	//determine thumbnail size keeping aspect ratio
	 	$old_x=imageSX($srcImg);
		$old_y=imageSY($srcImg);
		
		if ($height > $old_y) {
			$height = $old_y;
			$width = $old_y;
		}
		if ($width > $old_x) {
			$width = $old_x;
			$height = $old_x;
		}
		
		if ($this->defaultDimension == 'height') {
			$thumb_w = $old_x*($width/$old_y);
			$thumb_h = $height;
		} else if ($this->defaultDimension == 'width') {
			$thumb_w = $width;
			$thumb_h = $old_y*($height/$old_x);
		} else {
			if ($old_x > $old_y) {
				$thumb_w = $width;
				$thumb_h = $old_y*($height/$old_x);
			} else if ($old_x < $old_y) {
				$thumb_w = $old_x*($width/$old_y);
				$thumb_h = $height;
			} else if ($old_x == $old_y) {
				$thumb_w = $width;
				$thumb_h = $height;
			}
		}
		
		//create image resource
		if($imagetype != IMAGETYPE_GIF) {
			$dstImg = ImageCreateTrueColor($thumb_w, $thumb_h);
		} else {
			$dstImg = imagecreate($thumb_w, $thumb_h);
		}
		
		//preserve transparency for png
		if($imagetype == IMAGETYPE_PNG) {
			imagealphablending($dstImg, false);
			imagesavealpha($dstImg, true);
		} else if($imagetype == IMAGETYPE_GIF) {
			imagecolorallocate($dstImg, 255, 255, 255);
		}
		
		imagecopyresampled($dstImg,$srcImg,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
		
		$thumbName = $this->createThumbName($this->filename, $prefix);
		
		if($imagetype == IMAGETYPE_PNG) {
			imagepng($dstImg, $this->dir . $thumbName, 4);
		} else if($imagetype == IMAGETYPE_GIF) {
			imagegif($dstImg, $this->dir . $thumbName);	 
		} else {
			imagejpeg($dstImg, $this->dir . $thumbName, 100);
		}	
	
		imagedestroy($dstImg);
		imagedestroy($srcImg);
		
		return $thumbName;
	}
646567
public function getProsize(Int $width = 0, Int $height = 0)
    {
        if( ! isset($this->sets['filePath']) )
        {
            return false;
        }

        return $this->image->getProsize($this->sets['filePath'], $width, $height);
    }
656692
public function copyFromWithReSampling(Image $srcImage, $srcX = 0, $srcY = 0, $x = 0, $y = 0, $srcWidth = 0, $srcHeight = 0, $width = 0, $height = 0) {
		if (empty($srcWidth)) {
			$srcWidth = $srcImage->getWidth();
		}
		if (empty($srcHeight)) {
			$srcHeight = $srcImage->getHeight();
		}
		if (empty($width)) {
			$width = $this->getWidth();
		}
		if (empty($height)) {
			$height = $this->getHeight();
		}
		return imagecopyresampled($this->image, $srcImage->image, $x, $y, $srcX, $srcY, $width, $height, $srcWidth, $srcHeight);
	}
1118574
public function crop( $x, $y, $width, $height )
	{
		// check for auto
		if ( $x == 'center' || $x == 'auto' )
		{
			$x = ( $this->width / 2 ) - ( $width / 2 );
		}

		if ( $y == 'middle' || $y == 'auto' )
		{
			$y = ( $this->height / 2 ) - ( $height / 2 );
		}

		$result = imagecreatetruecolor( $width, $height );  
		ImageCopyResampled( $result, $this->image_context, 0, 0, $x, $y, $width, $height, $width, $height );

		$this->image_context = $result;

		// update properties
		$this->reload_context_info();

		// return self
		return $this;
	}
951041
private function getImageHeight(Image $image, int $width): int
    {
        return (int)($width * ($image->getHeight() / $image->getWidth()));
    }
1342116
public function thumb($width, $height, $type = Image::IMAGE_THUMB_SCALE){
        if(empty($this->img)) E('没有可以被缩略的图像资源');

        //原图宽度和高度
        $w = $this->info['width'];
        $h = $this->info['height'];

        /* 计算缩略图生成的必要参数 */
        switch ($type) {
            /* 等比例缩放 */
            case Image::IMAGE_THUMB_SCALE:
                //原图尺寸小于缩略图尺寸则不进行缩略
                if($w < $width && $h < $height) return;

                //计算缩放比例
                $scale = min($width/$w, $height/$h);
                
                //设置缩略图的坐标及宽度和高度
                $x = $y = 0;
                $width  = $w * $scale;
                $height = $h * $scale;
                break;

            /* 居中裁剪 */
            case Image::IMAGE_THUMB_CENTER:
                //计算缩放比例
                $scale = max($width/$w, $height/$h);

                //设置缩略图的坐标及宽度和高度
                $w = $width/$scale;
                $h = $height/$scale;
                $x = ($this->info['width'] - $w)/2;
                $y = ($this->info['height'] - $h)/2;
                break;

            /* 左上角裁剪 */
            case Image::IMAGE_THUMB_NORTHWEST:
                //计算缩放比例
                $scale = max($width/$w, $height/$h);

                //设置缩略图的坐标及宽度和高度
                $x = $y = 0;
                $w = $width/$scale;
                $h = $height/$scale;
                break;

            /* 右下角裁剪 */
            case Image::IMAGE_THUMB_SOUTHEAST:
                //计算缩放比例
                $scale = max($width/$w, $height/$h);

                //设置缩略图的坐标及宽度和高度
                $w = $width/$scale;
                $h = $height/$scale;
                $x = $this->info['width'] - $w;
                $y = $this->info['height'] - $h;
                break;

            /* 填充 */
            case Image::IMAGE_THUMB_FILLED:
                //计算缩放比例
                if($w < $width && $h < $height){
                    $scale = 1;
                } else {
                    $scale = min($width/$w, $height/$h);
                }

                //设置缩略图的坐标及宽度和高度
                $neww = $w * $scale;
                $newh = $h * $scale;
                $posx = ($width  - $w * $scale)/2;
                $posy = ($height - $h * $scale)/2;

                do{
                    //创建新图像
                    $img = imagecreatetruecolor($width, $height);
                    // 调整默认颜色
                    $color = imagecolorallocate($img, 255, 255, 255);
                    imagefill($img, 0, 0, $color);

                    //裁剪
                    imagecopyresampled($img, $this->img, $posx, $posy, $x, $y, $neww, $newh, $w, $h);
                    imagedestroy($this->img); //销毁原图
                    $this->img = $img;
                } while(!empty($this->gif) && $this->gifNext());
                
                $this->info['width']  = $width;
                $this->info['height'] = $height;
                return;

            /* 固定 */
            case Image::IMAGE_THUMB_FIXED:
                $x = $y = 0;
                break;

            default:
                E('不支持的缩略图裁剪类型');
        }

        /* 裁剪图像 */
        $this->crop($w, $h, $x, $y, $width, $height);
    }
1207529
public function wp_calculate_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
		foreach ( $sources as $width => $source ) {
			$size   = $this->find_image_size_from_width( $image_meta['sizes'], $width );
			$OS_url = $this->swift_get_attachment_url( $attachment_id, null, $size );
			if ( false === $OS_url || is_wp_error( $OS_url ) ) {
				continue;
			}
			$sources[ $width ]['url'] = $OS_url;
		}
		return $sources;
	}
936540
public function resize(int $width = 0, int $height = 0)
    {
        if (!$width && !$height) {
            throw new ImageException('You must supply at least one dimension');
        }
        $iw = $this->info[0];
        $ih = $this->info[1];
        if (!$height || !$width) {
            if (!$width) {
                $width  = $height / $ih * $iw;
            }
            if (!$height) {
                $height = $width  / $iw * $ih;
            }
        }
        $di = imagecreatetruecolor($width, $height);
        imagecopyresized($di, $this->data, 0, 0, 0, 0, $width, $height, $iw, $ih);
        $this->data = $di;
    }
534780
public function copyAndMergeFromWithGray(Image $srcImage, $x = 0, $y = 0, $opacity = 50, $srcX = 0, $srcY = 0, $srcWidth = 0, $srcHeight = 0) {
		if (empty($srcWidth)) {
			$srcWidth = $srcImage->getWidth();
		}
		if (empty($srcHeight)) {
			$srcHeight = $srcImage->getHeight();
		}
		return imagecopymergegray($this->image, $srcImage->image, $x, $y, $srcX, $srcY, $srcWidth, $srcHeight, $opacity);
	}
330056
public static function thumbnail($filename, $width, $height, $mode = ManipulatorInterface::THUMBNAIL_INSET)
    {
        $img = static::getImagine()->open(Yii::getAlias($filename));
        $sourceBox = $img->getSize();
        $thumbnailBox = static::getThumbnailBox($sourceBox, $width, $height);
        if (($sourceBox->getWidth() <= $thumbnailBox->getWidth() && $sourceBox->getHeight() <= $thumbnailBox->getHeight()) || (!$thumbnailBox->getWidth() && !$thumbnailBox->getHeight())) {
            return $img->copy();
        }
        $img = $img->thumbnail($thumbnailBox, $mode);
        // create empty image to preserve aspect ratio of thumbnail
        $thumb = static::getImagine()->create($thumbnailBox, new Color(static::$thumbnailBackgroundColor, static::$thumbnailBackgroundAlpha));
        // calculate points
        $startX = 0;
        $startY = 0;
        if ($sourceBox->getWidth() < $width) {
            $startX = ceil($width - $sourceBox->getWidth()) / 2;
        }
        if ($sourceBox->getHeight() < $height) {
            $startY = ceil($height - $sourceBox->getHeight()) / 2;
        }
        $thumb->paste($img, new Point($startX, $startY));
        return $thumb;
    }
906443
private int getSourcePixel(int[] srcImage, int x, int y)
    {
        int x1 = Math.max(0, x);
        x1 = Math.min(width - 1, x1);
        int y1 = Math.max(0, y);
        y1 = Math.min(height - 1, y1);

        return srcImage[x1 + y1 * width];
    }
1262088
protected function createEmptyCanvas($width, $height, $rgb, $real)
    {
        $width  = $this->width  ?? $width;
        $height = $this->height ?? $height;
        $rgb    = $this->color  ?? $rgb;
        $real   = $this->real   ?? $real;

        $this->imageSize = [$width, $height];

        if( $real === false )
        {
            $this->canvas = imagecreate($width, $height);
        }
        else
        {
            $this->canvas = imagecreatetruecolor($width, $height);
        }

        if( ! empty($rgb) )
        {
            $this->allocate($rgb);
        }
    }
538116
public static function CnVerify($length = 4, $type = 'png', $width = 180, $height = 50, $font = 'tahoma.ttf', $verifyName = 'verifyCode')
    {
        $code = StringProcess::randString($length, 4);
        $width = ($length * 45) > $width ? $length * 45 : $width;
        $authKey = md5(mt_rand() . microtime());
        Cookie::set($verifyName, $authKey);
        Model::getInstance()->cache()->set($authKey, md5($code), 1800);
        $im = imagecreatetruecolor($width, $height);
        $borderColor = imagecolorallocate($im, 100, 100, 100);  //边框色
        $bkcolor = imagecolorallocate($im, 250, 250, 250);
        imagefill($im, 0, 0, $bkcolor);
        imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
        // 干扰
        for ($i = 0; $i < 15; $i++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
        }
        for ($i = 0; $i < 255; $i++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $fontcolor);
        }
        if (!is_file($font)) {
            $font = CML_EXTEND_PATH . DIRECTORY_SEPARATOR . $font;
        }
        for ($i = 0; $i < $length; $i++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
            $codex = StringProcess::substrCn($code, $i, 1);
            imagettftext($im, mt_rand(16, 20), mt_rand(-60, 60), 40 * $i + 20, mt_rand(30, 35), $fontcolor, $font, $codex);
        }
        self::output($im, $type);
    }
564521
public function resize($width, $height) {
        if (!$width) $width = 1;
        if (!$height) $height = 1;
        return (
            (false !== ($img = new gd(array($width, $height)))) &&
            $img->imagecopyresampled($this) &&
            (false !== ($this->image = $img->get_image())) &&
            (false !== ($this->width = $img->get_width())) &&
            (false !== ($this->height = $img->get_height()))
        );
    }
669101
public function manipulate(AbstractImage $image, $width, $height)
    {
        $box = $this->calculateResize($image, $width, $height);

        return $image->resize($box);
    }