使用imagick扩展生成验证码图片
@param int $img_width
@param int $img_height
@param bool $addRandomLines
@param bool $swirl
@return array
352409
static function verifycode_imagick($img_width = 80, $img_height = 30, $addRandomLines = true, $swirl = true)
    {
        $fontSize = 24;

        /* imagick对象 */
        $Imagick = new \Imagick();

         /* 背景对象 */
        $bg = new \ImagickPixel();

        /* Set the pixel color to white */
        $bg->setColor('rgb(235,235,235)');

        /* 画刷 */
        $ImagickDraw = new \ImagickDraw();

        if (is_file(self::$verifyCodeFontFile))
        {
            $ImagickDraw->setFont(self::$verifyCodeFontFile);
        }

        $ImagickDraw->setFontSize($fontSize);
        $ImagickDraw->setFillColor('black');

        $code = strtoupper(RandomKey::string(self::$verifyCodeLength));

        /* Create new empty image */
        $Imagick->newImage($img_width, $img_height, $bg);

        /* Write the text on the image */
        $Imagick->annotateImage($ImagickDraw, 4, 20, 0, $code);

        /* 变形 */
        if ($swirl)
        {
            $Imagick->swirlImage(10);
        }

        /* 随即线条 */
        if ($addRandomLines)
        {
            $ImagickDraw->line(rand(0, 70), rand(0, 30), rand(0, 70), rand(0, 30));
            $ImagickDraw->line(rand(0, 70), rand(0, 30), rand(0, 70), rand(0, 30));
            $ImagickDraw->line(rand(0, 70), rand(0, 30), rand(0, 70), rand(0, 30));
            $ImagickDraw->line(rand(0, 70), rand(0, 30), rand(0, 70), rand(0, 30));
            $ImagickDraw->line(rand(0, 70), rand(0, 30), rand(0, 70), rand(0, 30));
        }

        /* Draw the ImagickDraw object contents to the image. */
        $Imagick->drawImage($ImagickDraw);

        /* Give the image a format */
        $Imagick->setImageFormat('png');

        /* Send headers and output the image */
        return array('image' => $Imagick->getImageBlob(), 'code' => $code);
    }
242187
def addImg(self, img):
        '''
        
        '''
        # self.opts['imgs'].append(img)

        self.img = imread(img, 'gray', 'uint8')

        didFindCorners, corners = self.method()
        self.opts['foundPattern'].append(didFindCorners)

        if didFindCorners:
            self.findCount += 1
            self.objpoints.append(self.objp)
            self.opts['imgPoints'].append(corners)
        return didFindCorners
725067
def fbcorr(imgs, filters):
    n_imgs, n_rows, n_cols, n_channels = imgs.shape
    n_filters, height, width, n_ch2 = filters.shape
    output = numpy.zeros((n_imgs, n_filters, (n_rows - height + 1),(n_cols - width + 1)))
    ""
    for ii in xrange(n_imgs):
        for rr in xrange(n_rows - height + 1):
            for cc in xrange(n_cols - width + 1):
                for hh in xrange(height):
                    for ww in xrange(width):
                        for jj in xrange(n_channels):
                            for ff in xrange(n_filters):
                                imgval = imgs[ii,rr + hh,cc + ww,jj]
                                filterval = filters[ff,hh,ww,jj]
                                output[ii,ff,rr,cc] += imgval * filterval
    return output
527006
function uploadImg(faceTarget) {
    return function(err, img) {
      --numToLoad;
      if (err) {
        errors.push(err);
      } else {
        if (img.width !== img.height) {
          errors.push("cubemap face img is not a square: " + img.src);
        } else {
          savePackState(gl, options);
          gl.bindTexture(target, tex);

          // So assuming this is the first image we now have one face that's img sized
          // and 5 faces that are 1x1 pixel so size the other faces
          if (numToLoad === 5) {
            // use the default order
            getCubeFaceOrder(gl).forEach(function(otherTarget) {
              // Should we re-use the same face or a color?
              gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
            });
          } else {
            gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
          }

          restorePackState(gl, options);
          if (shouldAutomaticallySetTextureFilteringForSize(options)) {
            gl.generateMipmap(target);
          }
        }
      }

      if (numToLoad === 0) {
        callback(errors.length ? errors : undefined, tex, imgs);
      }
    };
  }
