예제 #1
0
파일: Layers.php 프로젝트: mm999/EduSoho
 /**
  * {@inheritdoc}
  */
 public function animate($format, $delay, $loops)
 {
     if ('gif' !== strtolower($format)) {
         throw new InvalidArgumentException('Animated picture is currently only supported on gif');
     }
     if (!is_int($loops) || $loops < 0) {
         throw new InvalidArgumentException('Loops must be a positive integer.');
     }
     if (null !== $delay && (!is_int($delay) || $delay < 0)) {
         throw new InvalidArgumentException('Delay must be either null or a positive integer.');
     }
     try {
         foreach ($this as $offset => $layer) {
             $this->resource->setIteratorIndex($offset);
             $this->resource->setFormat($format);
             if (null !== $delay) {
                 $this->resource->setImageDelay($delay / 10);
                 $this->resource->setImageTicksPerSecond(100);
             }
             $this->resource->setImageIterations($loops);
             $this->resource->setImage($layer->getImagick());
         }
     } catch (\ImagickException $e) {
         throw new RuntimeException('Failed to animate layers', $e->getCode(), $e);
     }
     return $this;
 }
예제 #2
0
파일: Video.php 프로젝트: renus/media
 /**
  * Generate the animated gif
  *
  * @return string binary image data
  */
 private function createAnimation($images)
 {
     $animation = new \Imagick();
     $animation->setFormat('gif');
     foreach ($images as $image) {
         $frame = new \Imagick();
         $frame->readImageBlob($image);
         $animation->addImage($frame);
         $animation->setImageDelay(50);
     }
     return $animation->getImagesBlob();
 }
예제 #3
0
파일: Layers.php 프로젝트: BeerMan88/yii
 /**
  * {@inheritdoc}
  */
 public function animate($format, $delay, $loops)
 {
     if ('gif' !== strtolower($format)) {
         throw new InvalidArgumentException('Animated picture is currently only supported on gif');
     }
     foreach (array('Loops' => $loops, 'Delay' => $delay) as $name => $value) {
         if (!is_int($value) || $value < 0) {
             throw new InvalidArgumentException(sprintf('%s must be a positive integer.', $name));
         }
     }
     try {
         foreach ($this as $offset => $layer) {
             $this->resource->setIteratorIndex($offset);
             $this->resource->setFormat($format);
             $this->resource->setImageDelay($delay / 10);
             $this->resource->setImageTicksPerSecond(100);
             $this->resource->setImageIterations($loops);
         }
     } catch (\ImagickException $e) {
         throw new RuntimeException('Failed to animate layers', $e->getCode(), $e);
     }
     return $this;
 }
예제 #4
0
 public function add_watermark($path, $x = 0, $y = 0)
 {
     $watermark = new Imagick($path);
     $draw = new ImagickDraw();
     $draw->composite($watermark->getImageCompose(), $x, $y, $watermark->getImageWidth(), $watermark->getimageheight(), $watermark);
     if ($this->type == 'gif') {
         $image = $this->image;
         $canvas = new Imagick();
         $images = $image->coalesceImages();
         foreach ($image as $frame) {
             $img = new Imagick();
             $img->readImageBlob($frame);
             $img->drawImage($draw);
             $canvas->addImage($img);
             $canvas->setImageDelay($img->getImageDelay());
         }
         $image->destroy();
         $this->image = $canvas;
     } else {
         $this->image->drawImage($draw);
     }
 }
예제 #5
0
 function waterMarkFont()
 {
     if ($this->param['water_mark_color']) {
         $color = $this->param['water_mark_color'];
     } else {
         $color = '#000000';
     }
     $r = hexdec(substr($color, 1, 2));
     $g = hexdec(substr($color, 3, 2));
     $b = hexdec(substr($color, 5, 2));
     $draw = new ImagickDraw();
     $draw->setFillColor(new ImagickPixel($color));
     //设置填充颜色
     $draw->setFont($this->param['water_mark_font']);
     //设置文本字体,要求ttf或者ttc字体,可以绝对或者相对路径
     $draw->setFontSize($this->param['water_mark_fontsize']);
     //设置字号
     switch ($this->param['water_mark_pos']) {
         case 0:
         case 1:
             $gravity = Imagick::GRAVITY_NORTHWEST;
             //'NorthWest';
             break;
         case 2:
             $gravity = Imagick::GRAVITY_NORTH;
             //'North';
             break;
         case 3:
             $gravity = Imagick::GRAVITY_NORTHEAST;
             //'NorthEast';
             break;
         case 4:
             $gravity = Imagick::GRAVITY_WEST;
             //'West';
             break;
         case 5:
             $gravity = Imagick::GRAVITY_CENTER;
             //'Center';
             break;
         case 6:
             $gravity = Imagick::GRAVITY_EAST;
             //'East';
             break;
         case 7:
             $gravity = Imagick::GRAVITY_SOUTHWEST;
             //'SouthWest';
             break;
         case 8:
             $gravity = Imagick::GRAVITY_SOUTH;
             //'South';
             break;
         case 9:
             $gravity = Imagick::GRAVITY_SOUTHEAST;
             break;
     }
     $draw->setGravity($gravity);
     if ($this->image_type == 'GIF') {
         $color_transparent = new ImagickPixel("transparent");
         $dest = new Imagick();
         foreach ($this->image as $frame) {
             $page = $frame->getImagePage();
             $tmp = new Imagick();
             $tmp->newImage($page['width'], $page['height'], $color_transparent, 'gif');
             $tmp->compositeImage($frame, Imagick::COMPOSITE_OVER, $page['x'], $page['y']);
             $tmp->annotateImage($draw, 0, 0, $this->param['water_mark_angle'], $this->param['water_mark_string']);
             $dest->addImage($tmp);
             $dest->setImagePage($tmp->getImageWidth(), $tmp->getImageHeight(), 0, 0);
             $dest->setImageDelay($frame->getImageDelay());
             $dest->setImageDispose($frame->getImageDispose());
         }
         $dest->coalesceImages();
         $this->image->destroy();
         $this->image = $dest;
     } else {
         if ($this->param['water_mark_opacity']) {
             $draw->setFillOpacity($this->param['water_mark_opacity'] / 100);
         }
         $this->image->annotateImage($draw, 0, 0, $this->param['water_mark_angle'], $this->param['water_mark_string']);
     }
 }
