Using Pango in PHP Imagick API - php

I want to use Imagick API for PHP instead of runnung command line in my PHP code.
exec("convert -gravity Center -size 200x200 -fill black -font Arial -pointsize 20 pango:\"Hello World!\" output.png");
for above command I have following Imagick API code:
<?php
$img = new Imagick();
$draw = new ImagickDraw();
$draw->setFont("Arial");
$draw->setFontSize(20);
$draw->setGravity( Imagick::GRAVITY_CENTER );
$img->newImage( 200, 200, "black", "png" );
//Pango code for Hello World!
$img->writeImage("output.png");
?>
But I could not find equal method/option for Pango. Do you know how can use Pango in Imagick API?

Just improving Aite's answer by adding background color.
$img = new Imagick();
$img->setBackgroundColor(new ImagickPixel('black'));
$img->setFont("Arial");
$img->setPointSize(20);
$img->setGravity( Imagick::GRAVITY_CENTER );
$img->setImageFormat('jpg');
//Pango code for Hello World!
$img->newPseudoImage(200, 200, "pango:Hello World");
$img->writeImage("output.png");

The part pango:\"Hello World!\" is 'the name of a file' in ImageMagick. This means you should call readImage to 'read' the pango image:
$img->readImage("pango:\"Hello World!\"");
This will only work if ImageMagick is compiled with support for pango.

First of all, you don't need the ImagickDraw object, you only need to set your options to the Imagick object then to create/call new pseudo image (with pango: prefix) instead of regular new image method:
$img = new Imagick();
$img->setFont("Arial");
$img->setPointSize(20);
$img->setGravity( Imagick::GRAVITY_CENTER );
$img->setImageFormat('jpg');
//Pango code for Hello World!
$img->newPseudoImage(200, 200, "pango:Hello World");
$img->writeImage("output.png");
sorry that I couldn't find out how to set the fill color, but this is another issue you may know how to solve it.
P.S. You need to make sure you have pango installed, and imagemagick library is built with pango support (which is done for you since you are able to use command line).

Related

Imagemagick remove the upper content while converting pdf to image

My pdf first page looks like :
When I run below command:
exec("convert -density 300 $pdf_path $temp_images 2>&1",$output);
It converts its page in image that looks like:
this thing happen only when the dimensions of pdf are width- 595 and height- 842.
Any suggestion will be appreciated.
Looks like the CropBox of the PDF is being used instead of the media size, or possibly ImageMagick is sending a fixed (incorrect, Letter) media size to Ghostscript in order to render the page.
Unfortunately that's about all I know regarding ImageMagick, you need someone who can tell you how to find and alter the Ghostscript invocation.
This code solve my problem but I want this in command line
$im = new Imagick();
$im->readImage( $pdf_path );
$im->setImageFormat( "jpg" );
$im->writeImage( $temp_images );
echo 'Image Converted';

How to create canvas effect with Imagick?

I am trying to replicate the canvas texture style effect on a photo using the Imagick php class as seen here under the heading 'texture':
http://www.imagemagick.org/Usage/photos/
Using the command line it can be achieved like this:
composite texture_fabric.gif pagoda_sm.jpg \
-tile -compose Hardlight photo_texture.jpg
However I am using the 'Imagick' php class for ImageMagick and I can't seem to work out how to tile the texture.
I have tried:
$im = new Imagick("source.jpg")
$img2 = new Imagick("texture.gif");
$im->compositeImage($img2, imagick::COMPOSITE_HARDLIGHT, 0, 0);
But all I end up with is the texture image composited on the the source image in the top left corner.
How can I recreate the effect in the link?
Looking at the imagick php there is http://php.net/manual/en/imagick.textureimage.php
You may not be able to do what you want with Imagick as it has limited options.
A bit of lateral thinking:
Make a canvas the size of the image and tile the texture across it then composite it?
You could do it with ( untested ):
exec("composite texture_fabric.gif pagoda_sm.jpg -tile -compose Hardlight photo_texture.jpg");

Imagick: Failed to query the font metrics