959534
public function create($width, $height, $type = null)
    {
        if ( !is_numeric($width) ) {
            throw new \InvalidArgumentException('Image create failed, width must be numeric');
        }

        if ( !is_numeric($height) ) {
            throw new \InvalidArgumentException('Image create failed, height must be numeric');
        }

        $type = $this->getType($type);

        if ($type !== 'gif' && function_exists('imagecreatetruecolor'))
        {
            $newImage = imagecreatetruecolor($width, $height); // Unsupport gif file
        }
        else
        {
            $newImage = imagecreate($width, $height);
        }

        imagealphablending($newImage, true);

        $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 0);

        imagefilledrectangle($newImage, 0, 0, imagesx($newImage), imagesy($newImage), $transparent);
        imagefill($newImage, 0, 0, $transparent);

        imagesavealpha($newImage, true);

        $this->newImage = $newImage;

        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;
    }
1324263
private function gd_apply_overlay($im, $type, $amount)
		#
		# Original Author:    Marc Hibbins
		# License:  Attribution-ShareAlike 3.0
		# Purpose:
		# Params in:
		# Params out:
		# Notes:
		#
	{
		$width = imagesx($im);
		$height = imagesy($im);
		$filter = imagecreatetruecolor($width, $height);

		imagealphablending($filter, false);
		imagesavealpha($filter, true);

		$transparent = imagecolorallocatealpha($filter, 255, 255, 255, 127);
		imagefilledrectangle($filter, 0, 0, $width, $height, $transparent);

		// *** Resize overlay
		$overlay = $this->filterOverlayPath . '/' . $type . '.png';
		$png = imagecreatefrompng($overlay);
		imagecopyresampled($filter, $png, 0, 0, 0, 0, $width, $height, imagesx($png), imagesy($png));

		$comp = imagecreatetruecolor($width, $height);
		imagecopy($comp, $im, 0, 0, 0, 0, $width, $height);
		imagecopy($comp, $filter, 0, 0, 0, 0, $width, $height);
		imagecopymerge($im, $comp, 0, 0, 0, 0, $width, $height, $amount);

		imagedestroy($comp);

		return $im;
	}
317784
def img_box(img, box):
    
    if isinstance(box, tuple):
        box = Box.from_tup(box)

    if len(img.shape) == 3:
        return img[box.y:box.y + box.height, box.x:box.x + box.width, :]
    else:
        return img[box.y:box.y + box.height, box.x:box.x + box.width]
445090
def vignettingFromRandomSteps(imgs, bg, inPlane_scale_factor=None,
                              debugFolder=None, **kwargs):
    '''
    
    '''
    # TODO: inPlane_scale_factor
    if debugFolder:
        debugFolder = PathStr(debugFolder)

    s = ObjectVignettingSeparation(imgs[0], bg,  **kwargs)
    for img in imgs[1:]:
        fit = s.addImg(img)

        if debugFolder and fit is not False:
            imwrite(debugFolder.join('fit_%s.tiff' % len(s.fits)), fit)

    if debugFolder:
        imwrite(debugFolder.join('init.tiff'), s.flatField)

    smoothed_ff, mask, flatField, obj = s.separate()

    if debugFolder:
        imwrite(debugFolder.join('object.tiff'), obj)
        imwrite(debugFolder.join('flatfield.tiff'), flatField, dtype=float)
        imwrite(debugFolder.join('flatfield_smoothed.tiff'), smoothed_ff,
                dtype=float)

    return smoothed_ff, mask
1188307
@Override
	public Planar<T> createNew(int imgWidth, int imgHeight) {
		return new Planar<>(type, imgWidth, imgHeight, bands.length);
	}
1333679
public static BufferedImage joinImages(List<BufferedImage> images, int lines, int height, int minHeight, boolean scale) {

      // Array of input images.
      List<BufferedImage> input = new ArrayList<BufferedImage>();

      int actualHeight = height;

      // if not scale, start from 1, so will use the max height of images, not more than that.
      if (!scale) {
        actualHeight = 1;
      }

      for (BufferedImage imageToJoin : images) {
        try {
          BufferedImage img = MyImageUtils.trim(imageToJoin);

          if (scale) {
            actualHeight = Integer.min(actualHeight, img.getHeight());
          } else {
            actualHeight = Integer.max(actualHeight, img.getHeight());
          }
          input.add(img);

        } catch (Exception x) {
          x.printStackTrace();
        }
      }

      // minimum height for the posts is 480
      if (scale && actualHeight < minHeight 
          && input.size() > 0) {
        actualHeight = minHeight;
      }

      int totalWidth = 0;
      List<BufferedImage> fixedInput = new ArrayList<BufferedImage>();
      for (BufferedImage img : input) {
        BufferedImage newImg = img;
        if (scale && img.getHeight() > actualHeight) {
          newImg = MyImageUtils.resizeToHeight(img, actualHeight);
        }

        fixedInput.add(newImg);

        totalWidth += newImg.getWidth();
      }

      // Create the output image and fill with white.
      
      BufferedImage output = getWhiteImage(actualHeight * lines, totalWidth / lines);
      drawImages(lines, scale, input, actualHeight, output.getGraphics());
      
      return output;
  }
