示例#1
0
 /**
  * @return $this
  */
 public function setColorspaceToRGB()
 {
     $imageColorspace = $this->resource->getImageColorspace();
     if ($imageColorspace == \Imagick::COLORSPACE_CMYK) {
         if (self::getCMYKColorProfile() && self::getRGBColorProfile()) {
             $profiles = $this->resource->getImageProfiles('*', false);
             // we're only interested if ICC profile(s) exist
             $has_icc_profile = array_search('icc', $profiles) !== false;
             // if it doesn't have a CMYK ICC profile, we add one
             if ($has_icc_profile === false) {
                 $this->resource->profileImage('icc', self::getCMYKColorProfile());
             }
             // then we add an RGB profile
             $this->resource->profileImage('icc', self::getRGBColorProfile());
             $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB);
             // we have to use SRGB here, no clue why but it works
         } else {
             $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB);
         }
     } else {
         if ($imageColorspace == \Imagick::COLORSPACE_GRAY) {
             $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB);
         } else {
             if (!in_array($imageColorspace, array(\Imagick::COLORSPACE_RGB, \Imagick::COLORSPACE_SRGB))) {
                 $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB);
             } else {
                 // this is to handle embedded icc profiles in the RGB/sRGB colorspace
                 $profiles = $this->resource->getImageProfiles('*', false);
                 $has_icc_profile = array_search('icc', $profiles) !== false;
                 if ($has_icc_profile) {
                     try {
                         // if getImageColorspace() says SRGB but the embedded icc profile is CMYK profileImage() will throw an exception
                         $this->resource->profileImage('icc', self::getRGBColorProfile());
                     } catch (\Exception $e) {
                         \Logger::warn($e);
                     }
                 }
             }
         }
     }
     // this is a HACK to force grayscale images to be real RGB - truecolor, this is important if you want to use
     // thumbnails in PDF's because they do not support "real" grayscale JPEGs or PNGs
     // problem is described here: http://imagemagick.org/Usage/basics/#type
     // and here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=6888#p31891
     $currentLocale = setlocale(LC_ALL, "0");
     // this locale hack thing is also a hack for imagick
     setlocale(LC_ALL, "");
     // details see https://www.pimcore.org/issues/browse/PIMCORE-2728
     $draw = new \ImagickDraw();
     $draw->setFillColor("#ff0000");
     $draw->setfillopacity(0.01);
     $draw->point(floor($this->getWidth() / 2), floor($this->getHeight() / 2));
     // place it in the middle of the image
     $this->resource->drawImage($draw);
     setlocale(LC_ALL, $currentLocale);
     // see setlocale() above, for details ;-)
     return $this;
 }
 private function colorizeWall()
 {
     $wall = new Imagick($this->wallImage);
     $im = new Imagick();
     $im->newimage(1280, 720, $this->baseColor, 'png');
     $wall->compositeimage($im, Imagick::COMPOSITE_MULTIPLY, 0, 0);
     $im->clear();
     // darken image
     if ($this->darkenValue < 100 && $this->darkenValue >= 50) {
         $wallAdjust = clone $wall;
         $wallAdjust->modulateimage($this->darkenValue, 100, 100);
         $wall->compositeimage($wallAdjust, Imagick::COMPOSITE_MULTIPLY, 0, 0);
         $wallAdjust->clear();
     }
     // lighten image
     if ($this->lightenValue > 0) {
         $wallAdjust = clone $wall;
         $draw = new ImagickDraw();
         $draw->setFillColor('#ffffff');
         $draw->setfillopacity($this->lightenValue / 100);
         $geometry = $wallAdjust->getImageGeometry();
         $width = $geometry['width'];
         $height = $geometry['height'];
         $draw->rectangle(0, 0, $width, $height);
         $wallAdjust->drawImage($draw);
         $wall->compositeimage($wallAdjust, Imagick::COMPOSITE_DEFAULT, 0, 0);
         $wallAdjust->clear();
     }
     // apply hue/saturation
     $wall->modulateimage(100, $this->saturationValue, $this->hueValue);
     $wall->compositeimage($this->mask, Imagick::COMPOSITE_DSTIN, 0, 0);
     return $wall;
 }
