/** * 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; }
/** * 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; }
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(); } }
/** * 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; }
/** * 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; }