예제 #6
0
 /**
  * {@inheritdoc}
  */
 public function animate(array $frames, $delay = 20)
 {
     $gif = new \Imagick();
     $gif->setFormat('gif');
     foreach ($frames as $im) {
         if ($im instanceof Imanee) {
             $frame = $im->getResource()->getResource();
         } else {
             $frame = new \Imagick($im);
         }
         $frame->setImageDelay($delay);
         $gif->addImage($frame);
     }
     $imagickResource = new ImagickResource();
     $imagickResource->setResource($gif);
     $imanee = new Imanee();
     $imanee->setResource($imagickResource);
     $imanee->setFormat('gif');
     return $imanee;
 }
예제 #7
0
 /**
  * Resamples an image to a new copy
  *
  * @param Imagick $dst_image
  * @param Imagick $src_image
  * @param int $dst_x
  * @param int $dst_y
  * @param int $src_x
  * @param int $src_y
  * @param int $dst_w
  * @param int $dst_h
  * @param int $src_w
  * @param int $src_h
  * @return bool
  */
 function zp_resampleImage($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
 {
     foreach ($src_image->getImageProfiles() as $name => $profile) {
         $dst_image->profileImage($name, $profile);
     }
     $result = true;
     $src_image = $src_image->coalesceImages();
     foreach ($src_image as $frame) {
         $frame->cropImage($src_w, $src_h, $src_x, $src_y);
         $frame->setImagePage(0, 0, 0, 0);
     }
     $src_image = $src_image->coalesceImages();
     foreach ($src_image as $frame) {
         $frame->resizeImage($dst_w, $dst_h, Imagick::FILTER_LANCZOS, 1);
         $dst_image->setImageDelay($frame->getImageDelay());
         $result &= $dst_image->compositeImage($frame, Imagick::COMPOSITE_OVER, $dst_x, $dst_y);
         if ($dst_image->getNumberImages() < $src_image->getNumberImages()) {
             $result &= $dst_image->addImage(zp_createImage($dst_image->getImageWidth(), $dst_image->getImageHeight()));
         }
         if (!$result) {
             break;
         }
     }
     return $result;
 }
예제 #8
0
 /**
  * 截取图片
  *
  * @param int $x 开始坐标x
  * @param int $y 坐标y
  * @param int $width
  * @param int $height
  * @param string $savepath 文件存放路径,未提供则修改当前
  */
 public function watchmark($x, $y, $savepath = NULL)
 {
     if (!$this->waterImage instanceof \Imagick) {
         throw new MediaException('Watermark source is invalid.');
     }
     if ($format == 'GIF') {
         $format = $this->getImageFormat();
         $transparent = new \ImagickPixel("rgba(0, 0, 0, 0)");
         // 透明色
         $tmpImage = new \Imagick();
         foreach ($this->image as $frame) {
             $page = $frame->getImagePage();
             $tmp = new \Imagick();
             $tmp->newImage($page['width'], $page['height'], $transparent, $format);
             $tmp->compositeImage($frame, \Imagick::COMPOSITE_DEFAULT, $page['x'], $page['y']);
             if (min($this->getOriginalSize()) > $this->waterStart) {
                 $tmp->compositeImage($this->waterImage, \Imagick::COMPOSITE_DEFAULT, $x, $y);
             }
             $tmpImage->addImage($tmp);
             $tmpImage->setImagePage($tmp->getImageWidth(), $tmp->getImageHeight(), 0, 0);
             $tmpImage->setImageDelay($frame->getImageDelay());
             $tmpImage->setImageDispose($frame->getImageDispose());
         }
     } else {
         $tmpImage = new \Imagick();
         $tmpImage->addimage($this->image);
         if (min($this->getOriginalSize()) > $this->waterStart) {
             $tmpImage->compositeImage($this->waterImage, \Imagick::COMPOSITE_DEFAULT, $x, $y);
         }
     }
     if ($savepath == NULL) {
         //缩放当前
         $this->image = $tmpImage;
     } else {
         //写入储存
         $this->writeToPath($savepath, $tmpImage);
     }
     return $this;
 }
예제 #9
0
 public function addFont($text, $font, $startX = 0, $startY = 12, $color = 'black', $fontSize = 12, $alpha = 1, $degree = 0)
 {
     if (!$text) {
         return $this;
     }
     if (!is_readable($font)) {
         throw new ImageUtilityException('ImageImagickUtility 錯誤!', '參數錯誤,font:' . $font, '字型檔案不存在!');
     }
     if (!($startX >= 0 && $startY >= 0)) {
         throw new ImageUtilityException('ImageImagickUtility 錯誤!', '起始點錯誤,startX:' . $startX . ',startY:' . $startY, '水平、垂直的起始點一定要大於 0!');
     }
     if (!is_string($color)) {
         throw new ImageUtilityException('ImageImagickUtility 錯誤!', '色碼錯誤!', '請確認色碼格式,目前只支援 字串HEX 格式!');
     }
     if (!($fontSize > 0)) {
         throw new ImageUtilityException('ImageImagickUtility 錯誤!', '參數錯誤,fontSize:' . $fontSize, '文字大小一定要大於 0!');
     }
     if (!($alpha && is_numeric($alpha) && $alpha >= 0 && $alpha <= 1)) {
         throw new ImageUtilityException('ImageImagickUtility 錯誤!', '參數錯誤,alpha:' . $alpha, '參數 alpha 一定要是 0 或 1!');
     }
     if (!is_numeric($degree %= 360)) {
         throw new ImageUtilityException('ImageImagickUtility 錯誤!', '角度一定要是數字,degree:' . $degree, '請確認 GD 函式庫中是否有支援 imagerotate 函式!');
     }
     if (!($draw = $this->_createFont($font, $fontSize, $color, $alpha))) {
         throw new ImageUtilityException('ImageImagickUtility 錯誤!', ' Create 文字物件失敗', '請程式設計者確認狀況!');
     }
     if ($this->format == 'gif') {
         $newImage = new Imagick();
         $newImage->setFormat($this->format);
         $imagick = $this->image->clone()->coalesceImages();
         do {
             $temp = new Imagick();
             $temp->newImage($this->dimension['width'], $this->dimension['height'], new ImagickPixel('transparent'));
             $temp->compositeImage($imagick, imagick::COMPOSITE_DEFAULT, 0, 0);
             $temp->annotateImage($draw, $startX, $startY, $degree, $text);
             $newImage->addImage($temp);
             $newImage->setImageDelay($imagick->getImageDelay());
         } while ($imagick->nextImage());
     } else {
         $newImage = $this->image->clone();
         $newImage->annotateImage($draw, $startX, $startY, $degree, $text);
     }
     return $this->_updateImage($newImage);
 }
 private function _imageickThumb($url, $type, $new_w = 765, $new_h = 1000, $self = false, $trim = false)
 {
     $srcFile = $url;
     if ($self) {
         $destFile = $url;
     } else {
         $destFile = $type;
     }
     if ($new_w <= 0 || !file_exists($srcFile)) {
         return false;
     }
     $src = new Imagick($srcFile);
     $image_format = strtolower($src->getImageFormat());
     if ($image_format != 'jpeg' && $image_format != 'gif' && $image_forumat != 'bmp' && $image_format != 'png' && $image_format != 'jpg') {
         return false;
     }
     $src_page = $src->getImagePage();
     $src_w = $src_page['width'];
     $rate_w = $new_w / $src_w;
     if ($rate_w >= 1) {
         return false;
     }
     if ($new_h == -1) {
         $new_h = $rate_w * $src_page['height'];
     }
     //如果是 jpg jpeg gif
     if ($image_format != 'gif') {
         $dest = $src;
         if (!$trim) {
             $dest->thumbnailImage($new_w, $new_h, true);
         } else {
             $dest->cropthumbnailImage($new_w, $new_h);
         }
         $dest->writeImage($destFile);
         $dest->clear();
         //gif需要以帧一帧的处理
     } else {
         $dest = new Imagick();
         $color_transparent = new ImagickPixel("transparent");
         //透明色
         foreach ($src as $img) {
             $page = $img->getImagePage();
             if ($new_h == -1) {
                 $new_h = $new_w / $page['width'] * $src_page['hight'];
             }
             $tmp = new Imagick();
             $tmp->newImage($page['width'], $page['height'], $color_transparent, 'gif');
             $tmp->compositeImage($img, Imagick::COMPOSITE_OVER, $page['x'], $page['y']);
             if (!$trim) {
                 $tmp->thumbnailImage($new_w, $new_h, true);
             } else {
                 $tmp->cropthumbnailImage($new_w, $new_h);
             }
             $dest->addImage($tmp);
             $dest->setImagePage($tmp->getImageWidth(), $tmp->getImageHeight(), 0, 0);
             $dest->setImageDelay($img->getImageDelay());
             $dest->setImageDispose($img->getImageDispose());
         }
         $dest->coalesceImages();
         $dest->writeImages($destFile, true);
         $dest->clear();
     }
 }
예제 #11
0
    //    var_dump($identifyInfo);
    $imagickFrame->writeImage($frame . "debug.gif");
    //if (false) {
    $paletteImage = clone $imagickFrame;
    $paletteImage->quantizeImage(256, Imagick::COLORSPACE_YIQ, 0, false, false);
    //$imagickFrame->setImageDepth(8);
    //$imagickFrame->quantizeImage(15,Imagick::COLORSPACE_TRANSPARENT,0,false,false);
    //Imagick::mapImage ( Imagick $map , bool $dither )
    $imagickFrame->remapImage($paletteImage, Imagick::DITHERMETHOD_FLOYDSTEINBERG);
    //}
    //Imagick::DITHERMETHOD_RIEMERSMA
    //Imagick::DITHERMETHOD_FLOYDSTEINBERG
    //6.9meg
    //$imagickFrame->quantizeImage(255, Imagick::COLORSPACE_RGB, 0, true, false );
    //orderedPosterizeImage ( string $threshold_map [, int $channel = Imagick::CHANNEL_ALL ] )
    //posterizeImage ( int $levels , bool $dither )
    //$imagickFrame->remapimage()
    $imagickFrame->setImageDelay($frameDelay);
    $animation->addImage($imagickFrame);
}
//Zero default - 8 biggest tree
//$animation->quantizeImages(255, Imagick::COLORSPACE_RGB, 0, false, false );
//$animation->quantizeImages(255, Imagick::COLORSPACE_RGB, 8, true, false );
//Imagick::clutImage ( Imagick $lookup_table [, float $channel = Imagick::CHANNEL_DEFAULT ] )
$animation->setFormat('gif');
$animation->setImageFormat('gif');
$outputGif = $animation->deconstructImages();
$outputFilename = $animDir . ".gif";
$outputGif->writeImages($outputFilename, true);
$filesize = filesize($outputFilename);
echo "output is {$outputFilename}, filesize is {$filesize} \n";
예제 #12
0
 public function add_full_watermark($path, $x = 0, $y = 0)
 {
     $watermark = new Imagick($path);
     $draw = new ImagickDraw();
     $image = $this->image;
     $image_w = $image->getImageWidth();
     $image_h = $image->getimageheight();
     $water_w = $watermark->getImageWidth();
     $water_h = $watermark->getimageheight();
     $a = floor($image_w / $water_w);
     $b = floor($image_h / $water_h);
     //$offset_x = ($image_w>$water_w)?floor($image_w%$water_w/2):0;
     //$offset_y = ($image_h>$water_h)?floor($image_h%$water_h/2):0;
     $offset_x = $offset_y = 0;
     for ($i = 0; $i <= $a; $i++) {
         for ($j = 0; $j <= $b; $j++) {
             $draw->composite($watermark->getImageCompose(), $i * $water_w + $offset_x, $j * $water_h + $offset_y, $water_w, $water_h, $watermark);
         }
     }
     if ($this->type == 'gif') {
         $image = $this->image;
         $canvas = new Imagick();
         $images = $image->coalesceImages();
         foreach ($image as $frame) {
             $img = new Imagick();
             $img->readImageBlob($frame);
             $img->drawImage($draw);
             $canvas->addImage($img);
             $canvas->setImageDelay($img->getImageDelay());
         }
         $image->destroy();
         $this->image = $canvas;
     } else {
         $this->image->drawImage($draw);
     }
 }
