示例#1
0
/* {{{ proto bool GmagickDraw::setFont(string font_name)
	Sets the fully-sepecified font to use when annotating with text.
*/
PHP_METHOD(gmagickdraw, setfont)
{
	php_gmagickdraw_object *internd;
	char *font, *absolute;
	int font_len, error = 0;

	/* Parse parameters given to function */
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &font, &font_len) == FAILURE) {
		return;
	}

	/* Check that no empty string is passed */
	if (font_len == 0) {
		GMAGICK_THROW_EXCEPTION_WITH_MESSAGE(GMAGICKDRAW_CLASS, "Can not set empty font", 2);
	}

	internd = (php_gmagickdraw_object *)zend_object_store_get_object(getThis() TSRMLS_CC);

	/* And if it wasn't */
	if (!check_configured_font(font, font_len TSRMLS_CC)) {

		if (!(absolute = expand_filepath(font, NULL TSRMLS_CC))) {
			GMAGICK_THROW_EXCEPTION_WITH_MESSAGE(GMAGICKDRAW_CLASS, "Unable to set font", 2);
		}

		/* Do a safe-mode check for the font */
		GMAGICK_SAFE_MODE_CHECK(absolute, error);
		GMAGICKDRAW_CHECK_READ_OR_WRITE_ERROR(internd, absolute, error, GMAGICK_FREE_FILENAME);

		if (VCWD_ACCESS(absolute, F_OK|R_OK)) {
			zend_throw_exception_ex(php_gmagickdraw_exception_class_entry, 2 TSRMLS_CC,
				"The given font is not found in the GraphicsMagick configuration and the file (%s) is not accessible", absolute);

			efree(absolute);
			return;
		}

		DrawSetFont(internd->drawing_wand, absolute);
		efree(absolute);

	} else {
		DrawSetFont(internd->drawing_wand, font);
	}

	GMAGICK_CHAIN_METHOD;
}
示例#2
0
文件: zimg.c 项目: Codefor/zimg
char* get_phone_img(const char *phone_str, size_t *img_size){
    if(phone_str == NULL){
	return NULL;
    }

    MagickWand *m_wand  = NULL;
    PixelWand *p_wand  = NULL;
    DrawingWand *d_wand = NULL;


    /* Create a wand */
    m_wand = NewMagickWand();
    p_wand = NewPixelWand();
    d_wand = NewDrawingWand();

    PixelSetColor(p_wand,"white");

    int height = 18;
    int width = strlen(phone_str) * 130 / 12;

    MagickNewImage(m_wand, width,height ,p_wand);

    //draw number
    PixelSetColor(p_wand,"black");
    DrawSetFillColor(d_wand,p_wand);
    DrawSetFont (d_wand, "Arial" ) ;
    DrawSetFontSize(d_wand,20);
    DrawSetStrokeColor(d_wand,p_wand);
    DrawAnnotation(d_wand,0,height -2,phone_str);
    MagickDrawImage(m_wand,d_wand);
    //MagickTrimImage(m_wand,0);
    //ImageFormat MUST be SET,otherwise,otherwise we will not MagickGetImageBlob properly
    MagickSetImageFormat(m_wand,"JPEG");

    char *p = NULL;
    char *data = NULL;

    p = (char *)MagickGetImageBlob(m_wand,img_size);

    if(p != NULL){
	data = (char *)malloc(*img_size);
	if(data != NULL){
	    memcpy(data,p,*img_size);
	}else{
	    LOG_PRINT(LOG_INFO, "malloc Failed!");
	}
    }else{
	LOG_PRINT(LOG_INFO, "MagickGetImageBlob Failed!");
    }
    
    /* Tidy up */
    MagickRelinquishMemory(p);
    DestroyMagickWand(m_wand);
    DestroyPixelWand(p_wand);

    return data;
}
示例#3
0
static bool HHVM_METHOD(ImagickDraw, setFont,
    const String& font_name) {
  auto wand = getDrawingWandResource(Object{this_});
  if (font_name.empty()) {
    IMAGICKDRAW_THROW("Can not set empty font");
  }
  auto font = magickResolveFont(font_name);
  if (font.isNull()) {
    IMAGICKDRAW_THROW("Unable to set font, file path expansion failed");
  }
  auto status = DrawSetFont(wand->getWand(), font.c_str());
  if (status == MagickFalse) {
    IMAGICKDRAW_THROW("Unable to set font");
  }
  return true;
}
示例#4
0
文件: magick.c 项目: BobPyron/calibre
static int
magick_DrawingWand_font_setter(magick_DrawingWand *self, PyObject *val, void *closure) {
    char *fmt;
    NULL_CHECK(-1);

    if (val == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete DrawingWand font");
        return -1;
    }

    fmt = PyString_AsString(val);
    if (fmt == NULL) return -1;

    if (!DrawSetFont(self->wand, fmt)) {
        PyErr_SetString(PyExc_ValueError, "Unknown font");
        return -1;
    }

    return 0;
}
示例#5
0
文件: main.cpp 项目: vic-w/MyUniverse
void test_wand(void)
{
	MagickWand *magick_wand = NULL;
	MagickWand *c_wand = NULL;
	DrawingWand *d_wand = NULL;
	PixelWand *p_wand = NULL;

	// Used for text effect #3
	double dargs[1] = {120.};

	// Used for text effect #5
	double d_args[8] = {
		-0.02,0.0,
		0.0,1.02,
		0.0,0.0,
		-0.5,1.9
	};

	MagickWandGenesis();

// Text effect 1 - shadow effect using MagickShadowImage
// This is derived from Anthony's Soft Shadow effect
// convert -size 300x100 xc:none -font Candice -pointsize 72 \
//           -fill white  -stroke black  -annotate +25+65 'Anthony' \
//           \( +clone -background navy  -shadow 70x4+5+5 \) +swap \
//           -background lightblue -flatten  -trim +repage  font_shadow_soft.jpg

//NOTE - if an image has a transparent background, adding a border of any colour other 
// than "none" will remove all the transparency and replace it with the border's colour

	magick_wand = NewMagickWand();
	d_wand = NewDrawingWand();
	p_wand = NewPixelWand();
	PixelSetColor(p_wand,"none");
	// Create a new transparent image
	MagickNewImage(magick_wand,350,100,p_wand);

	// Set up a 72 point white font 
	PixelSetColor(p_wand,"white");
	DrawSetFillColor(d_wand,p_wand);
	DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
	DrawSetFontSize(d_wand,72);
	// Add a black outline to the text
	PixelSetColor(p_wand,"black");
	DrawSetStrokeColor(d_wand,p_wand);
	// Turn antialias on - not sure this makes a difference
	DrawSetTextAntialias(d_wand,MagickTrue);
	// Now draw the text
	DrawAnnotation(d_wand,25,65,(const unsigned char *)"Magick");
	// Draw the image on to the magick_wand
	MagickDrawImage(magick_wand,d_wand);

	// Trim the image down to include only the text
	MagickTrimImage(magick_wand,0);
	
	// equivalent to the command line +repage
	MagickResetImagePage(magick_wand,"");

	// Make a copy of the text image
	c_wand = CloneMagickWand(magick_wand);

	// Set the background colour to blue for the shadow
	PixelSetColor(p_wand,"blue");

	MagickSetImageBackgroundColor(magick_wand,p_wand);
	// Opacity is a real number indicating (apparently) percentage
	MagickShadowImage(magick_wand,70,4,5,5);

	// Composite the text on top of the shadow
	MagickCompositeImage(magick_wand,c_wand,OverCompositeOp,5,5);
 
	if(c_wand)c_wand = DestroyMagickWand(c_wand);
	c_wand = NewMagickWand();

	// Create a new image the same size as the text image and put a solid colour
	// as its background
	PixelSetColor(p_wand,"rgb(125,215,255)");
	MagickNewImage(c_wand,MagickGetImageWidth(magick_wand),MagickGetImageHeight(magick_wand),p_wand);
	// Now composite the shadowed text over the plain background
	MagickCompositeImage(c_wand,magick_wand,OverCompositeOp,0,0);
	// and write the result
	MagickWriteImage(c_wand,"text_shadow.png"); 

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(c_wand)c_wand = DestroyMagickWand(c_wand);
	if(d_wand)d_wand = DestroyDrawingWand(d_wand);
	if(p_wand)p_wand = DestroyPixelWand(p_wand);

// Text effect 2 - tiled text using the builtin checkerboard pattern
// Anthony's Tiled Font effect
// convert -size 320x100 xc:lightblue -font Candice -pointsize 72 \
//          -tile pattern:checkerboard   -annotate +28+68 'Anthony' \
//           font_tile.jpg

	magick_wand = NewMagickWand();
	d_wand = NewDrawingWand();
	p_wand = NewPixelWand();

	set_tile_pattern(d_wand,"#check","pattern:checkerboard");

	PixelSetColor(p_wand,"lightblue");
	// Create a new transparent image
	MagickNewImage(magick_wand,320,100,p_wand);

	// Set up a 72 point font 
	DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
	DrawSetFontSize(d_wand,72);
	// Now draw the text
	DrawAnnotation(d_wand,28,68,(const unsigned char *)"Magick");
	// Draw the image on to the magick_wand
	MagickDrawImage(magick_wand,d_wand);
	// Trim the image
	MagickTrimImage(magick_wand,0);
	// Add a transparent border
	PixelSetColor(p_wand,"lightblue");
	MagickBorderImage(magick_wand,p_wand,5,5);
	// and write it
	MagickWriteImage(magick_wand,"text_pattern.png");

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(d_wand)d_wand = DestroyDrawingWand(d_wand);
	if(p_wand)p_wand = DestroyPixelWand(p_wand);

// Text effect 3 -  arc font (similar to http://www.imagemagick.org/Usage/fonts/#arc) 
//convert -size 320x100 xc:lightblue -font Candice -pointsize 72 \
//           -annotate +25+65 'Anthony' -distort Arc 120 \
//           -trim +repage -bordercolor lightblue -border 10  font_arc.jpg
	magick_wand = NewMagickWand();
	d_wand = NewDrawingWand();
	p_wand = NewPixelWand();

	// Create a 320x100 lightblue canvas
	PixelSetColor(p_wand,"lightblue");
	MagickNewImage(magick_wand,320,100,p_wand);

	// Set up a 72 point font 
	DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
	DrawSetFontSize(d_wand,72);
	// Now draw the text
	DrawAnnotation(d_wand,25,65,(const unsigned char *)"Magick");
	// Draw the image on to the magick_wand
	MagickDrawImage(magick_wand,d_wand);

	MagickDistortImage(magick_wand,ArcDistortion,1,dargs,MagickFalse);
	// Trim the image
	MagickTrimImage(magick_wand,0);
	// Add the border
	PixelSetColor(p_wand,"lightblue");
	MagickBorderImage(magick_wand,p_wand,10,10);

	// and write it
	MagickWriteImage(magick_wand,"text_arc.png");

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(d_wand)d_wand = DestroyDrawingWand(d_wand);
	if(p_wand)p_wand = DestroyPixelWand(p_wand);

// Text effect 4 - bevelled font http://www.imagemagick.org/Usage/fonts/#bevel
// convert -size 320x100 xc:black -font Candice -pointsize 72 \
//              -fill white   -annotate +25+65 'Anthony' \
//              -shade 140x60  font_beveled.jpg
	magick_wand = NewMagickWand();
	d_wand = NewDrawingWand();
	p_wand = NewPixelWand();

	// Create a 320x100 canvas
	PixelSetColor(p_wand,"gray");
	MagickNewImage(magick_wand,320,100,p_wand);
	// Set up a 72 point font 
	DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
	DrawSetFontSize(d_wand,72);
	// Set up a 72 point white font 
	PixelSetColor(p_wand,"white");
	DrawSetFillColor(d_wand,p_wand);
	// Now draw the text
	DrawAnnotation(d_wand,25,65,(const unsigned char *)"Magick");
	// Draw the image on to the magick_wand
	MagickDrawImage(magick_wand,d_wand);
	// the "gray" parameter must be true to get the effect shown on Anthony's page
	MagickShadeImage(magick_wand,MagickTrue,140,60);

#ifdef COLORIZE
	PixelSetColor(p_wand,"yellow");
	DrawSetFillColor(d_wand,p_wand);
	cp_wand = NewPixelWand();
	PixelSetColor(cp_wand,"gold");
	MagickColorizeImage(magick_wand,p_wand,cp_wand);
#endif
	// and write it
	MagickWriteImage(magick_wand,"text_bevel.png");

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(d_wand)d_wand = DestroyDrawingWand(d_wand);
	if(p_wand)p_wand = DestroyPixelWand(p_wand);
#ifdef COLORIZE
	if(cp_wand)cp_wand = DestroyPixelWand(cp_wand);
#endif


// Text effect 5 and 6 - Plain text and then Barrel distortion
	// This one uses d_args
	magick_wand = NewMagickWand();
	d_wand = NewDrawingWand();
	p_wand = NewPixelWand();

	// Create a 320x100 transparent canvas
	PixelSetColor(p_wand,"none");
	MagickNewImage(magick_wand,320,100,p_wand);

	// Set up a 72 point font 
	DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
	DrawSetFontSize(d_wand,72);
	// Now draw the text
	DrawAnnotation(d_wand,25,65,(const unsigned char *)"Magick");
	// Draw the image on to the magick_wand
	MagickDrawImage(magick_wand,d_wand);
	MagickWriteImage(magick_wand,"text_plain.png");
	
	// Trim the image
	MagickTrimImage(magick_wand,0);
	// Add the border
	PixelSetColor(p_wand,"none");
	MagickBorderImage(magick_wand,p_wand,10,10);
//	MagickSetImageMatte(magick_wand,MagickTrue);
//	MagickSetImageVirtualPixelMethod(magick_wand,TransparentVirtualPixelMethod);
// 	d_args[0] = 0.1;d_args[1] = -0.25;d_args[2] = -0.25; [3] += .1
	// The first value should be positive. If it is negative the image is *really* distorted
	d_args[0] = 0.0;
	d_args[1] = 0.0;
	d_args[2] = 0.5;
	// d_args[3] should normally be chosen such the sum of all 4 values is 1
	// so that the result is the same size as the original
	// You can override the sum with a different value
	// If the sum is greater than 1 the resulting image will be smaller than the original
	d_args[3] = 1 - (d_args[0] + d_args[1] + d_args[2]);
	// Make the result image smaller so that it isn't as likely
	// to overflow the edges
	// d_args[3] += 0.1;
	// 0.0,0.0,0.5,0.5,0.0,0.0,-0.5,1.9
	d_args[3] = 0.5;
	d_args[4] = 0.0;
	d_args[5] = 0.0;
	d_args[6] = -0.5;
	d_args[7] = 1.9;
	// DON'T FORGET to set the correct number of arguments here
	MagickDistortImage(magick_wand,BarrelDistortion,8,d_args,MagickTrue);
//	MagickResetImagePage(magick_wand,"");
	// Trim the image again
	MagickTrimImage(magick_wand,0);
	// Add the border
	PixelSetColor(p_wand,"none");
	MagickBorderImage(magick_wand,p_wand,10,10);
	// and write it
	MagickWriteImage(magick_wand,"text_barrel.png");

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(d_wand)d_wand = DestroyDrawingWand(d_wand);
	if(p_wand)p_wand = DestroyPixelWand(p_wand);

// Text effect 7 - Polar distortion
	// This one uses d_args[0]
	magick_wand = NewMagickWand();
	d_wand = NewDrawingWand();
	p_wand = NewPixelWand();

	// Create a 320x200 transparent canvas
	PixelSetColor(p_wand,"none");
	MagickNewImage(magick_wand,320,200,p_wand);

	// Set up a 72 point font 
	DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
	DrawSetFontSize(d_wand,72);
	// Now draw the text
	DrawAnnotation(d_wand,25,65,(const unsigned char *)"Magick");
	// Draw the image on to the magick_wand
	MagickDrawImage(magick_wand,d_wand);

	d_args[0] = 0.0;

	// DON'T FORGET to set the correct number of arguments here
	MagickDistortImage(magick_wand,PolarDistortion,1,d_args,MagickTrue);
//	MagickResetImagePage(magick_wand,"");
	// Trim the image again
	MagickTrimImage(magick_wand,0);
	// Add the border
	PixelSetColor(p_wand,"none");
	MagickBorderImage(magick_wand,p_wand,10,10);
	// and write it
	MagickWriteImage(magick_wand,"text_polar.png");

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(d_wand)d_wand = DestroyDrawingWand(d_wand);
	if(p_wand)p_wand = DestroyPixelWand(p_wand);

// Text effect 8 - Shepard's distortion
	// This one uses d_args[0]
	magick_wand = NewMagickWand();
	d_wand = NewDrawingWand();
	p_wand = NewPixelWand();

	// Create a 320x200 transparent canvas
	PixelSetColor(p_wand,"none");
	MagickNewImage(magick_wand,640,480,p_wand);

	// Set up a 72 point font 
	DrawSetFont (d_wand, "Verdana-Bold-Italic" ) ;
	DrawSetFontSize(d_wand,72);
	// Now draw the text
	DrawAnnotation(d_wand,50,240,(const unsigned char *)"Magick Rocks");
	// Draw the image on to the magick_wand
	MagickDrawImage(magick_wand,d_wand);
	d_args[0] = 150.0;
	d_args[1] = 190.0;
	d_args[2] = 100.0;
	d_args[3] = 290.0;
	d_args[4] = 500.0;
	d_args[5] = 200.0;
	d_args[6] = 430.0;
	d_args[7] = 130.0;
	// DON'T FORGET to set the correct number of arguments here
	MagickDistortImage(magick_wand,ShepardsDistortion,8,d_args,MagickTrue);

	// Trim the image
	MagickTrimImage(magick_wand,0);
	// Add the border
	PixelSetColor(p_wand,"none");
	MagickBorderImage(magick_wand,p_wand,10,10);
	// and write it
	MagickWriteImage(magick_wand,"text_shepards.png");

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(d_wand)d_wand = DestroyDrawingWand(d_wand);
	if(p_wand)p_wand = DestroyPixelWand(p_wand);


	MagickWandTerminus();
}
示例#6
0
void
generate(char *source_image_path, char *sink_image_path, char *top_text, char *bottom_text)
{

  MagickWand *wand = NULL;
  DrawingWand *drawing_wand = NULL;
  PixelWand *pixel_wand = NULL;

  MagickWandGenesis();

  wand = NewMagickWand();
  drawing_wand = NewDrawingWand();
  pixel_wand = NewPixelWand();

  // read base image
  MagickReadImage(wand, source_image_path);

  ssize_t width;
  ssize_t pointsize;
  ssize_t stroke_width;
  double scale;
  char formatted_text[100] = { '\0' };

  // Scale text
  width = MagickGetImageWidth(wand);
  scale = scaleText(top_text, formatted_text);
  pointsize = width / 5.0;
  stroke_width = pointsize / 30.0;

  // Draw top text
  PixelSetColor(pixel_wand, "white");
  DrawSetFillColor(drawing_wand, pixel_wand);
  DrawSetFont(drawing_wand, "Impact");
  DrawSetFontSize(drawing_wand, pointsize * scale);
  DrawSetFontWeight(drawing_wand, 700);
  DrawSetGravity(drawing_wand, NorthGravity);

  // Add a black outline to the text
  PixelSetColor(pixel_wand, "black");
  DrawSetStrokeWidth(drawing_wand, stroke_width * scale);
  DrawSetStrokeColor(drawing_wand, pixel_wand);

  // Turn on Anitalias
  DrawSetTextAntialias(drawing_wand, MagickTrue);

  // Draw the text
  DrawAnnotation(drawing_wand, 0, 0, (const unsigned char *)formatted_text);

  if (bottom_text) {
    char formatted_bottom_text[100] = { '\0' };

    // Scale text
    width = MagickGetImageWidth(wand);
    scale = scaleText(bottom_text, formatted_bottom_text);
    pointsize = width / 5.0;
    stroke_width = pointsize / 30.0;

    // Draw bottom text
    PixelSetColor(pixel_wand, "white");
    DrawSetFillColor(drawing_wand, pixel_wand);
    DrawSetFont(drawing_wand, "Impact");
    DrawSetFontSize(drawing_wand, pointsize * scale);
    DrawSetFontWeight(drawing_wand, 700);
    DrawSetGravity(drawing_wand, SouthGravity);

    // Add a black outline to the text
    PixelSetColor(pixel_wand, "black");
    DrawSetStrokeWidth(drawing_wand, stroke_width * scale);
    DrawSetStrokeColor(drawing_wand, pixel_wand);

    // Turn on Anitalias
    DrawSetTextAntialias(drawing_wand, MagickTrue);

    // Draw the text
    DrawAnnotation(drawing_wand, 0, 0, (const unsigned char *)formatted_bottom_text);
  }

  // Draw the image on the magick wand
  MagickDrawImage(wand, drawing_wand);

  // Write the image
  if (sink_image_path) {
    MagickWriteImage(wand, sink_image_path);
  } else {
    MagickWriteImageFile(wand, stdout);
  }

  // Clean up
  if(pixel_wand) pixel_wand = DestroyPixelWand(pixel_wand);
  if(drawing_wand) drawing_wand = DestroyDrawingWand(drawing_wand);
  if(wand) wand = DestroyMagickWand(wand);

  MagickWandTerminus();
}