171945
function overlay(Image $img,$align=NULL,$alpha=100) {
		if (is_null($align))
			$align=self::POS_Right|self::POS_Bottom;
		if (is_array($align)) {
			list($posx,$posy)=$align;
			$align = 0;
		}
		$ovr=imagecreatefromstring($img->dump());
		imagesavealpha($ovr,TRUE);
		$imgw=$this->width();
		$imgh=$this->height();
		$ovrw=imagesx($ovr);
		$ovrh=imagesy($ovr);
		if ($align & self::POS_Left)
			$posx=0;
		if ($align & self::POS_Center)
			$posx=($imgw-$ovrw)/2;
		if ($align & self::POS_Right)
			$posx=$imgw-$ovrw;
		if ($align & self::POS_Top)
			$posy=0;
		if ($align & self::POS_Middle)
			$posy=($imgh-$ovrh)/2;
		if ($align & self::POS_Bottom)
			$posy=$imgh-$ovrh;
		if (empty($posx))
			$posx=0;
		if (empty($posy))
			$posy=0;
		if ($alpha==100)
			imagecopy($this->data,$ovr,$posx,$posy,0,0,$ovrw,$ovrh);
		else {
			$cut=imagecreatetruecolor($ovrw,$ovrh);
			imagecopy($cut,$this->data,0,0,$posx,$posy,$ovrw,$ovrh);
			imagecopy($cut,$ovr,0,0,0,0,$ovrw,$ovrh);
			imagecopymerge($this->data,
				$cut,$posx,$posy,0,0,$ovrw,$ovrh,$alpha);
		}
		return $this->save();
	}
910315
public function createTextWatermark($text, $width, $height, $prefix = 'wm_', $textAngle = -30.0, $textOpacity = 0.4) {
		$imagick = new Imagick($this->dir.$this->filename);
	 	
 	 	$draw = new ImagickDraw();
	 	
	 	//set font size proportional to target image size
	 	$fontSize = min($imagick->getImageWidth(), $imagick->getImageHeight()) * 0.12;
	 	$draw->setFontSize($fontSize);
	 	
	 	//text transparency
	 	$draw->setFillOpacity($textOpacity);
	 	
	 	//write text in specified positions
	 	$draw->setGravity(Imagick::GRAVITY_CENTER);
	 	$imagick->annotateImage($draw, 0, 0, $textAngle, $text);
	 	$draw->setGravity(Imagick::GRAVITY_NORTH);
	 	$imagick->annotateImage($draw, 0, 0, $textAngle, $text);
	 	$draw->setGravity(Imagick::GRAVITY_SOUTH);
	 	
	 	$imagick->annotateImage($draw, 0, 0, $textAngle, $text);
 	
	 	//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;
	}
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;
	}
491846
public static function image_widthin($src, $dest, $w, $h) {
        $img = getimagesize(WEB_ROOT . $src);
        if (!$img) {
            return false;
        }
        $nw = $img[0];
        $nh = $img[1];
        if ($nw > $w && $w > 0) {
            $nh = (($w / $nw) * $nh);
            $nw = $w;
        }
        if ($nh > $h && $h > 0) {
            $nw = (($h / $nh) * $nw);
            $nh = $h;
        }
        if ($nw != $img[0] || $nh != $img[1]) {
            self::image_resize(WEB_ROOT . $src, WEB_ROOT . $dest, $nw, $nh);
        } else {
            copy(WEB_ROOT . $src, WEB_ROOT . $dest);
        }
        return $dest;
    }
995235
function _getColorByXY(coord, imgSize, imgCols) {
    var colPos = _getColorPositionByXY(coord, imgSize);
    return [imgCols[colPos], imgCols[colPos+1], imgCols[colPos+2], imgCols[colPos+3]];
}
1220437
function getNewImageDimentions(img, width, height) {
    var detImg = img.width / img.height;
    if (width > 0 && height > 0) {
      // 同时指定了宽高，按原图缩放
      if (width / height > detImg) {
        height = width / detImg;
      } else {
        width = height * detImg;
      }
      return [ width, height ];
    } else if (width > 0) {
      // 只指定宽度的情况
      return [ width, width / detImg ];
    } else if (height > 0) {
      // 只指定高度的情况
      return [ height * detImg, height ];
    } else {
      // 否则原 size 返回
      return [ img.width, img.height ];
    }
  }