예제 #13
0
 private function copySize($size)
 {
     list($width, $height) = getimagesize($this->publicPath());
     list($newWidth, $newHeight, $destinationX, $destinationY, $sourceX, $sourceY, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight) = $this->calcSizes($size, $width, $height);
     $thumb = imagecreatetruecolor($newWidth, $newHeight);
     imagealphablending($thumb, false);
     imagesavealpha($thumb, true);
     $transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
     imagefilledrectangle($thumb, 0, 0, $newWidth, $newHeight, $transparent);
     if (strtolower($this->extension) == 'jpg') {
         $source = imagecreatefromjpeg($this->publicPath());
         imagecopyresampled($thumb, $source, $destinationX, $destinationY, $sourceX, $sourceY, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight);
         imagejpeg($thumb, $this->publicPath($size));
     } elseif (strtolower($this->extension) == 'jpeg') {
         $source = imagecreatefromjpeg($this->publicPath());
         imagecopyresampled($thumb, $source, $destinationX, $destinationY, $sourceX, $sourceY, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight);
         imagejpeg($thumb, $this->publicPath($size));
     } elseif (strtolower($this->extension) == 'png') {
         $source = imagecreatefrompng($this->publicPath());
         imagecopyresampled($thumb, $source, $destinationX, $destinationY, $sourceX, $sourceY, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight);
         imagepng($thumb, $this->publicPath($size));
     } elseif (strtolower($this->extension) == 'gif') {
         if (extension_loaded('imagick')) {
             $image = new \Imagick($this->publicPath());
             $image = $image->coalesceimages();
             $final = new \Imagick();
             foreach ($image as $frame) {
                 $canvas = new \Imagick();
                 $frame->cropimage($sourceWidth, $sourceHeight, $sourceX, $sourceY);
                 $frame->thumbnailImage($destinationWidth, $destinationHeight);
                 $canvas->newImage($newWidth, $newHeight, new \ImagickPixel('none'));
                 $delay = $frame->getImageDelay();
                 $canvas->setImageDelay($delay);
                 $canvas->compositeImage($frame, \Imagick::COMPOSITE_OVER, $destinationX, $destinationY);
                 $final->addimage($canvas);
             }
             $final->writeImages($this->publicPath($size), true);
         } else {
             $source = imagecreatefromgif($this->publicPath());
             imagecopyresampled($thumb, $source, $destinationX, $destinationY, $sourceX, $sourceY, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight);
             imagegif($thumb, $this->publicPath($size));
         }
     }
 }
 /**
  * Put image to square
  *
  * @param  string $path image file
  * @param  int $width square width
  * @param  int $height square height
  * @param int|string $align reserved
  * @param int|string $valign reserved
  * @param  string $bgcolor square background color in #rrggbb format
  * @param  string $destformat image destination format
  * @param  int $jpgQuality JEPG quality (1-100)
  * @return false|string
  * @author Dmitry (dio) Levashov
  * @author Alexey Sukhotin
  */
 protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null, $jpgQuality = null)
 {
     if (($s = getimagesize($path)) == false) {
         return false;
     }
     $result = false;
     /* Coordinates for image over square aligning */
     $y = ceil(abs($height - $s[1]) / 2);
     $x = ceil(abs($width - $s[0]) / 2);
     if (!$jpgQuality) {
         $jpgQuality = $this->options['jpgQuality'];
     }
     elFinder::extendTimeLimit(300);
     switch ($this->imgLib) {
         case 'imagick':
             try {
                 $img = new imagick($path);
             } catch (Exception $e) {
                 return false;
             }
             if ($bgcolor === 'transparent') {
                 $bgcolor = 'rgba(255, 255, 255, 0.0)';
             }
             $ani = $img->getNumberImages() > 1;
             if ($ani && is_null($destformat)) {
                 $img1 = new Imagick();
                 $img1->setFormat('gif');
                 $img = $img->coalesceImages();
                 do {
                     $gif = new Imagick();
                     $gif->newImage($width, $height, new ImagickPixel($bgcolor));
                     $gif->setImageColorspace($img->getImageColorspace());
                     $gif->setImageFormat('gif');
                     $gif->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y);
                     $gif->setImageDelay($img->getImageDelay());
                     $gif->setImageIterations($img->getImageIterations());
                     $img1->addImage($gif);
                     $gif->clear();
                 } while ($img->nextImage());
                 $img1 = $img1->optimizeImageLayers();
                 $result = $img1->writeImages($path, true);
             } else {
                 if ($ani) {
                     $img->setFirstIterator();
                 }
                 $img1 = new Imagick();
                 $img1->newImage($width, $height, new ImagickPixel($bgcolor));
                 $img1->setImageColorspace($img->getImageColorspace());
                 $img1->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y);
                 $result = $this->imagickImage($img1, $path, $destformat, $jpgQuality);
             }
             $img1->clear();
             $img->clear();
             return $result ? $path : false;
             break;
         case 'convert':
             extract($this->imageMagickConvertPrepare($path, $destformat, $jpgQuality, $s));
             if ($bgcolor === 'transparent') {
                 $bgcolor = 'rgba(255, 255, 255, 0.0)';
             }
             $cmd = sprintf('convert -size %dx%d "xc:%s" png:- | convert%s%s png:-  %s -geometry +%d+%d -compose over -composite%s %s', $width, $height, $bgcolor, $coalesce, $jpgQuality, $quotedPath, $x, $y, $deconstruct, $quotedDstPath);
             $result = false;
             if ($this->procExec($cmd) === 0) {
                 $result = true;
             }
             return $result ? $path : false;
             break;
         case 'gd':
             $img = $this->gdImageCreate($path, $s['mime']);
             if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
                 $this->gdImageBackground($tmp, $bgcolor);
                 if ($bgcolor === 'transparent' && ($destformat === 'png' || $s[2] === IMAGETYPE_PNG)) {
                     $bgNum = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
                     imagefill($tmp, 0, 0, $bgNum);
                 }
                 if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
                     return false;
                 }
                 $result = $this->gdImage($tmp, $path, $destformat, $s['mime'], $jpgQuality);
                 imagedestroy($img);
                 imagedestroy($tmp);
                 return $result ? $path : false;
             }
             break;
     }
     return false;
 }
