/** * {@inheritdoc} */ public function polygon(array $coordinates, Color $color, $fill = false, $thickness = 1) { if (count($coordinates) < 3) { throw new InvalidArgumentException(sprintf('Polygon must consist of at least 3 coordinates, %d given', count($coordinates))); } $points = array_map(function (PointInterface $p) { return array('x' => $p->getX(), 'y' => $p->getY()); }, $coordinates); try { $pixel = $this->getColor($color); $polygon = new \GmagickDraw(); $polygon->setstrokecolor($pixel); $polygon->setstrokewidth(max(1, (int) $thickness)); if ($fill) { $polygon->setfillcolor($pixel); } else { $polygon->setfillcolor('transparent'); } $polygon->polygon($points); $this->gmagick->drawImage($polygon); $pixel = null; $polygon = null; } catch (\GmagickException $e) { throw new RuntimeException('Draw polygon operation failed', $e->getCode(), $e); } return $this; }
/** * 生成水印 * @param string $groundImage */ function waterImage($groundImage = '') { try { //获取背景图的高,宽 if ($groundImage && is_file($groundImage)) { $bg = new Gmagick(); $bg->readImage($groundImage); $bgHeight = $bg->getImageHeight(); $bgWidth = $bg->getImageWidth(); } //获取水印图的高,宽 if ($this->waterImage && is_file($this->waterImage)) { $water = new Gmagick($this->waterImage); $waterHeight = $water->getImageHeight(); $waterWidth = $water->getImageWidth(); } //如果背景图的高宽小于水印图的高宽则不加水印 if ($bgHeight < $waterHeight || $bgWidth < $waterWidth) { return false; } else { $isWaterImg = TRUE; } //加水印 if ($isWaterImg) { $dw = new GmagickDraw(); //加图片水印 if (is_file($this->waterImage)) { //水印位置随机 $waterPos = $this->getWaterPos($bgWidth, $bgHeight, $waterWidth, $waterHeight); $bg->compositeImage($water, 1, $waterPos['x'], $waterPos['y']); if (!$bg->writeImage($groundImage)) { return FALSE; } } else { //加文字水印 $waterTextInfo = array('textFont' => '15', 'textColor' => '#FF0000', 'textAlpha' => 1, 'textInfo' => 'www.okooo.com'); $dw->setFontSize($waterTextInfo['textFont']); //$dw->setFillColor($waterTextInfo['textColor']); $dw->setFillOpacity(1); $x = abs(130 - $bgWidth); $y = abs(15 - $bgHeight); $dw->annotate($x, $y, $waterTextInfo['textInfo']); $dw->setTextEncoding('UTF-8'); $bg->drawImage($dw); if (!$bg->writeImage($groundImage)) { return FALSE; } } } } catch (Exception $e) { Logger::getLogger('dataengine.lottery.snapshot')->apps('exception')->info(json_encode($e->getMessage())); } }