示例#3
0
 /**
  * 获取文字的描绘对象
  * @param string $font 字体名称或字体路径
  * @param integer $size 字体大小
  * @param string | ImagickPixel $color 字颜色
  * @param float $opacity 文字的透明度,取值范围0(全透明)-1(不透明)
  * @param string | ImagickPixel $underColor 字的背景颜色
  * @return ImagickDraw | false
  */
 public function getDrawForText($font = null, $size = 8, $color = 'transparent', $opacity = 0.58, $underColor = null)
 {
     $draw = new ImagickDraw();
     // $draw->setGravity(Imagick::GRAVITY_CENTER);
     // 特别注意这里了,如果这里设置了文字水印的位置的话,那么在其写入别到文件时就会起作用,且其他设置的坐标都没有用
     if ($font !== null) {
         if (is_file($font)) {
             $draw->setfont($font);
         } else {
             $this->_error = '字体文件不存在';
             return false;
         }
     }
     $draw->setfontsize($size);
     $draw->setFillColor($color);
     $draw->setfillopacity($opacity);
     // 貌似和这个是一样的: $draw->setFillAlpha(0.5);
     if ($underColor !== null) {
         $draw->settextundercolor($underColor);
     }
     // $draw->settextalignment(2); //文字对齐方式,2为居中
     return $draw;
 }
示例#4
0
 /**
  * this is to force RGB and to apply custom icc color profiles
  */
 protected function applyColorProfiles()
 {
     if ($this->resource->getImageColorspace() == Imagick::COLORSPACE_CMYK) {
         if (self::getCMYKColorProfile() && self::getRGBColorProfile()) {
             $profiles = $this->resource->getImageProfiles('*', false);
             // we're only interested if ICC profile(s) exist
             $has_icc_profile = array_search('icc', $profiles) !== false;
             // if it doesn't have a CMYK ICC profile, we add one
             if ($has_icc_profile === false) {
                 $this->resource->profileImage('icc', self::getCMYKColorProfile());
             }
             // then we add an RGB profile
             $this->resource->profileImage('icc', self::getRGBColorProfile());
             $this->resource->setImageColorspace(Imagick::COLORSPACE_RGB);
         }
     }
     // this is a HACK to force grayscale images to be real RGB - truecolor, this is important if you want to use
     // thumbnails in PDF's because they do not support "real" grayscale JPEGs or PNGs
     // problem is described here: http://imagemagick.org/Usage/basics/#type
     // and here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=6888#p31891
     if ($this->resource->getimagetype() == Imagick::IMGTYPE_GRAYSCALE) {
         $draw = new ImagickDraw();
         $draw->setFillColor("red");
         $draw->setfillopacity(0.001);
         $draw->point(0, 0);
         $this->resource->drawImage($draw);
     }
 }
示例#5
0
 /**
  * Draw filled circle on current image
  *
  * @param string $color
  * @param int    $center_x
  * @param int    $center_y
  * @param int    $radius
  * @param float  $opacity
  *
  * @return image
  */
 public function fillCircle($color, $center_x, $center_y, $radius, $opacity = 1)
 {
     $fill = new \ImagickDraw();
     $fill->setfillcolor(new \ImagickPixel($color));
     $fill->setfillopacity($opacity);
     $fill->ellipse($center_x, $center_y, $radius, $radius, 0, 360);
     $this->imagick->drawimage($fill);
     return $this;
 }