예제 #15
0
 /**
  * 创建图片
  * @param array $options 参数数组
  * 必须填写的参数:
  *   save_type: int 保存类型 1:保存图片路径自定义 2:保存后的图片覆盖原图片
  * 选填的参数:
  *   save_path: string 图片保存地址,包括目录和文件名
  *   resize_width: int 保存后图片的宽,默认取原图宽
  *   resize_height: int 保存后图片的高,默认取原图高
  *   quality: int 图片压缩质量,1-100,数字越大质量越好,默认取85
  * @return array
  */
 public function createImage($options)
 {
     $resArr = ['code' => 0];
     $acceptType = ['image/png', 'image/jpeg', 'image/gif'];
     if ($this->image == null) {
         $resArr['code'] = 1;
         $resArr['message'] = '打开图片失败';
     } else {
         if (!in_array($this->mimeType, $acceptType)) {
             $resArr['code'] = 2;
             $resArr['message'] = '图片类型不支持';
         } else {
             if ($options['save_type'] == 1) {
                 $savePath = str_replace('\\', '/', $options['save_path']);
             } else {
                 if ($options['save_type'] == 2) {
                     $savePath = $this->originalName;
                 } else {
                     $resArr['code'] = 3;
                     $resArr['message'] = '保存类型不正确';
                     return $resArr;
                 }
             }
             $resize_width = isset($options['resize_width']) && (int) $options['resize_width'] > 0 ? (int) $options['resize_width'] : $this->originalWidth;
             $resize_height = isset($options['resize_height']) && (int) $options['resize_height'] > 0 ? (int) $options['resize_height'] : $this->originalHeight;
             $quality = isset($options['quality']) && (int) $options['quality'] > 0 && (int) $options['quality'] <= 100 ? (int) $options['quality'] : 85;
             $now_quality = $this->image->getImageCompressionQuality();
             if ($now_quality == 0) {
                 $need_quality = $quality;
             } else {
                 $need_quality = ceil($now_quality * $quality / 100);
             }
             if ($this->mimeType == 'image/gif') {
                 //处理gif动态图片
                 $image = $this->image;
                 $images = $image->coalesceImages();
                 $canvas = new \Imagick();
                 foreach ($images as $eImage) {
                     $img = new \Imagick();
                     $img->readImageBlob($eImage);
                     $img->resizeImage($resize_width, $resize_height, \Imagick::FILTER_CATROM, 1, true);
                     $img->setImageCompressionQuality($need_quality);
                     $canvas->addImage($img);
                     $canvas->setImageDelay($img->getImageDelay());
                     $img->destroy();
                 }
                 $this->image = $canvas;
                 $image->destroy();
                 $canvas->destroy();
             } else {
                 if ($this->mimeType == 'image/jpeg') {
                     $this->image->resizeImage($resize_width, $resize_height, \Imagick::FILTER_CATROM, 1, true);
                     $this->image->setImageCompression(\Imagick::COMPRESSION_JPEG);
                     $this->image->setImageCompressionQuality($need_quality);
                 } else {
                     $this->image->resizeImage($resize_width, $resize_height, \Imagick::FILTER_CATROM, 1, true);
                     $this->image->setImageCompressionQuality($need_quality);
                 }
             }
             $writeRes = $this->image->writeImage($savePath);
             if ($writeRes) {
                 //修改写入成功后文件的操作权限
                 chmod($savePath, 0777);
                 $resArr['data'] = ['image_name' => $savePath, 'image_width' => $this->image->getImageWidth(), 'image_height' => $this->image->getImageHeight()];
             } else {
                 $resArr['code'] = 4;
                 $resArr['message'] = '写入文件失败';
             }
         }
     }
     return $resArr;
 }
