public function process($args) { if (isset($args[0]) and is_numeric($args[0])) { $rotation = $args[0]; } else { $rotation = mt_rand(-5, 5); } if (isset($args[1])) { $signature = $args[1]; } if (isset($args[2])) { $role = $args[2]; } /* -------------------------- */ $frame_path = DOCROOT . "/staticfiles/img/polaroids/polaroid_frame.png"; if (!isset($frame_path) or !file_exists($frame_path)) { return; } // Load the frame and crop the requested image $frame = new Gmagick(); $frame->readImage($frame_path); $w = $frame->getimagewidth(); $h = $frame->getimageheight(); $this->crop(285, 294); $x = 28; $y = 31; $this->image->borderImage("transparent", $x, $y); // Have to add a border as the x displacement in compositeImage() is broken! $frame->compositeImage($this->image, Gmagick::COMPOSITE_OVER, 0, 0); // Some comp styles seem to throw errors! $this->image = $frame; // Add the signature if we have been asked for one if (isset($signature)) { $sig_path = DOCROOT . "/staticfiles/img/polaroids/" . $signature . "_sig.png"; if (file_exists($sig_path)) { $sig = new Gmagick(); $sig->readImage($sig_path); $sw = $sig->getimagewidth(); $sh = $sig->getimageheight(); $x = ($this->image->getimagewidth() - $sig->getimagewidth()) / 2; $y = 330; // Have to add a border as the x displacement in compositeImage() is broken! $sig->borderImage("transparent", $x, $y); $this->image->compositeImage($sig, Gmagick::COMPOSITE_OVER, 0, 0); // Some comp styles seem to throw errors! } } // Add the role if we've been asked for one if (isset($role)) { $font_size = 17; $draw = new GmagickDraw(); $draw->setFontSize($font_size); $draw->setFont(DOCROOT . "/staticfiles/img/polaroids/monaco.ttf"); $draw->setFillColor('#666'); $text_width = $font_size * strlen($role) * 0.77; // Seems to be about a 0.77 ration for monaco between width and height $x = ($this->image->getimagewidth() - $text_width) / 2; $this->image->annotateimage($draw, $x, 395, 0, $role); } // Rotate the image if ($rotation != 0) { $frame->magnifyimage(); $frame->magnifyimage(); $this->image->rotateimage('transparent', $rotation); $frame->minifyimage(); $frame->minifyimage(); } }
/** * Set and apply the text on the image * * @param string $string * @throws Exception * @return Gmagick */ public function text($string) { $draw = new \GmagickDraw(); // Set the font if passed if (null !== $this->font) { if (!$draw->setFont($this->font)) { throw new Exception('Error: That font is not recognized by the Gmagick extension.'); } // Else, attempt to set a basic, default system font } else { $fonts = $this->image->resource()->queryFonts(); if (in_array('Arial', $fonts)) { $this->font = 'Arial'; } else { if (in_array('Helvetica', $fonts)) { $this->font = 'Helvetica'; } else { if (in_array('Tahoma', $fonts)) { $this->font = 'Tahoma'; } else { if (in_array('Verdana', $fonts)) { $this->font = 'Verdana'; } else { if (in_array('System', $fonts)) { $this->font = 'System'; } else { if (in_array('Fixed', $fonts)) { $this->font = 'Fixed'; } else { if (in_array('system', $fonts)) { $this->font = 'system'; } else { if (in_array('fixed', $fonts)) { $this->font = 'fixed'; } else { if (isset($fonts[0])) { $this->font = $fonts[0]; } else { throw new Exception('Error: No default font could be found by the Gmagick extension.'); } } } } } } } } } } $draw->setFont($this->font); $draw->setFontSize($this->size); $draw->setFillColor($this->image->getColor($this->fillColor, $this->opacity)); if (null !== $this->rotation) { $draw->rotate($this->rotation); } if (null !== $this->strokeColor) { $draw->setStrokeColor($this->image->getColor($this->strokeColor, $this->opacity)); $draw->setStrokeWidth((int) $this->strokeWidth); } $draw->annotate($this->x, $this->y, $string); $this->image->resource()->drawImage($draw); 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())); } }