/** * Flood the image with a color fill. * * @param int $r * @param int $g * @param int $b * @return Gmagick */ public function fill($r, $g, $b) { $draw = new \GmagickDraw(); $draw->setFillColor($this->image->getColor([(int) $r, (int) $g, (int) $b])); $draw->rectangle(0, 0, $this->image->getWidth(), $this->image->getHeight()); $this->image->resource()->drawImage($draw); return $this; }
/** * (non-PHPdoc) * @see Imagine\Image\FontInterface::box() */ public function box($string, $angle = 0) { $text = new \GmagickDraw(); $text->setfont($this->file); $text->setfontsize($this->size); $text->setfontstyle(\Gmagick::STYLE_OBLIQUE); $info = $this->gmagick->queryfontmetrics($text, $string); $box = new Box($info['textWidth'], $info['textHeight']); return $box; }
private function drawRectangleAndReplaceExistImg($image, $height, $width) { // прямоугольник рисуем снизу изображения высотой 8% от высоты изображения $heightRectagle = $height * 0.08; $gmagicDraw = new GmagickDraw(); $gmagicDraw->setfillcolor("#fff"); $gmagicDraw->rectangle(0, $height - $heightRectagle, $width, $height); $gImage = new Gmagick(); $gImage->readImage($image); $gImage->drawimage($gmagicDraw); $gImage->writeimage($image); $gImage->destroy(); }
/** * {@inheritdoc} */ public function box($string, $angle = 0) { $text = new \GmagickDraw(); $text->setfont($this->file); /** * @see http://www.php.net/manual/en/imagick.queryfontmetrics.php#101027 * * ensure font resolution is the same as GD's hard-coded 96 */ $text->setfontsize((int) ($this->size * (96 / 72))); $text->setfontstyle(\Gmagick::STYLE_OBLIQUE); $info = $this->gmagick->queryfontmetrics($text, $string); $box = new Box($info['textWidth'], $info['textHeight']); return $box; }
private function getFont($family, $style) { $map = array("serif" => "Times", "sans-serif" => "Helvetica", "fantasy" => "Symbol", "cursive" => "serif", "monospance" => "Courier"); $family = strtolower($family); if (isset($map[$family])) { $family = $map[$family]; } return $this->canvas->load_font($family, "unicode", "fontstyle={$style}"); }
public function transform($operation, $parameters) { if ($this->properties["mimetype"] == "image/tilepic") { return false; } # no transformations for Tilepic if (!$this->handle) { return false; } if (!$this->info["TRANSFORMATIONS"][$operation]) { # invalid transformation $this->postError(1655, _t("Invalid transformation %1", $operation), "WLPlugGmagick->transform()"); return false; } # get parameters for this operation $sparams = $this->info["TRANSFORMATIONS"][$operation]; $w = $parameters["width"]; $h = $parameters["height"]; $cw = $this->get("width"); $ch = $this->get("height"); if ((bool) $this->properties['no_upsampling']) { $w = min($cw, round($w)); $h = min($ch, round($h)); } $do_crop = 0; try { switch ($operation) { # ----------------------- case 'ANNOTATE': $d = new GmagickDraw(); if ($parameters['font']) { try { $d->setfont($parameters['font']); } catch (Exception $e) { $this->postError(1655, _t("Couldn't set font to %1. Gmagick error message: %2", $parameters['font'], $e->getMessage()), "WLPlugGmagick->transform()"); break; } } $size = $parameters['size'] > 0 ? $parameters['size'] : 18; $d->setfontsize($size); $inset = $parameters['inset'] > 0 ? $parameters['inset'] : 0; $pw = new GmagickPixel(); $pw->setcolor($parameters['color'] ? $parameters['color'] : "black"); $d->setfillcolor($pw); switch ($parameters['position']) { default: break; } $this->handle->annotateimage($d, $inset, $size + $inset, 0, $parameters['text']); break; # ----------------------- # ----------------------- case 'WATERMARK': if (!file_exists($parameters['image'])) { break; } # gmagick can't handle opacity when compositing images #$vn_opacity_setting = $parameters['opacity']; #if (($vn_opacity_setting < 0) || ($vn_opacity_setting > 1)) { # $vn_opacity_setting = 0.5; #} if (($vn_watermark_width = $parameters['width']) < 10) { $vn_watermark_width = $cw / 2; } if (($vn_watermark_height = $parameters['height']) < 10) { $vn_watermark_height = $ch / 2; } switch ($parameters['position']) { case 'north_east': $vn_watermark_x = $cw - $vn_watermark_width; $vn_watermark_y = 0; break; case 'north_west': $vn_watermark_x = 0; $vn_watermark_y = 0; break; case 'north': $vn_watermark_x = ($cw - $vn_watermark_width) / 2; $vn_watermark_y = 0; break; case 'south_east': $vn_watermark_x = $cw - $vn_watermark_width; $vn_watermark_y = $ch - $vn_watermark_height; break; case 'south': $vn_watermark_x = ($cw - $vn_watermark_width) / 2; $vn_watermark_y = $ch - $vn_watermark_height; break; case 'center': $vn_watermark_x = ($cw - $vn_watermark_width) / 2; $vn_watermark_y = ($ch - $vn_watermark_height) / 2; break; case 'south_west': default: $vn_watermark_x = $cw - $vn_watermark_width; $vn_watermark_y = $ch - $vn_watermark_height; break; } try { $w = new Gmagick($parameters['image']); $this->setResourceLimits($w); } catch (Exception $e) { $this->postError(1610, _t("Couldn't load watermark image at %1", $parameters['image']), "WLPlugGmagick->transform:WATERMARK()"); return false; } $w->scaleimage($vn_watermark_width, $vn_watermark_height); $this->handle->compositeimage($w, 1, $vn_watermark_x, $vn_watermark_y); break; # ----------------------- # ----------------------- case 'SCALE': $aa = $parameters["antialiasing"]; if ($aa <= 0) { $aa = 0; } switch ($parameters["mode"]) { # ---------------- case "width": $scale_factor = $w / $cw; $h = $ch * $scale_factor; break; # ---------------- # ---------------- case "height": $scale_factor = $h / $ch; $w = $cw * $scale_factor; break; # ---------------- # ---------------- case "bounding_box": $scale_factor_w = $w / $cw; $scale_factor_h = $h / $ch; $w = $cw * ($scale_factor_w < $scale_factor_h ? $scale_factor_w : $scale_factor_h); $h = $ch * ($scale_factor_w < $scale_factor_h ? $scale_factor_w : $scale_factor_h); break; # ---------------- # ---------------- case "fill_box": $crop_from = $parameters["crop_from"]; if (!in_array($crop_from, array('center', 'north_east', 'north_west', 'south_east', 'south_west', 'random'))) { $crop_from = ''; } $scale_factor_w = $w / $cw; $scale_factor_h = $h / $ch; $w = $cw * ($scale_factor_w > $scale_factor_h ? $scale_factor_w : $scale_factor_h); $h = $ch * ($scale_factor_w > $scale_factor_h ? $scale_factor_w : $scale_factor_h); $do_fill_box_crop = true; break; # ---------------- } $w = round($w); $h = round($h); if ($w > 0 && $h > 0) { $crop_w_edge = $crop_h_edge = 0; if (preg_match("/^([\\d]+)%\$/", $parameters["trim_edges"], $va_matches)) { $crop_w_edge = ceil(intval($va_matches[1]) / 100 * $w); $crop_h_edge = ceil(intval($va_matches[1]) / 100 * $h); } else { if (isset($parameters["trim_edges"]) && intval($parameters["trim_edges"]) > 0) { $crop_w_edge = $crop_h_edge = intval($parameters["trim_edges"]); } } if (!$this->handle->resizeimage($w + $crop_w_edge * 2, $h + $crop_h_edge * 2, Gmagick::FILTER_CUBIC, $aa)) { $this->postError(1610, _t("Error during resize operation"), "WLPlugGmagick->transform()"); return false; } if ($do_fill_box_crop) { // use face detection info to intelligently crop if (is_array($this->properties['faces']) && sizeof($this->properties['faces'])) { $va_info = array_shift($this->properties['faces']); $crop_from_offset_x = ceil($va_info['x'] * ($scale_factor_w > $scale_factor_h ? $scale_factor_w : $scale_factor_h)); $crop_from_offset_x -= ceil(0.15 * $parameters["width"]); // since face will be tightly cropped give it some room $crop_from_offset_y = ceil($va_info['y'] * ($scale_factor_w > $scale_factor_h ? $scale_factor_w : $scale_factor_h)); $crop_from_offset_y -= ceil(0.15 * $parameters["height"]); // since face will be tightly cropped give it some room // Don't try to crop beyond image boundaries, you just end up scaling the image, often awkwardly if ($crop_from_offset_x > $w - $parameters["width"]) { $crop_from_offset_x = 0; } if ($crop_from_offset_y > $h - $parameters["height"]) { $crop_from_offset_y = 0; } if ($crop_from_offset_x < 0) { $crop_from_offset_x = 0; } if ($crop_from_offset_y < 0) { $crop_from_offset_y = 0; } } else { switch ($crop_from) { case 'north_west': $crop_from_offset_y = 0; $crop_from_offset_x = $w - $parameters["width"]; break; case 'south_east': $crop_from_offset_x = 0; $crop_from_offset_y = $h - $parameters["height"]; break; case 'south_west': $crop_from_offset_x = $w - $parameters["width"]; $crop_from_offset_y = $h - $parameters["height"]; break; case 'random': $crop_from_offset_x = rand(0, $w - $parameters["width"]); $crop_from_offset_y = rand(0, $h - $parameters["height"]); break; case 'north_east': $crop_from_offset_x = $crop_from_offset_y = 0; break; case 'center': default: $crop_from_offset_x = $crop_from_offset_y = 0; // Get image center $vn_center_x = caGetOption('_centerX', $parameters, 0.5); $vn_center_y = caGetOption('_centerY', $parameters, 0.5); if ($w > $parameters["width"]) { $crop_from_offset_x = ceil($w * $vn_center_x) - $parameters["width"] / 2; if ($crop_from_offset_x + $parameters["width"] > $w) { $crop_from_offset_x = $w - $parameters["width"]; } if ($crop_from_offset_x < 0) { $crop_from_offset_x = 0; } } else { if ($h > $parameters["height"]) { $crop_from_offset_y = ceil($h * $vn_center_y) - $parameters["height"] / 2; if ($crop_from_offset_y + $parameters["height"] > $h) { $crop_from_offset_y = $h - $parameters["height"]; } if ($crop_from_offset_y < 0) { $crop_from_offset_y = 0; } } } break; } } if (!$this->handle->cropimage($parameters["width"], $parameters["height"], $crop_w_edge + $crop_from_offset_x, $crop_h_edge + $crop_from_offset_y)) { $this->postError(1610, _t("Error during crop operation"), "WLPlugGmagick->transform()"); return false; } $this->properties["width"] = $parameters["width"]; $this->properties["height"] = $parameters["height"]; } else { if ($crop_w_edge || $crop_h_edge) { if (!$this->handle->cropimage($w, $h, $crop_w_edge, $crop_h_edge)) { $this->postError(1610, _t("Error during crop operation"), "WLPlugGmagick->transform()"); return false; } } $this->properties["width"] = $w; $this->properties["height"] = $h; } } break; # ----------------------- # ----------------------- case "ROTATE": $angle = $parameters["angle"]; if ($angle > -360 && $angle < 360) { if (!$this->handle->rotateimage("#FFFFFF", $angle)) { $this->postError(1610, _t("Error during image rotate"), "WLPlugGmagick->transform():ROTATE"); return false; } } break; # ----------------------- # ----------------------- case "DESPECKLE": if (!$this->handle->despeckleimage()) { $this->postError(1610, _t("Error during image despeckle"), "WLPlugGmagick->transform:DESPECKLE()"); return false; } break; # ----------------------- # ----------------------- case "MEDIAN": $radius = $parameters["radius"]; if ($radius < 0.1) { $radius = 1; } if (!$this->handle->medianfilterimage($radius)) { $this->postError(1610, _t("Error during image median filter"), "WLPlugGmagick->transform:MEDIAN()"); return false; } break; # ----------------------- # ----------------------- case "SHARPEN": $radius = $parameters["radius"]; if ($radius < 0.1) { $radius = 1; } $sigma = $parameters["sigma"]; if ($sigma < 0.1) { $sigma = 1; } if (!$this->handle->sharpenImage($radius, $sigma)) { $this->postError(1610, _t("Error during image sharpen"), "WLPlugGmagick->transform:SHARPEN()"); return false; } break; # ----------------------- # ----------------------- case "UNSHARPEN_MASK": # noop break; # ----------------------- # ----------------------- case "SET": while (list($k, $v) = each($parameters)) { $this->set($k, $v); } break; # ----------------------- } return 1; } catch (Exception $e) { $this->postError(1610, _t("Gmagick exception"), "WLPlugGmagick->transform"); return false; } }
/** * {@inheritdoc} */ public function fill(FillInterface $fill) { try { $draw = new \GmagickDraw(); $size = $this->getSize(); for ($x = 0; $x <= $size->getWidth(); $x++) { for ($y = 0; $y <= $size->getHeight(); $y++) { $pixel = $this->getColor($fill->getColor(new Point($x, $y))); $draw->setfillcolor($pixel); $draw->point($x, $y); $pixel = null; } } $this->gmagick->drawimage($draw); $draw = null; } catch (\GmagickException $e) { throw new RuntimeException('Fill operation failed', $e->getCode(), $e); } return $this; }
/** * {@inheritdoc} */ public function text($string, AbstractFont $font, PointInterface $position, $angle = 0) { try { $pixel = $this->getColor($font->getColor()); $text = new \GmagickDraw(); $text->setfont($font->getFile()); /** * @see http://www.php.net/manual/en/imagick.queryfontmetrics.php#101027 * * ensure font resolution is the same as GD's hard-coded 96 */ $text->setfontsize((int) ($font->getSize() * (96 / 72))); $text->setfillcolor($pixel); $info = $this->gmagick->queryfontmetrics($text, $string); $rad = deg2rad($angle); $cos = cos($rad); $sin = sin($rad); $x1 = round(0 * $cos - 0 * $sin); $x2 = round($info['textWidth'] * $cos - $info['textHeight'] * $sin); $y1 = round(0 * $sin + 0 * $cos); $y2 = round($info['textWidth'] * $sin + $info['textHeight'] * $cos); $xdiff = 0 - min($x1, $x2); $ydiff = 0 - min($y1, $y2); $this->gmagick->annotateimage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string); $pixel = null; $text = null; } catch (\GmagickException $e) { throw new RuntimeException('Draw text operation failed', $e->getCode(), $e); } return $this; }
/** * 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; }
/** * Draw a polygon on the image. * * @param array $points * @return Gmagick */ public function polygon($points) { $draw = new \GmagickDraw(); if (null !== $this->fillColor) { $draw->setFillColor($this->image->getColor($this->fillColor, $this->opacity)); } if ($this->strokeWidth > 0) { $draw->setStrokeColor($this->image->getColor($this->strokeColor, $this->opacity)); $draw->setStrokeWidth($this->strokeWidth); } $draw->polygon($points); $this->image->resource()->drawImage($draw); return $this; }
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(); } }
/** * (non-PHPdoc) * @see Imagine\Draw\DrawerInterface::text() */ public function text($string, AbstractFont $font, PointInterface $position, $angle = 0) { try { $pixel = $this->getColor($font->getColor()); $text = new \GmagickDraw(); $text->setfont($font->getFile()); $text->setfontsize($font->getSize()); $text->setfillcolor($pixel); $info = $this->gmagick->queryfontmetrics($text, $string); $rad = deg2rad($angle); $cos = cos($rad); $sin = sin($rad); $x1 = round(0 * $cos - 0 * $sin); $x2 = round($info['textWidth'] * $cos - $info['textHeight'] * $sin); $y1 = round(0 * $sin + 0 * $cos); $y2 = round($info['textWidth'] * $sin + $info['textHeight'] * $cos); $xdiff = 0 - min($x1, $x2); $ydiff = 0 - min($y1, $y2); $this->gmagick->annotateimage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string); $pixel = null; $text = null; } catch (\GmagickException $e) { throw new RuntimeException('Draw text operation failed', $e->getCode(), $e); } }
/** * Internal method to set a pixel into the existing image object instance * * @param resource the image object instance * @param integer the x position * @param integer the y position * @param integer the color to be set * @return boolean true on success, false on failure */ protected function _set_pixel(&$image, $x, $y, $color) { switch (self::$driver) { case 'gd': imagesetpixel($image, $x, $y, $color); return TRUE; case 'gmagick': $draw = new GmagickDraw(); $draw->setFillColor($color); $draw->point($x, $y); $image->drawImage($draw); return TRUE; case 'imagick': $draw = new ImagickDraw(); $draw->setFillColor($color); $draw->point($x, $y); $image->drawImage($draw); return TRUE; } return FALSE; }
/** * {@inheritdoc} */ public function text($string, AbstractFont $font, PointInterface $position, $angle = 0, $width = null) { try { $pixel = $this->getColor($font->getColor()); $text = new \GmagickDraw(); $text->setfont($font->getFile()); /** * @see http://www.php.net/manual/en/imagick.queryfontmetrics.php#101027 * * ensure font resolution is the same as GD's hard-coded 96 */ $text->setfontsize((int) ($font->getSize() * (96 / 72))); $text->setfillcolor($pixel); $info = $this->gmagick->queryfontmetrics($text, $string); // $rad = deg2rad($angle); // $cos = cos($rad); // $sin = sin($rad); // $x1 = round(0 * $cos - 0 * $sin); // $x2 = round($info['textWidth'] * $cos - $info['textHeight'] * $sin); // $y1 = round(0 * $sin + 0 * $cos); // $y2 = round($info['textWidth'] * $sin + $info['textHeight'] * $cos); // $xdiff = 0 - min($x1, $x2); // $ydiff = 0 - min($y1, $y2); if ($width !== null) { throw new NotSupportedException('Gmagick doesn\'t support queryfontmetrics function for multiline text', 1); } // $this->gmagick->annotateimage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string); $deltaX = $info['characterWidth'] * sin(deg2rad($angle)); $deltaY = $info['characterHeight']; $posX = $position->getX() - $deltaX; if ($posX < 0) { $posX = 0; } $this->gmagick->annotateimage($text, $posX, $position->getY() + $deltaY, $angle, $string); unset($pixel, $text); } catch (\GmagickException $e) { throw new RuntimeException('Draw text 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())); } }