예제 #16
0
파일: MooPHP.php 프로젝트: noikiy/zays
function ImagickResizeImage($srcFile, $destFile, $new_w, $new_h, $zhenshu = false)
{
    if ($new_w <= 0 || $new_h <= 0 || !file_exists($srcFile)) {
        return false;
    }
    $src = new Imagick($srcFile);
    $image_format = strtolower($src->getImageFormat());
    if ($image_format != 'jpeg' && $image_format != 'gif' && $image_format != 'png' && $image_format != 'jpg' && $image_format != 'bmp') {
        return false;
    }
    //如果是 jpg jpeg
    if ($image_format != 'gif') {
        $dest = $src;
        $dest->thumbnailImage($new_w, $new_h, true);
        $dest->writeImage($destFile);
        $dest->clear();
        //gif需要以帧一帧的处理
    } else {
        $dest = new Imagick();
        $color_transparent = new ImagickPixel("transparent");
        //透明色
        $gif_i = 1;
        foreach ($src as $img) {
            $page = $img->getImagePage();
            $tmp = new Imagick();
            $tmp->newImage($page['width'], $page['height'], $color_transparent, 'gif');
            $tmp->compositeImage($img, Imagick::COMPOSITE_OVER, $page['x'], $page['y']);
            $tmp->thumbnailImage($new_w, $new_h, true);
            $dest->addImage($tmp);
            $dest->setImagePage($tmp->getImageWidth(), $tmp->getImageHeight(), 0, 0);
            $dest->setImageDelay($img->getImageDelay());
            $dest->setImageDispose($img->getImageDispose());
            if ($zhenshu > 0) {
                if ($zhenshu <= $gif_i) {
                    break;
                }
                $gif_i++;
            }
        }
        $dest->coalesceImages();
        $dest->writeImages($destFile, true);
        $dest->clear();
    }
}
 /**
  * Put image to square
  *
  * @param  string   $path               image file
  * @param  int      $width              square width
  * @param  int      $height             square height
  * @param  int	    $align              reserved
  * @param  int 	    $valign             reserved
  * @param  string   $bgcolor            square background color in #rrggbb format
  * @param  string   $destformat         image destination format
  * @return string|false
  * @author Dmitry (dio) Levashov
  * @author Alexey Sukhotin
  **/
 protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null)
 {
     if (($s = @getimagesize($path)) == false) {
         return false;
     }
     $result = false;
     /* Coordinates for image over square aligning */
     $y = ceil(abs($height - $s[1]) / 2);
     $x = ceil(abs($width - $s[0]) / 2);
     switch ($this->imgLib) {
         case 'imagick':
             try {
                 $img = new imagick($path);
             } catch (Exception $e) {
                 return false;
             }
             $ani = $img->getNumberImages() > 1;
             if ($ani && is_null($destformat)) {
                 $img1 = new Imagick();
                 $img1->setFormat('gif');
                 $img = $img->coalesceImages();
                 do {
                     $gif = new Imagick();
                     $gif->newImage($width, $height, new ImagickPixel($bgcolor));
                     $gif->setImageColorspace($img->getImageColorspace());
                     $gif->setImageFormat('gif');
                     $gif->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y);
                     $gif->setImageDelay($img->getImageDelay());
                     $gif->setImageIterations($img->getImageIterations());
                     $img1->addImage($gif);
                     $gif->destroy();
                 } while ($img->nextImage());
                 $img1 = $img1->optimizeImageLayers();
                 $result = $img1->writeImages($path, true);
             } else {
                 if ($ani) {
                     $img->setFirstIterator();
                 }
                 $img1 = new Imagick();
                 $img1->newImage($width, $height, new ImagickPixel($bgcolor));
                 $img1->setImageColorspace($img->getImageColorspace());
                 $img1->setImageFormat($destformat != null ? $destformat : $img->getFormat());
                 $img1->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y);
                 $result = $img1->writeImage($path);
             }
             $img1->destroy();
             $img->destroy();
             return $result ? $path : false;
             break;
         case 'gd':
             $img = self::gdImageCreate($path, $s['mime']);
             if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
                 self::gdImageBackground($tmp, $bgcolor);
                 if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
                     return false;
                 }
                 $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
                 imagedestroy($img);
                 imagedestroy($tmp);
                 return $result ? $path : false;
             }
             break;
     }
     return false;
 }
