/** * Draws an ellipse on the handle * * @param ImageMagick-object $handle The handle on which the ellipse is drawn * @param Zend_Image_Action_DrawEllipse $ellipseObject The object that with all info */ public function perform(Zend_Image_Adapter_ImageMagick $adapter, Zend_Image_Action_DrawEllipse $ellipseObject) { $draw = new ImagickDraw(); $strokeColor = (string) $ellipseObject->getStrokeColor(); $strokeAlpha = $ellipseObject->getStrokeAlpha() * 0.01; $draw->setStrokeColor($strokeColor); $draw->setStrokeOpacity($strokeAlpha); $draw->setStrokeWidth($ellipseObject->getStrokeWidth()); $strokeDashArray = $ellipseObject->getStrokeDashPattern(); if (count($strokeDashArray) > 0) { $draw->setStrokeDashArray($strokeDashArray); } $draw->setStrokeDashOffset($ellipseObject->getStrokeDashOffset()); if ($ellipseObject->filled()) { $fillColor = (string) $ellipseObject->getFillColor(); $draw->setFillColor($fillColor); $draw->setFillOpacity($ellipseObject->getFillAlpha() * 0.01); } else { $draw->setFillOpacity(0); } $width = $ellipseObject->getWidth(); $height = $ellipseObject->getHeight(); $x = $ellipseObject->getLocation()->getX(); $y = $ellipseObject->getLocation()->getY(); $draw->ellipse($x, $y, $width / 2, $height / 2, 0, 360); $adapter->getHandle()->drawImage($draw); }
/** * Draws a polygon on the handle * * @param ImageMagick-object $handle The handle on which the polygon is drawn * @param Zend_Image_Action_DrawPolygon $polygon The object that with all info */ public function perform($handle, Zend_Image_Action_DrawPolygon $polygon) { // As of ZF2.0 / PHP5.3, this can be made static. $points = $this->_parsePoints($polygon->getPoints()); if ($polygon->isClosed()){ //add first point at the end to close $points[count($points)] = $points[0]; } $draw = new ImagickDraw(); $draw->setStrokeColor('#' . $polygon->getStrokeColor()->getHex()); $draw->setStrokeOpacity($polygon->getStrokeAlpha()/100); $draw->setStrokeWidth($polygon->getStrokeWidth()); $strokeDashArray = $polygon->getStrokeDashPattern(); if (count($strokeDashArray) > 0){ $draw->setStrokeDashArray($strokeDashArray); } $draw->setStrokeDashOffset($polygon->getStrokeDashOffset()); if($polygon->isFilled()) { $fillColor = $polygon->getFillColor(); $draw->setFillColor('#' . $fillColor->getHex()); $draw->polygon($points); } else { //Use transparent fill to render unfilled $draw->setFillOpacity(0); $draw->polyline($points); } $handle->getHandle()->drawImage($draw); }
public function printText($canvas, $size, $angle, $x, $y, $color, $fontfile, $text, $opacity = 100) { $draw = new ImagickDraw(); $draw->setFillColor(self::rgb($color)); $draw->setFontSize($size * $this->fontSizeScale); $draw->setFont($fontfile); $draw->setFillOpacity($opacity / 100); $canvas->annotateImage($draw, $x, $y, $angle, $text); }
public function draw(OsuSignature $signature) { if (empty($this->hexColour)) { $this->hexColour = $signature->getHexColour(); } $composite = new Imagick(); $composite->newPseudoImage($this->getWidth(), $this->getHeight(), "canvas:transparent"); // Background $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel("#555555")); $draw->rectangle(0, 0, $this->getWidth(), $this->getHeight()); $composite->drawImage($draw); // Main bar $level = $signature->getUser()['level']; $xp = $level - floor($level); $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel($this->hexColour)); $draw->rectangle(0, 0, $this->getWidth() * $xp, $this->getHeight()); $composite->drawImage($draw); // Bar end glow $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel('#ffffff')); $draw->setFillOpacity(0.35); $draw->rectangle($this->getWidth() * $xp - $this->getHeight(), 0, $this->getWidth() * $xp, $this->getHeight()); $composite->drawImage($draw); // Text draw & metrics $textDraw = new ImagickDraw(); $textDraw->setFillColor(new ImagickPixel('#555555')); $textDraw->setFontSize(12); $textDraw->setFont(ComponentLabel::FONT_DIRECTORY . ComponentLabel::FONT_REGULAR); $textDraw->setGravity(Imagick::GRAVITY_NORTHWEST); $metrics = $composite->queryFontMetrics($textDraw, 'lv' . floor($level)); // Text white bg $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel('#ffffff')); $draw->rectangle(($this->getWidth() - $metrics['textWidth']) / 2 - 2, 0, ($this->getWidth() + $metrics['textWidth']) / 2 + 1, $this->getHeight()); $composite->drawImage($draw); // Rounding $roundMask = new Imagick(); $roundMask->newPseudoImage($this->getWidth(), $this->getHeight(), "canvas:transparent"); $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel("black")); $draw->roundRectangle(0, 0, $this->getWidth() - 1, $this->getHeight() - 1, $this->rounding, $this->rounding); $roundMask->drawImage($draw); $roundMask->setImageFormat('png'); $composite->compositeImage($roundMask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA); $signature->getCanvas()->compositeImage($composite, Imagick::COMPOSITE_DEFAULT, $this->x, $this->y); // Level text $signature->getCanvas()->annotateImage($textDraw, $this->x + ($this->getWidth() - $metrics['textWidth']) / 2, $this->y + ($this->getHeight() - $metrics['textHeight']) / 2 - 2, 0, 'lv' . floor($level)); }
public function draw($image) { $draw = new \ImagickDraw(); $draw->setStrokeWidth($this->borderSize); if (null !== $this->fillColor) { $fillColor = new \ImagickPixel($this->fillColor->getHexString()); $draw->setFillColor($fillColor); } else { $draw->setFillOpacity(0); } if (null !== $this->borderColor) { $borderColor = new \ImagickPixel($this->borderColor->getHexString()); $draw->setStrokeColor($borderColor); } else { $draw->setStrokeOpacity(0); } $draw->polygon($this->points()); $image->getCore()->drawImage($draw); return $image; }
public function draw($image) { $draw = new \ImagickDraw(); $draw->setStrokeWidth($this->borderSize); if (null !== $this->fillColor) { $fillColor = new \ImagickPixel($this->fillColor->getHexString()); $draw->setFillColor($fillColor); } else { $draw->setFillOpacity(0); } if (null !== $this->borderColor) { $borderColor = new \ImagickPixel($this->borderColor->getHexString()); $draw->setStrokeColor($borderColor); } else { $draw->setStrokeOpacity(0); } $x1 = $this->pos[0]; $x2 = $x1 + $this->getWidth(); $y1 = $this->pos[1]; $y2 = $y1 + $this->getHeight(); $draw->rectangle($x1, $y1, $x2, $y2); $image->getCore()->drawImage($draw); return $image; }
/** * Execute a text * * @param string $text * @param int $offsetX * @param int $offsetY * @param float $opacity * @param int $color * @param int $size * @param string $font_file * * @return static */ public function text($text, $offsetX = 0, $offsetY = 0, $opacity = 1.0, $color = 0x0, $size = 12, $font_file = null) { $draw = new \ImagickDraw(); $textColor = sprintf('rgb(%u,%u,%u)', $color >> 16 & 0xff, $color >> 8 & 0xff, $color & 0xff); $draw->setFillColor(new \ImagickPixel($textColor)); if ($font_file) { $draw->setFont($this->alias->resolve($font_file)); } $draw->setFontSize($size); $draw->setFillOpacity($opacity); $draw->setGravity(\Imagick::GRAVITY_NORTHWEST); $this->_image->annotateImage($draw, $offsetX, $offsetY, 0, $text); $draw->destroy(); return $this; }
/** * @param array $options * 'watermark' => waImage|string $watermark * 'opacity' => float|int 0..1 * 'align' => self::ALIGN_* const * 'font_file' => null|string If null - will be used some default font. Note: use when watermark option is text * 'font_size' => float Size of font. Note: use when watermark option is text * 'font_color' => string Hex-formatted of color (without #). Note: use when watermark option is text * 'text_orientation' => self::ORIENTATION_* const. Note: use when watermark option is text * @return mixed */ protected function _watermark($options) { $opacity = 0.5; $watermark = false; $align = self::ALIGN_BOTTOM_RIGHT; $font_file = null; $font_size = 12; $font_color = '888888'; $text_orientation = self::ORIENTATION_HORIZONTAL; extract($options, EXTR_IF_EXISTS); $opacity = min(max($opacity, 0), 1); /** * @var waImage $watermark */ if ($watermark instanceof waImage) { $width = ifset($options['width'], $watermark->width); $height = ifset($options['height'], $watermark->height); $offset = $this->calcWatermarkOffset($width, $height, $align); $iwatermark = new Imagick($watermark->file); if ($width != $watermark->width || $height != $watermark->height) { $iwatermark->resizeImage($width, $height, Imagick::FILTER_CUBIC, 0.5); } if (method_exists($iwatermark, 'setImageAlphaChannel')) { try { $iwatermark->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE); } catch (Exception $e) { } } $iwatermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity, Imagick::CHANNEL_ALPHA); $this->im->compositeImage($iwatermark, Imagick::COMPOSITE_DISSOLVE, $offset[0], $offset[1]); $iwatermark->clear(); $iwatermark->destroy(); } else { $text = (string) $watermark; if (!$text) { return; } $font_size = 24 * $font_size / 18; // 24px = 18pt $font_color = new ImagickPixel('#' . $font_color); $watermark = new ImagickDraw(); $watermark->setFillColor($font_color); $watermark->setFillOpacity($opacity); if ($font_file && file_exists($font_file)) { $watermark->setFont($font_file); } $watermark->setFontSize($font_size); // Throws ImagickException on error $metrics = $this->im->queryFontMetrics($watermark, $text); $width = $metrics['textWidth']; $height = $metrics['textHeight']; if ($text_orientation == self::ORIENTATION_VERTICAL) { list($width, $height) = array($height, $width); } $margin = round($font_size * 0.21); list($offset, $rotation) = $this->calcWatermarkTextOffset($width, $height, $align, $text_orientation, ifset($options['rotation']), $margin); $this->im->annotateImage($watermark, $offset[0], $offset[1], $rotation, $text); $watermark->clear(); $watermark->destroy(); } }
/*** Set memory limit to 8 MB ***/ $im->setResourceLimit(Imagick::RESOURCETYPE_MEMORY, 8); /*** read the image into the object ***/ $im->readImage($image); /* Create a drawing object and set the font size */ $draw = new ImagickDraw(); /*** set the font ***/ $draw->setFont("/Library/Fonts/Impact.ttf"); /*** set the font size ***/ $draw->setFontSize(36); /*** add some transparency ***/ /*** set gravity to the center ***/ $draw->setGravity(Imagick::GRAVITY_SOUTHEAST); /*** overlay the text on the image ***/ $draw->setFillColor('white'); $draw->setFillOpacity(0.6); $im->annotateImage($draw, 0, 0, 0, "voices4earth.org"); /**** set to png ***/ $im->setImageFormat("png"); /*** write image to disk ***/ $im->writeImage('/Library/WebServer/Documents/Sites/VEJ/temp/watermark_overlay.png'); echo 'Image Created'; } catch (Exception $e) { echo $e->getMessage(); } ///Library/WebServer/Documents/Sites/VEJ/temp/dropshadow.png ?> </section>
public function addWatermarkTextMosaic($text = "Copyright", $font = "Courier", $size = 20, $color = "grey70", $maskColor = "grey30", $position = \Imagick::GRAVITY_SOUTHEAST) { $watermark = new \Imagick(); $draw = new \ImagickDraw(); $watermark->newImage(140, 80, new \ImagickPixel('none')); $draw->setFont($font); $draw->setFillColor('grey'); $draw->setFillOpacity(0.4); $draw->setGravity(\Imagick::GRAVITY_NORTHWEST); $watermark->annotateImage($draw, 10, 10, 0, $text); $draw->setGravity(\Imagick::GRAVITY_SOUTHEAST); $watermark->annotateImage($draw, 5, 15, 0, $text); for ($w = 0; $w < $this->getImage()->getImageWidth(); $w += 140) { for ($h = 0; $h < $this->getImage()->getImageHeight(); $h += 80) { $this->getImage()->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $w, $h); } } }
function draw_text($poster, $y, $font_size, $text) { $draw = new ImagickDraw(); $draw->setFont(is_ascii($text) ? '../font/bernhc.ttf' : '../font/ヒラギノ角ゴ StdN W8.otf'); $draw->setFillOpacity(0.6); $draw->setFontSize($font_size); $draw->annotation(0, $font_size, $text); $fm = $poster->queryFontMetrics($draw, $text, false); $text_im = trample_text_layer($draw, $fm['textWidth'], $fm['textHeight']); if ($text_im->getImageWidth() > 160) { $text_im->scaleImage(160, $text_im->getImageHeight()); } $poster->compositeImage($text_im, imagick::COMPOSITE_OVER, ($poster->getImageWidth() - $text_im->getImageWidth()) / 2, $y); }
public function previewWithCropAction() { $previewWidth = 390; $previewHeight = 184; $fileRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($this->_getParam('uploadId')); if (!$fileRow) { throw new Kwf_Exception("Can't find upload"); } if ($fileRow->getHashKey() != $this->_getParam('hashKey')) { throw new Kwf_Exception_AccessDenied(); } //Scale dimensions $dimensions = array($previewWidth, $previewHeight, 'cover' => false); $cache = Kwf_Assets_Cache::getInstance(); $cacheId = 'previewLarge_' . $fileRow->id; if (!($output = $cache->load($cacheId))) { $output = array(); $output['contents'] = Kwf_Media_Image::scale($fileRow->getFileSource(), $dimensions, $fileRow->id); $output['mimeType'] = $fileRow->mime_type; $cache->save($output, $cacheId); } $cropX = $this->_getParam('cropX'); $cropY = $this->_getParam('cropY'); $cropWidth = $this->_getParam('cropWidth'); $cropHeight = $this->_getParam('cropHeight'); $imageOriginal = new Imagick($fileRow->getFileSource()); if ($this->_getParam('cropX') == null || $this->_getParam('cropY') == null || $this->_getParam('cropWidth') == null || $this->_getParam('cropHeight') == null) { //calculate default selection $dimension = $this->_getParam('dimension'); if (!$dimension) { Kwf_Media_Output::output($output); } $dimension = array('width' => $this->_getParam('dimension_width') ? $this->_getParam('dimension_width') : 0, 'height' => $this->_getParam('dimension_height') ? $this->_getParam('dimension_height') : 0, 'cover' => $this->_getParam('dimension_cover') ? $this->_getParam('dimension_cover') : false); if ($dimension['width'] == Kwf_Form_Field_Image_UploadField::USER_SELECT) { $dimension['width'] = $this->_getParam('width'); } if ($dimension['height'] == Kwf_Form_Field_Image_UploadField::USER_SELECT) { $dimension['height'] = $this->_getParam('height'); } if (!$dimension['cover']) { Kwf_Media_Output::output($output); } if ($dimension['width'] == Kwf_Form_Field_Image_UploadField::CONTENT_WIDTH) { Kwf_Media_Output::output($output); } if ($dimension['height'] == 0 || $dimension['width'] == 0) { Kwf_Media_Output::output($output); } $cropX = 0; $cropY = 0; $cropHeight = $imageOriginal->getImageHeight(); $cropWidth = $imageOriginal->getImageWidth(); if ($imageOriginal->getImageHeight() / $dimension['height'] > $imageOriginal->getImageWidth() / $dimension['width']) { // orientate on width $cropHeight = $dimension['height'] * $imageOriginal->getImageWidth() / $dimension['width']; $cropY = ($imageOriginal->getImageHeight() - $cropHeight) / 2; } else { // orientate on height $cropWidth = $dimension['width'] * $imageOriginal->getImageHeight() / $dimension['height']; $cropX = ($imageOriginal->getImageWidth() - $cropWidth) / 2; } } // Calculate values relative to preview image $image = new Imagick(); $image->readImageBlob($output['contents']); $previewFactor = 1; if ($image->getImageWidth() == $previewWidth) { $previewFactor = $image->getImageWidth() / $imageOriginal->getImageWidth(); } else { if ($image->getImageHeight() == $previewHeight) { $previewFactor = $image->getImageHeight() / $imageOriginal->getImageHeight(); } } $cropX = floor($cropX * $previewFactor); $cropY = floor($cropY * $previewFactor); $cropWidth = floor($cropWidth * $previewFactor); $cropHeight = floor($cropHeight * $previewFactor); $draw = new ImagickDraw(); if ($this->_isLightImage($output)) { $draw->setFillColor('black'); } else { $draw->setFillColor('white'); } $draw->setFillOpacity(0.3); // if cropX == 0 or cropY == 0 no rectangle should be drawn, because it // can't be 0 wide on topmost and leftmost position so it will result in // a 1px line which is wrong //Top region if ($cropY > 0) { $draw->rectangle(0, 0, $image->getImageWidth(), $cropY); } //Left region if ($cropX > 0) { if ($cropY > 0) { $draw->rectangle(0, $cropY + 1, $cropX, $cropY + $cropHeight - 1); } else { $draw->rectangle(0, $cropY, $cropX, $cropY + $cropHeight - 1); } } //Right region if ($cropY > 0) { $draw->rectangle($cropX + $cropWidth, $cropY + 1, $image->getImageWidth(), $cropY + $cropHeight - 1); } else { $draw->rectangle($cropX + $cropWidth, $cropY, $image->getImageWidth(), $cropY + $cropHeight - 1); } //Bottom region $draw->rectangle(0, $cropY + $cropHeight, $image->getImageWidth(), $image->getImageHeight()); $image->drawImage($draw); $output['contents'] = $image->getImageBlob(); Kwf_Media_Output::output($output); }
/** * 添加文字注解,可用于文字水印 * * @param string $txt 必须为utf8编码,文档格式utf-8格式即可 * @param float $opacity 设置不透明度 * @param constant $gravity 设置文字摆放位置,GRAVITY_NorthWest,GRAVITY_North,GRAVITY_NorthEast,GRAVITY_West, * GRAVITY_Center,GRAVITY_East,GRAVITY_SouthWest,GRAVITY_South,GRAVITY_SouthEast,SAE_Static * @param array $font 字体数组可以设置如下属性: * <pre> * name,常量,字体名称,如果需要添加中文注解,请使用中文字体,否则中文会显示乱码。 * 支持的字体:FONT_SimSun(宋体,默认)、FONT_SimKai(楷体)、FONT_SimHei(正黑)、FONT_MicroHei(微米黑)、FONT_Arial * weight,字体宽度,int * size,字体大小,int * color,字体颜色,例如:"blue", "#0000ff", "rgb(0,0,255)"等,默认为"black"; * </pre> * * @return bool * @author Sauwe */ public function annotate($txt, $opacity = 0.5, $gravity = SAE_Static, $font = array("name" => FONT_SimSun, "size" => 15, "weight" => 300, "color" => "black")) { if ($this->notValid()) { return false; } $opacity = floatval($opacity); $fontfamily = FONT_SimSun; if (isset($font["name"])) { $fontfamily = $font["name"]; } $fontsize = 15; if (isset($font["size"])) { $fontsize = $font["size"]; } $fontweight = 300; if (isset($font["weight"])) { $fontweight = $font["weight"]; } $fontcolor = "black"; if (isset($font["color"])) { $fontcolor = $font["color"]; } try { $im = new Imagick(); $im->readImageBlob($this->_img_data); $id = new ImagickDraw(); $id->setFont($fontfamily); //这里是一个实际上的字体路径 $id->setFontSize($fontsize); $id->setFontWeight($fontweight); $id->setFillColor(new ImagickPixel($fontcolor)); $id->setGravity($gravity); $id->setFillOpacity($opacity); if ($im->annotateImage($id, 0, 0, 0, $txt)) { $this->_img_data = $im->getImageBlob(); return true; } return false; } catch (Exception $e) { $this->_errno = SAE_ErrUnknown; $this->_errmsg = $e->getMessage(); return false; } }
/** * 添加水印 */ public function watermark() { if (!is_file($this->watermarkFile)) { throw new Exception('Watermark file not found'); } $watermark = new Imagick($this->watermarkFile); if ($watermark->getImageWidth() > $this->getWidth()) { throw new Exception('Watermark file too width'); } if ($watermark->getImageHeight() > $this->getHeight()) { throw new Exception('Watermark file too height'); } $dw = new ImagickDraw(); $dw->setFillOpacity(0.8); $dw->setGravity($this->watermarkPosition); $dw->composite($watermark->getImageCompose(), 0, 0, 0, 0, $watermark); if ($this->getFrames() > 1) { foreach ($this->im as $frame) { $this->im->drawImage($dw); } $this->im->writeImages($this->targetName, TRUE); } else { $this->im->drawImage($dw); $this->im->writeImage($this->targetName); } }
public function watermarkText($overlayText, $position, $param = array('rotation' => 0, 'opacity' => 50, 'color' => 'FFF', 'size' => null)) { $imagick = new Imagick(); $imagick->readImage($this->_file); $size = null; if ($param['size']) { $size = $param['size']; } else { $text = new ImagickDraw(); $text->setFontSize(12); $text->setFont($this->_watermarkFont); $im = new Imagick(); $stringBox12 = $im->queryFontMetrics($text, $overlayText, false); $string12 = $stringBox12['textWidth']; $size = (int) ($this->_width / 2) * 12 / $string12; $im->clear(); $im->destroy(); $text->clear(); $text->destroy(); } $draw = new ImagickDraw(); $draw->setFont($this->_watermarkFont); $draw->setFontSize($size); $draw->setFillOpacity($param['opacity']); switch ($position) { case Gio_Image_Abstract::POS_TOP_LEFT: $draw->setGravity(Imagick::GRAVITY_NORTHWEST); break; case Gio_Image_Abstract::POS_TOP_RIGHT: $draw->setGravity(Imagick::GRAVITY_NORTHEAST); break; case Gio_Image_Abstract::POS_MIDDLE_CENTER: $draw->setGravity(Imagick::GRAVITY_CENTER); break; case Gio_Image_Abstract::POS_BOTTOM_LEFT: $draw->setGravity(Imagick::GRAVITY_SOUTHWEST); break; case Gio_Image_Abstract::POS_BOTTOM_RIGHT: $draw->setGravity(Imagick::GRAVITY_SOUTHEAST); break; default: throw new Exception('Do not support ' . $position . ' type of alignment'); break; } $draw->setFillColor('#' . $param['color']); $imagick->annotateImage($draw, 5, 5, $param['rotation'], $overlayText); $imagick->writeImage($this->_file); $imagick->clear(); $imagick->destroy(); $draw->clear(); $draw->destroy(); }
/** * imagick watermarking * * @param string $filename * @return bool */ function tp_imagick_watermark($filename) { $watermark_text = elgg_get_plugin_setting('watermark_text', 'tidypics'); if (!$watermark_text) { return false; } // plugins can do their own watermark and return false to prevent this function from running if (elgg_trigger_plugin_hook('tp_watermark', 'imagick', $filename, true) === false) { return true; } $owner = elgg_get_logged_in_user_entity(); $watermark_text = tp_process_watermark_text($watermark_text, $owner); $img = new Imagick($filename); $img->readImage($image); $draw = new ImagickDraw(); //$draw->setFont(""); $draw->setFontSize(28); $draw->setFillOpacity(0.5); $draw->setGravity(Imagick::GRAVITY_SOUTH); $img->annotateImage($draw, 0, 0, 0, $watermark_text); if ($img->writeImage($filename) != true) { $img->destroy(); return false; } $img->destroy(); return true; }
private function watermarkText($options) { if (is_file($options->font_style)) { $watermark = new ImagickDraw(); $watermark->setFontSize((int) $options->font_size); $options->font_color = '#' . preg_replace('#[^a-z0-9]+#i', '', $options->font_color); if ($options->opacity > 1) { $options->opacity = $options->opacity / 100; } switch ($options->position) { default: case 'center': $watermark->setGravity(Imagick::GRAVITY_CENTER); break; case 'top-left': $watermark->setGravity(Imagick::GRAVITY_NORTHEAST); break; case 'top-right': $watermark->setGravity(Imagick::GRAVITY_NORTHEAST); break; case 'center-left': $watermark->setGravity(Imagick::GRAVITY_WEST); break; case 'center-right': $watermark->setGravity(Imagick::GRAVITY_EAST); break; case 'top-center': $watermark->setGravity(Imagick::GRAVITY_NORTH); break; case 'bottom-center': $watermark->setGravity(Imagick::GRAVITY_SOUTH); break; case 'bottom-left': $watermark->setGravity(Imagick::GRAVITY_SOUTHWEST); break; case 'bottom-right': $watermark->setGravity(Imagick::GRAVITY_SOUTHEAST); break; } $watermark->setFillColor($options->font_color); $watermark->setFillOpacity((double) $options->opacity); $watermark->setFont($options->font_style); $this->handle->annotateImage($watermark, (int) $options->margin, (int) $options->margin, 0, $options->text); } }
/** * @param array $options * 'watermark' => waImage|string $watermark * 'opacity' => float|int 0..1 * 'align' => self::ALIGN_* const * 'font_file' => null|string If null - will be used some default font. Note: use when watermark option is text * 'font_size' => float Size of font. Note: use when watermark option is text * 'font_color' => string Hex-formatted of color (without #). Note: use when watermark option is text * 'text_orientation' => self::ORIENTATION_* const. Note: use when watermark option is text * @return mixed */ protected function _watermark($options) { // export options to php-vars foreach ($options as $name => $value) { ${$name} = $value; } $opacity = min(max($opacity, 0), 1); /** * @var waImage $watermark */ if ($watermark instanceof waImage) { $offset = $this->calcWatermarkOffset($watermark->width, $watermark->height, $align); $watermark = new Imagick($watermark->file); $watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity, Imagick::CHANNEL_ALPHA); $this->im->compositeImage($watermark, Imagick::COMPOSITE_DEFAULT, $offset[0], $offset[1]); $watermark->clear(); $watermark->destroy(); } else { $text = (string) $watermark; if (!$text) { return; } $font_size = 24 * $font_size / 18; // 24px = 18pt $font_color = new ImagickPixel('#' . $font_color); $watermark = new ImagickDraw(); $watermark->setFillColor($font_color); $watermark->setFillOpacity($opacity); if ($font_file && file_exists($font_file)) { $watermark->setFont($font_file); } $watermark->setFontSize($font_size); // Throws ImagickException on error $metrics = $this->im->queryFontMetrics($watermark, $text); $width = $metrics['textWidth']; $height = $metrics['textHeight']; if ($text_orientation == self::ORIENTATION_VERTICAL) { list($width, $height) = array($height, $width); } $offset = $this->calcWatermarkTextOffset($width, $height, $align, $text_orientation); $this->im->annotateImage($watermark, $offset[0], $offset[1], $text_orientation == self::ORIENTATION_VERTICAL ? -90 : 0, $text); $watermark->clear(); $watermark->destroy(); } }
/** * @see \wcf\system\image\adapter\IImageAdapter::drawText() */ public function drawText($text, $x, $y, $font, $size, $opacity = 1) { $draw = new \ImagickDraw(); $draw->setFillOpacity($opacity); $draw->setFillColor($this->color); $draw->setTextAntialias(true); $draw->setFont($font); $draw->setFontSize($size); // draw text $draw->annotation($x, $y, $text); if ($this->imagick->getImageFormat() == 'GIF') { $this->imagick = $this->imagick->coalesceImages(); do { $this->imagick->drawImage($draw); } while ($this->imagick->nextImage()); } else { $this->imagick->drawImage($draw); } }
<?php $draw = new ImagickDraw(); // clip $draw->setClipRule(Imagick::FILLRULE_EVENODD); var_dump($draw->getClipRule() === Imagick::FILLRULE_EVENODD); $draw->setClipUnits(10); var_dump($draw->getClipUnits()); // fill $draw->setFillColor('yellow'); var_dump($draw->getFillColor()->getColor()); $draw->setFillOpacity(0.5); printf("%.2f\n", $draw->getFillOpacity()); $draw->setFillRule(Imagick::FILLRULE_NONZERO); var_dump($draw->getClipRule() === Imagick::FILLRULE_NONZERO); // gravity $draw->setGravity(Imagick::GRAVITY_SOUTHEAST); var_dump($draw->getGravity() === Imagick::GRAVITY_SOUTHEAST); // stroke $draw->setStrokeAntialias(false); var_dump($draw->getStrokeAntialias()); $draw->setStrokeColor(new ImagickPixel('#F02B88')); var_dump($draw->getStrokeColor()->getColor()); $draw->setStrokeDashArray(array(1, 2, 3)); var_dump($draw->getStrokeDashArray()); $draw->setStrokeDashOffset(-1); var_dump($draw->getStrokeDashOffset()); $draw->setStrokeLineCap(Imagick::LINECAP_SQUARE); var_dump($draw->getStrokeLineCap() === Imagick::LINECAP_SQUARE); $draw->setStrokeLineJoin(Imagick::LINEJOIN_BEVEL); var_dump($draw->getStrokeLineJoin() === Imagick::LINEJOIN_BEVEL);
function setFillOpacity($strokeColor, $fillColor, $backgroundColor) { $draw = new \ImagickDraw(); $draw->setStrokeColor($strokeColor); $draw->setFillColor($fillColor); $draw->setStrokeOpacity(1); $draw->setStrokeWidth(2); $draw->rectangle(100, 200, 200, 300); $draw->setFillOpacity(0.4); $draw->rectangle(300, 200, 400, 300); $imagick = new \Imagick(); $imagick->newImage(500, 500, $backgroundColor); $imagick->setImageFormat("png"); $imagick->drawImage($draw); header("Content-Type: image/png"); echo $imagick->getImageBlob(); }
public function diyAction() { Yaf_Dispatcher::getInstance()->autoRender(FALSE); $id = $this->_req->getQuery('id', 1); if ($this->_req->isPost()) { $this->diy = new DiyModel(); $pre_svg = '<?xml version="1.0" standalone="no" ?>' . trim($_POST['svg_val']); $rdate = APPLICATION_PATH . '/public/Uploads/' . date("Ymd", time()); //文件名 if (!file_exists($rdate)) { chmod(APPLICATION_PATH . '/public/Uploads/', 0777); mkdir($rdate); //创建目录 } $savename = $this->create_unique(); $path = $rdate . '/' . $savename; if (!($file_svg = fopen($path . '.svg', 'w+'))) { echo "不能打开文件 {$path}.'.svg'"; exit; } if (fwrite($file_svg, $pre_svg) === FALSE) { echo "不能写入到文件 {$path}.'.svg'"; exit; } echo "已成功写入"; fclose($file_svg); //$path= APPLICATION_PATH . '/public/Uploads/' . date("Ymd", time()) .'/m-1'; //添加图片转化 $im = new Imagick(); $im->setBackgroundColor(new ImagickPixel('transparent')); $svg = file_get_contents($path . '.svg'); $im->readImageBlob($svg); $im->setImageFormat("png"); $am = $im->writeImage($path . '.png'); $im->thumbnailImage(579, 660, true); /* 改变大小 */ $ams = $im->writeImage($path . '-t.png'); $im->clear(); $im->destroy(); //图片加水印 $waterpath = APPLICATION_PATH . '/public/source/source.png'; $im1 = new Imagick($waterpath); $im2 = new Imagick($path . '.png'); $im2->thumbnailImage(600, 600, true); $dw = new ImagickDraw(); $dw->setGravity(5); $dw->setFillOpacity(0.1); $dw->composite($im2->getImageCompose(), 0, 0, 50, 0, $im2); $im1->drawImage($dw); if (!$im1->writeImage($path . '-s.png')) { echo '加水印失败'; exit; } $im1->clear(); $im2->clear(); $im1->destroy(); $im2->destroy(); //exit; //删除相应的文件 //unlink($path.'.svg'); //unlink($path.'.png'); $filepath = '/Uploads/' . date("Ymd", time()) . '/' . $savename . '.png'; $data['origin_img'] = $filepath; $data['diy_synthetic_img'] = '/Uploads/' . date("Ymd", time()) . '/' . $savename . '-s.png'; $data['diy_preview_img'] = '/Uploads/' . date("Ymd", time()) . '/' . $savename . '-s.png'; $data['user_id'] = 0; $data['source'] = 3; $data['created'] = date("Y-m-d H:i:s", time()); $datas['image'] = $data['diy_synthetic_img']; $datas['user_id'] = 0; $datas['source'] = 2; $datas['state'] = 1; $datas['createtime'] = date("Y-m-d H:i:s", time()); $datas['updatetime'] = date("Y-m-d H:i:s", time()); $diy_picture_id = $this->diy->adddiy($data); //$datas['use'] = $tool; //$datas['author'] = $userinfo['mobile']; $this->userpicture = new UserpictureModel(); $datas['diy_picture_id'] = $diy_picture_id; $this->userpicture->adduserpicture($datas); $response_data['origin_img'] = '/Uploads/' . date("Ymd", time()) . '/' . $savename . '.png'; $response_data['diy_preview_img'] = '/Uploads/' . date("Ymd", time()) . '/' . $savename . '-s.png'; $response_data['diy_thumb_img'] = '/Uploads/' . date("Ymd", time()) . '/' . $savename . '-t.png'; $response_data['diy_picture_id'] = $diy_picture_id; //$this->getView()->display("/index/buy.html",$response_data); $this->_session->set('diypicture', $response_data); $this->_redis = new phpredis(); $this->_redis->set($diy_picture_id, json_encode($response_data)); header("Location:/index/share?diy=" . $diy_picture_id); } else { switch ($id) { case 1: $this->getView()->display("index/diy_1.html"); break; case 2: $this->getView()->display("index/diy_2.html"); break; case 3: $this->getView()->display("index/diy_3.html"); break; case 4: $this->getView()->display("index/diy_4.html"); break; case 5: $this->getView()->display("index/diy_5.html"); break; case 6: $this->getView()->display("index/diy_6.html"); break; case 7: $this->getView()->display("index/diy_7.html"); break; } } }
/** * Watermars the image with the viewer's IP * @return boolean */ public function watermark() { $text = $_SERVER['REMOTE_ADDR']; $font = $_SERVER['DOCUMENT_ROOT'] . '/pixel.ttf'; $image = $this->getImagick(); $width = $image->getimagewidth(); $watermark = new Imagick(); $watermark->newImage(1000, 1000, new ImagickPixel('none')); $draw = new ImagickDraw(); $draw->setFont($font); $draw->setfontsize(30); $draw->setFillColor('gray'); $draw->setFillOpacity(0.3); $watermark->annotateimage($draw, 100, 200, -45, $text); $watermark->annotateimage($draw, 550, 550, 45, $text); $this->iMagick = $image->textureimage($watermark); return true; }
/** * Draws a reactangle into the image. * * @param int $x1 Horizontal start. * @param int $y1 Vertical start. * @param int $x2 Horizontal end. * @param int $y2 Vertical end. */ public function drawRectangle($x1, $y1, $x2, $y2) { $draw = new ImagickDraw(); $draw->setFillColor('wheat'); // Set up some colors to use for fill and outline $draw->setFillOpacity(0); $draw->setStrokeColor(new ImagickPixel('green')); $draw->rectangle($x1, $y1, $x2, $y2); // Draw the rectangle $this->Imgick->drawImage($draw); }
private static function __getImage($img, $type) { $client = new Client(Config::get('services.dropbox.token'), Config::get('services.dropbox.appName')); $fileSystem = new Filesystem(new DropboxAdapter($client, '/images/')); $biggerW = $biggerH = false; if ($img->width > $img->height) { $biggerW = true; } else { $biggerH = true; } $image = $w = $h = null; $public_path = public_path($img->path); //code minh //get image from dropbox if ($img->store == 'dropbox') { try { $file = $fileSystem->read($img->path); $public_path = $file; } catch (Exception $e) { return false; } } //end code if (in_array($type, ['with-logo', 'large-thumb'])) { if ($biggerW) { $w = 450; } else { $h = 450; } } else { if (in_array($type, ['thumb', 'small-thumb'])) { if ($type == 'thumb') { if ($biggerW) { $w = 150; } else { $h = 150; } } else { if ($biggerW) { $w = 100; } else { $h = 100; } } } else { if (in_array($type, ['crop', 'newcrop'])) { $h = 300; $w = 300; if ($type == 'newcrop') { $w = 600; } } } } try { if (in_array($type, ['crop', 'newcrop'])) { $image = Image::make($public_path)->fit($w, $h, function ($constraint) { $constraint->aspectRatio(); }); } else { $image = Image::make($public_path)->resize($w, $h, function ($constraint) { $constraint->aspectRatio(); }); } } catch (Exception $e) { return false; } if ($type == 'with-logo') { if (Cache::has('mask')) { $mask = Cache::get('mask'); } else { $mask = Configure::where('ckey', 'mask')->pluck('cvalue'); if (empty($mask)) { $mask = 'Visual Impact'; } Cache::forever('mask', $mask); } $size = 50; $w = $image->width(); $h = $image->height(); $x = round($w / 2); $y = round($h / 2); $img = Image::canvas($w, $h); $string = wordwrap($mask, 15, '|'); $strings = explode('|', $string); $line = 2; $i = round($y - count($strings) / 2 * ($size + $line)); $from = $i - 20; foreach ($strings as $string) { $draw = new \ImagickDraw(); $draw->setStrokeAntialias(true); $draw->setTextAntialias(true); $draw->setFont(public_path('assets' . DS . 'fonts' . DS . 'times.ttf')); $draw->setFontSize($size); $draw->setFillColor('rgb(0, 0, 0)'); $draw->setFillOpacity(0.2); $draw->setTextAlignment(\Imagick::ALIGN_CENTER); $draw->setStrokeColor('#fff'); $draw->setStrokeOpacity(0.2); $draw->setStrokeWidth(1); $dimensions = $img->getCore()->queryFontMetrics($draw, $string); $posy = $i + $dimensions['textHeight'] * 0.65 / 2; $img->getCore()->annotateImage($draw, $x, $posy, 0, $string); $i += $size + $line; } return $image->insert($img, 'center', $x, $y)->encode('jpg'); } if ($image) { return $image->encode('jpg'); } return false; }
/** * Draw an arc on the image. * * @param int $x * @param int $y * @param int $start * @param int $end * @param int $w * @param int $h * @return Imagick */ public function arc($x, $y, $start, $end, $w, $h = null) { if ($this->strokeWidth == 0) { $this->setStrokeWidth(1); } $wid = $w; $hgt = null === $h ? $w : $h; $draw = new \ImagickDraw(); $draw->setFillOpacity(0); $draw->setStrokeColor($this->image->getColor($this->strokeColor, $this->opacity)); $draw->setStrokeWidth($this->strokeWidth); $draw->ellipse($x, $y, $wid, $hgt, $start, $end); $this->image->resource()->drawImage($draw); return $this; }
$am = $im->writeImage($path_pre . '/Uploads/Uploads/' . $YMD . '/' . $v['cate_id'] . '/' . $v['attri_name'] . '/' . $lspatharr[0] . '-' . $v['updateid'] . '.' . $lspatharr[1]); if (!$am) { echo "图片压缩失败!"; } else { $im_get = new \Imagick($path_pre . '/Uploads/Uploads/' . $YMD . '/' . $v['cate_id'] . '/' . $v['attri_name'] . '/' . $lspatharr[0] . '-' . $v['updateid'] . '.' . $lspatharr[1]); $im_sta = $im_get->getImageColorspace(); if (2 == $im_sta) { list($bg_width, $bg_height) = getimagesize($path_pre . '/Uploads/Uploads/' . $YMD . '/' . $v['cate_id'] . '/' . $v['attri_name'] . '/' . $lspatharr[0] . '-' . $v['updateid'] . '.' . $lspatharr[1]); $bg_width -= 2; $bg_height -= 2; $imm = new \Imagick(); $imm->newImage(2, 2, new \ImagickPixel('#FFFFFE')); $im_get->setImageColorSpace(1); $dww = new \ImagickDraw(); $dww->setGravity(5); $dww->setFillOpacity(0.1); $dww->composite($imm->getImageCompose(), -$bg_width / 2, $bg_height / 2, 0, 0, $imm); $im_get->drawImage($dww); $im_get->writeImage($path_pre . '/Uploads/Uploads/' . $YMD . '/' . $v['cate_id'] . '/' . $v['attri_name'] . '/' . $lspatharr[0] . '-' . $v['updateid'] . '.' . $lspatharr[1]); } $im_get->clear(); $im_get->destroy(); $query_update = "UPDATE hb_orders_goods SET diy_print_path='/Uploads/" . $YMD . "/" . $v['cate_id'] . "/" . $v['attri_name'] . "/" . $lspatharr[0] . "-" . $v['updateid'] . "." . $lspatharr[1] . "' WHERE id=" . $v['updateid']; $result = mysql_query($query_update); $filename = $path_pre . '/Uploads/Uploads/' . $YMD . '/' . $v['cate_id'] . '/' . $v['attri_name'] . '/' . $lspatharr[0] . '-' . $v['updateid'] . '.' . $lspatharr[1]; $file = file_get_contents($filename); //数据块长度为9 $len = pack("N", 9); //数据块类型标志为pHYs $sign = pack("A*", "pHYs"); //X方向和Y方向的分辨率均为300DPI(1像素/英寸=39.37像素/米),单位为米(0为未知,1为米)