I'm working on (what should be) a simple script that will add text to an image. After going over my script several times looking for any mistakes I finally decided to try running a sample from php.net and I encountered the same, nondescript, error: "Failed to query the font metrics". Here's the code:
/* Text to write */
$text = "Hello World!";
/* Create Imagick objects */
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('#000000');
$background = new ImagickPixel('none'); // Transparent
/* Font properties */
$draw->setFont('Arial');
$draw->setFontSize(50);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
/* Get font metrics */
$metrics = $image->queryFontMetrics($draw, $text);
/* Create text */
$draw->annotation(0, $metrics['ascender'], $text);
/* Create image */
$image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
$image->setImageFormat('png');
$image->drawImage($draw);
/* Save image */
file_put_contents('/tmp/file.png', $image);
I can not for the life of me find any information via google about this error. Nor can I find adequate documentation on this method or potential causes of failure. Basically, I'm stumped. If anyone could provide insight or a fix it would be greatly appreciated.
ImageMagic version: ImageMagick 6.6.5-10 2011-04-06 Q16
Imagick module version: 3.1.0b1
have you tried cli version ? is imagemagick installed on server ? if yes
then run a command like
system('convert -background lightblue -fill blue \
-font Candice -pointsize 72 label:Anthony \
label.gif ');
see if you have imagenamed label.gif in server after running script.
for your reference http://www.imagemagick.org/Usage/text/
For those having similar issues, the PHP Imagick exceptions aren't always the most descriptive. I could have saved myself a lot of time examining the output from the Image Magic application installed on the server first. Just something to keep in mind. To view a list of the currently installed delegates(modules) use the command convert -list configure and examine the line that starts with "DELEGATES" from the output. If you encounter the same error, I recommend checking here first. I found I was missing the freetype and ghostscript delegates. After installing the dependancies and a quick recompile of ImageMagick everything works like a charm.

Convert PNG to JPG and set transparent background to white with ImageMagick and PHP

How can I use ImageMagick (with the php extension) to set the transparent background to white when converting an image from PNG to JPEG?
At time of writing, you have not specified which extension you are using, but if you were using the commandline, the command would be:
convert image.png -background white -flatten -alpha off image.jpg
More information can be found on the Masking Usage documentation.
Using IMagick for instance, I think you could do this as follows:
(totally untested, never used IMagick and don't have it installed to test)
$image = new IMagick('image.png');
$flattened = new IMagick();
$flattened->newImage($image->getImageWidth(), $image->getImageHeight(), new ImagickPixel("white"));
$flattened->compositeImage($image, imagick::COMPOSITE_OVER, 0, 0);
$flattened->setImageFormat("jpg");
$flattened->writeImage('image.jpg');
$image->clear();
$image->destroy();
$flattened->clear();
$flattened->destroy();
If you are using the Imagick extension:
<?php
// load the source transparent png
$i = new IMagick('image.png');
// set the background to white
// you can also use 'rgb(255,255,255)' in place of 'white'
$i->setImageBackgroundColor(new ImagickPixel('white'));
// flattens multiple layers
$i = $i->flattenImages();
// the output format
$i->setImageFormat('jpg');
// save to disk
$i->writeImage('image.jpg');
// and/or output directly
// header('Content-Type: '.$i->getFormat());
// echo $i->getImageBlob();
// cleanup
$i->clear();
$i->destroy();

How to create watermark with imagemagick

I'm trying to create a watermark with ImageMagick however, the guides on layering are pretty daunting. Basically I'll have one base image say "base.jgp" and an overlay image say "overlay.jpg". Overlay.jpg would be smaller than base.jpg. Which exec command would I run to place overlay centered on top of base.jpg?
Thanks!
shell_exec("composite -gravity center ./images/watermark_horizontal.png {$this->path} {$this->path}");
Here we go
Check out ImageMagick examples, especially the Compositing Images chapter. It has a number of ready-made real-world examples.
$image = new Imagick();
$image->readImage("image.jpg");
// Open the watermark
$watermark = new Imagick();
$watermark->readImage("watermark.png");
// Overlay the watermark on the original image
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 0, 0);
// send the result to the browser
header("Content-Type: image/" . $image->getImageFormat());

Categories