예제 #18
0
 public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALE)
 {
     if (empty($this->img)) {
         throw new Exception("没有可以被缩略的图像资源");
     }
     $w = $this->info["width"];
     $h = $this->info["height"];
     switch ($type) {
         case THINKIMAGE_THUMB_SCALE:
             if ($w < $width && $h < $height) {
                 return NULL;
             }
             $scale = min($width / $w, $height / $h);
             $x = $y = 0;
             $width = $w * $scale;
             $height = $h * $scale;
             break;
         case THINKIMAGE_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 THINKIMAGE_THUMB_NORTHWEST:
             $scale = max($width / $w, $height / $h);
             $x = $y = 0;
             $w = $width / $scale;
             $h = $height / $scale;
             break;
         case THINKIMAGE_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 THINKIMAGE_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;
             $newimg = new Imagick();
             $newimg->newImage($width, $height, "white", $this->info["type"]);
             if ("gif" == $this->info["type"]) {
                 $imgs = $this->img->coalesceImages();
                 $img = new Imagick();
                 $this->img->destroy();
                 do {
                     $image = $this->_fill($newimg, $posx, $posy, $neww, $newh, $imgs);
                     $img->addImage($image);
                     $img->setImageDelay($imgs->getImageDelay());
                     $img->setImagePage($width, $height, 0, 0);
                     $image->destroy();
                 } while ($imgs->nextImage());
                 $this->img->destroy();
                 $this->img = $img->deconstructImages();
                 $imgs->destroy();
                 $img->destroy();
             } else {
                 $img = $this->_fill($newimg, $posx, $posy, $neww, $newh);
                 $this->img->destroy();
                 $this->img = $img;
             }
             $this->info["width"] = $width;
             $this->info["height"] = $height;
             return NULL;
         case THINKIMAGE_THUMB_FIXED:
             $x = $y = 0;
             break;
         default:
             throw new Exception("不支持的缩略图裁剪类型");
     }
     $this->crop($w, $h, $x, $y, $width, $height);
 }