示例#6
0
function composite($strokeColor, $fillColor, $backgroundColor)
{
    $draw = new \ImagickDraw();
    $draw->setStrokeColor($strokeColor);
    $draw->setFillColor($fillColor);
    $draw->setFillOpacity(1);
    $draw->setStrokeWidth(2);
    $draw->setFontSize(72);
    $draw->setStrokeOpacity(1);
    $draw->setStrokeColor($strokeColor);
    $draw->setStrokeWidth(2);
    $draw->setFont("../fonts/CANDY.TTF");
    $draw->setFontSize(140);
    $draw->rectangle(0, 0, 1000, 300);
    $draw->setFillColor('white');
    $draw->setfillopacity(1);
    $draw->annotation(50, 180, "Lorem Ipsum!");
    //    $imagick = new \Imagick(realpath("../images/TestImage.jpg"));
    //    $draw->composite(\Imagick::COMPOSITE_MULTIPLY, -500, -200, 2000, 600, $imagick);
    //$imagick->compositeImage($draw, 0, 0, 1000, 500);
    //$draw->composite(Imagick::COMPOSITE_COLORBURN, -500, -200, 2000, 600, $imagick);
    //Create an image object which the draw commands can be rendered into
    $imagick = new \Imagick();
    $imagick->newImage(1000, 302, $backgroundColor);
    $imagick->setImageFormat("png");
    //Render the draw commands in the ImagickDraw object
    //into the image.
    $imagick->drawImage($draw);
    //Send the image to the browser
    header("Content-Type: image/png");
    echo $imagick->getImageBlob();
}
示例#7
0
 /**
  * @return $this
  */
 public function setColorspaceToRGB()
 {
     $imageColorspace = $this->resource->getImageColorspace();
     if ($imageColorspace == \Imagick::COLORSPACE_CMYK) {
         if (self::getCMYKColorProfile() && self::getRGBColorProfile()) {
             $profiles = $this->resource->getImageProfiles('*', false);
             // we're only interested if ICC profile(s) exist
             $has_icc_profile = array_search('icc', $profiles) !== false;
             // if it doesn't have a CMYK ICC profile, we add one
             if ($has_icc_profile === false) {
                 $this->resource->profileImage('icc', self::getCMYKColorProfile());
             }
             // then we add an RGB profile
             $this->resource->profileImage('icc', self::getRGBColorProfile());
             $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB);
             // we have to use SRGB here, no clue why but it works
         } else {
             $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB);
         }
     } else {
         if ($imageColorspace == \Imagick::COLORSPACE_GRAY) {
             $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB);
         } else {
             if (!in_array($imageColorspace, array(\Imagick::COLORSPACE_RGB, \Imagick::COLORSPACE_SRGB))) {
                 $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB);
             } else {
                 // this is to handle embedded icc profiles in the RGB/sRGB colorspace
                 $profiles = $this->resource->getImageProfiles('*', false);
                 $has_icc_profile = array_search('icc', $profiles) !== false;
                 if ($has_icc_profile) {
                     $this->resource->profileImage('icc', self::getRGBColorProfile());
                 }
             }
         }
     }
     // this is a HACK to force grayscale images to be real RGB - truecolor, this is important if you want to use
     // thumbnails in PDF's because they do not support "real" grayscale JPEGs or PNGs
     // problem is described here: http://imagemagick.org/Usage/basics/#type
     // and here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=6888#p31891
     $draw = new \ImagickDraw();
     $draw->setFillColor("#ff0000");
     $draw->setfillopacity(0.01);
     $draw->point(floor($this->getWidth() / 2), floor($this->getHeight() / 2));
     // place it in the middle of the image
     $this->resource->drawImage($draw);
     return $this;
 }
示例#8
0
 protected function _text($text, $offset_x, $offset_y, $opacity, $r, $g, $b, $size, $fontfile)
 {
     $opacity = $opacity / 100;
     $draw = new \ImagickDraw();
     $color = sprintf("rgb(%d, %d, %d)", $r, $g, $b);
     $pixel = new \ImagickPixel($color);
     $draw->setFillColor($pixel);
     if ($fontfile) {
         $draw->setFont($fontfile);
     }
     if ($size) {
         $draw->setFontSize($size);
     }
     if ($opacity) {
         $draw->setfillopacity($opacity);
     }
     if ($offset_x < 0) {
         $offset_x = abs($offset_x);
         if ($offset_y < 0) {
             $offset_y = abs($offset_y);
             $gravity = constant("Imagick::GRAVITY_SOUTHEAST");
         } else {
             $gravity = constant("Imagick::GRAVITY_NORTHEAST");
         }
     } else {
         /*
          * if (y < 0 { where y comes from??
          * $offset_y = abs(offset_y);
          * $gravity = constant("Imagick::GRAVITY_SOUTHWEST");
          * } else {
          * $gravity = constant("Imagick::GRAVITY_NORTHWEST");
          * }
          */
     }
     $draw->setGravity($gravity);
     $this->_image->setIteratorIndex(0);
     while (true) {
         $this->_image->annotateImage($draw, $offset_x, $offset_y, 0, $text);
         if (!$this->_image->nextImage()) {
             break;
         }
     }
     $draw->destroy();
 }