예제 #19
0
 /**
  * 生成缩略图
  * @param  integer $width  缩略图最大宽度
  * @param  integer $height 缩略图最大高度
  * @param  integer $type   缩略图裁剪类型
  */
 public function thumb($width, $height, $type = Image::IMAGE_THUMB_SCALE)
 {
     if (empty($this->img)) {
         throw new Exception('没有可以被缩略的图像资源');
     }
     //原图宽度和高度
     $w = $this->info['width'];
     $h = $this->info['height'];
     /* 计算缩略图生成的必要参数 */
     switch ($type) {
         /* 等比例缩放 */
         case Image::IMAGE_THUMB_SCALING:
             //原图尺寸小于缩略图尺寸则不进行缩略
             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;
             //创建一张新图像
             $newimg = new Imagick();
             $newimg->newImage($width, $height, 'white', $this->info['type']);
             if ('gif' == $this->info['type']) {
                 $imgs = $this->img->coalesceImages();
                 $img = new Imagick();
                 $this->img->destroy();
                 //销毁原图
                 //循环填充每一帧
                 do {
                     //填充图像
                     $image = $this->_fill($newimg, $posx, $posy, $neww, $newh, $imgs);
                     $img->addImage($image);
                     $img->setImageDelay($imgs->getImageDelay());
                     $img->setImagePage($width, $height, 0, 0);
                     $image->destroy();
                     //销毁零时图片
                 } while ($imgs->nextImage());
                 //压缩图片
                 $this->img->destroy();
                 $this->img = $img->deconstructImages();
                 $imgs->destroy();
                 //销毁零时图片
                 $img->destroy();
                 //销毁零时图片
             } else {
                 //填充图像
                 $img = $this->_fill($newimg, $posx, $posy, $neww, $newh);
                 //销毁原图
                 $this->img->destroy();
                 $this->img = $img;
             }
             //设置新图像属性
             $this->info['width'] = $width;
             $this->info['height'] = $height;
             return;
             /* 固定 */
         /* 固定 */
         case Image::IMAGE_THUMB_FIXED:
             $x = $y = 0;
             break;
         default:
             throw new Exception('不支持的缩略图裁剪类型');
     }
     /* 裁剪图像 */
     $this->crop($w, $h, $x, $y, $width, $height);
 }
예제 #20
0
 protected function _reflection($height, $opacity, $fade_in)
 {
     if ($this->_version >= 30100) {
         $reflection = clone $this->_image;
     } else {
         $reflection = clone $this->_image->{$clone}();
     }
     $reflection->setIteratorIndex(0);
     while (true) {
         $reflection->flipImage();
         $reflection->cropImage($reflection->getImageWidth(), $height, 0, 0);
         $reflection->setImagePage($reflection->getImageWidth(), $height, 0, 0);
         if (!$reflection->nextImage()) {
             break;
         }
     }
     $pseudo = $fade_in ? "gradient:black-transparent" : "gradient:transparent-black";
     $fade = new \Imagick();
     $fade->newPseudoImage($reflection->getImageWidth(), $reflection->getImageHeight(), $pseudo);
     $opacity /= 100;
     $reflection->setIteratorIndex(0);
     while (true) {
         $reflection->compositeImage($fade, constant("Imagick::COMPOSITE_DSTOUT"), 0, 0);
         $reflection->evaluateImage(constant("Imagick::EVALUATE_MULTIPLY"), $opacity / 100, constant("Imagick::CHANNEL_ALPHA"));
         if (!$reflection->nextImage()) {
             break;
         }
     }
     $fade->destroy();
     $image = new \Imagick();
     $pixel = new \ImagickPixel();
     $height = $this->_image->getImageHeight() + $height;
     $this->_image->setIteratorIndex(0);
     while (true) {
         $image->newImage($this->_width, $height, $pixel);
         $image->setImageAlphaChannel(constant("Imagick::ALPHACHANNEL_SET"));
         $image->setColorspace($this->_image->getColorspace());
         $image->setImageDelay($this->_image->getImageDelay());
         $image->compositeImage($this->_image, constant("Imagick::COMPOSITE_SRC"), 0, 0);
         if (!$this->_image->nextImage()) {
             break;
         }
     }
     $image->setIteratorIndex(0);
     $reflection->setIteratorIndex(0);
     while (true) {
         $image->compositeImage($reflection, constant("Imagick::COMPOSITE_OVER"), 0, $this->_height);
         $image->setImageAlphaChannel(constant("Imagick::ALPHACHANNEL_SET"));
         $image->setColorspace($this->_image->getColorspace());
         $image->setImageDelay($this->_image->getImageDelay());
         $image->compositeImage($this->_image, constant("Imagick::COMPOSITE_SRC"), 0, 0);
         if (!$image->nextImage() || !$reflection->nextImage()) {
             break;
         }
     }
     $reflection->destroy();
     $this->_image->clear();
     $this->_image->destroy();
     $this->_image = $image;
     $this->_width = $this->_image->getImageWidth();
     $this->_height = $this->_image->getImageHeight();
 }