PHP Imagick - setTextEncoding() function doesn't work - php

I'm trying to add some text on a Imagick object.
However I use setTextEncoding() function, it still doesn't work.
.......
$draw = new ImagickDraw();
$draw->setTextEncoding('utf-8');
$draw->setFont($fpath.'/process/ARIAL.TTF');
$draw->setFontSize(80);
$draw->setFillColor("#ffffff");
/*** annotate the text on the image ***/
$imageOrg->annotateImage($draw, 60, 100, 0, "onur küçükkeçe");
........
and as a result I get,
onur küçükkeçe
Any idea why it's not working?
Thanks in advance.
UPDATE
if I set a $text variable to something like chr(252) then I get a proper result
$text=chr(252);
$imageOrg->annotateImage($draw, 60, 100, 0, $text);
as a result I get
ü
UPDATE II
Finally I found what causing this.
The problem occurs because the charset of the document is not defined but if set a charset for the script then imagick doesn't work because the type of the document needs to be set to image/png.
But I don't know how can I fix it.

Ok. I found the solution.
php utf8_decode() function solves the problem
.......
$draw = new ImagickDraw();
$draw->setTextEncoding('utf-8');
$draw->setFont($fpath.'/process/ARIAL.TTF');
$draw->setFontSize(80);
$draw->setFillColor("#ffffff");
/*** annotate the text on the image ***/
$imageOrg->annotateImage($draw, 60, 100, 0, utf8_decode("onur küçükkeçe"));
........

Related

Encoding problem with caption in Imagemagick on php

I'm adding some heading in Russian on the image using newPseudoImage and CAPTION.
Heading can have some entities like — etc, so I clean it with html_entity_decode.
In some cases it works well, but in some cases text after decoded entity goes in wrong encoding, like that:
look at second line - there is the problem
Please, help me to find the solution.
If I use annotateImage - everything is nice.
If I use newPseudoImage and LABEL - everything is nice.
The problem appears only with newPseudoImage and CAPTION (but I have to CAPTION, cause I need to scale my headings depending on their length).
At the same time I have to use these entities in headings (i.e. to avoid wrapping of the dash to the next line).
Some headings shows nice:
'Путешествие в Карелию' - is fine, but 'Нуорунен в Карелии' - shows with error.
My code:
$postTitle = 'Нуорунен в Карелии';
$postTitle = html_entity_decode($postTitle);
$mainImage = new Imagick('source.jpg');
$title = new Imagick();
$colorize = new Imagick();
$mainImage->setImageCompressionQuality(85);
$title->setBackgroundColor('transparent');
$title->setFont('/fonts/roboto-slab_semibold.ttf');
$title->setGravity(imagick::GRAVITY_CENTER);
$title->newPseudoImage( 780, 480, "CAPTION:" . $postTitle );
$colorize->newImage(1, 1, new ImagickPixel('#f8bb00'));
$title->clutImage($colorize);
$colorize->destroy();
$mainImage->compositeImage($title, imagick::COMPOSITE_DEFAULT, 210, 110);
$mainImage->writeImage('image.jpg');

Convert image to sketch using php imagick

Trying to achieve sketch effect using php, but unable to get desired output.
Tried with
$im1->sketchimage(2, 1, -20);
but getting blur sort of image only.
Also, looked out and found
*s = Read-File-Into-Image("/path/to/image")
*g = ConvertToGrayScale(s)
*i = Invert Colors(g)
*b = ApplyGaussianBlur(i)
*result = Colour Dodge Blend Merge(b,g)
and tried
$im1->edgeImage(2);
$im1->contrastStretchImage(30, 500);
$im2 = $im1;
$im1->modulateImage(100, 20, 50);
$im1->negateImage(FALSE);
$im1->gaussianBlurImage(5, 1, FALSE);
$im1->compositeImage($im2, imagick::COMPOSITE_COLORDODGE, 0, 0 );
still not getting the desired output.
the radius should be larger than sigma. Else make it 0, so that sdk takes care. use a good quality image and format.
For reference..http://php.net/manual/en/imagick.sketchimage.php

Experiencing weird top margin when using fpdf Write()

I'm trying to write text to a PDF and there seems to be a weird margin on the top of my page.
This is my following code:
require_once('fpdf.php');
require_once('fpdi/fpdi.php');
//Start the FPDI
$pdf = new FPDI('P', 'pt');
//Set the source PDF file
$source_file = $pdf->setSourceFile("template.pdf");
//Import the first page of the file
$tppl = $pdf->importPage(1);
$pdf->AddPage();
//get size of pdf page
$size = $pdf->getTemplateSize($tppl);
$pdf->useTemplate($tppl, null, null, $size['w'], $size['h'], true);
$pdf->SetMargins(0, 0, 0);
$pdf->SetTextColor(0, 0, 0);
When I use a font-size pt 12, and write text I get this:
$pdf->SetFont('Arial', '', 12);
$pdf->SetXY(0, 0);
$pdf->Write(0, "Hi");
When I do $pdf->SetXY(0, 7.5) I get this
The above looks like I can easily add 7.5 points to the Y and be fine.
However, if I changed the font-size, the distance between the top and the text grows a little greater.
$pdf->SetFont('Arial', '', 8);
Could anyone help me figure out how to neutralize this to at least make it so if I set my XY to a number, it will put it on the some location regardless of the font-size? I've tried different pdf's and it works all the same.
EDIT:
I did $pdf->GetY() and I get 28.35
You simply define a line height of zero. Because of this the text is "centered" vertically around 0.
A common line height is:
$pdf->Write($pdf->FontSize * 1.2, "Hi");
I solved this by instead of doing Write() I used Cell().
I think the main issue was not having a solid width and height. All I know is it works perfectly now so anyone encountering the same problems should try this.
$pdf->Cell(WIDTH,HEIGHT,TEXT);
I also did the following, not sure if this helped or not but I have it in my script.
$pdf->SetMargins(0, 0);
$pdf->cMargin = 0;

PHP set the attribute for an image with matrix values

as the topic,I need to set the attribute for an image or create an image with the attribute what I get from a client as the matrix values.
I found a function named imageconvolution,but it doesn't work out.Maybe I used it incorrectly.
here is the code:
<?php
$image = imagecreatefromgif('http://www.php.net/images/php.gif');
$emboss = array(array(0, 0, 100), array(0, 0, 200), array(0, 0, 1));
imageconvolution($image, $emboss, 1, 0);
header('Content-Type: image/png');
imagepng($image, null, null);
?>
the matrix values are used to scale or rotate or move the image.Is these code right?
I hope to find out someone to teach me.
Thanks a lot.
Scaling, rotation and moving are affine transforms. You cannot use convolution for this matrixes.
I think the easiest way to use the php Imagick extension. It has an affineTransformImage function which you can use: http://php.net/manual/en/imagick.affinetransformimage.php

PHP ImagickDraw with outlined text issues

I'm learning and practicing my Imagick skills.
I have issues with outlined text using Imagick stroke. I would like to achieve an effect visible on this image: a popular Internet meme:
Here's the code I have so far:
$draw = new \ImagickDraw();
$outputImage = new \Imagick('meme.jpg');
$draw->setFillColor('#fff');
$draw->setFont('impact.ttf');
$draw->setFontSize(40);
$draw->setGravity(\Imagick::GRAVITY_NORTH);
$draw->setStrokeColor('#000');
$draw->setStrokeWidth(1);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$outputImage->annotateImage($draw, 0, 5, 0, 'Sample text');
$outputImage->setFormat('png');
$outputImage->writeimage('tmp/meme.png');
The issue: text stroke does not look nice. I've found a tip on Imagick discussion board about annotating image second time, but without stroke. Does not work.
Before writing image:
$draw->setStrokeColor('transparent');
$outputImage->annotateImage($draw, 0, 5, 0, 'Sample text');
Can anybody give me a clue?
Update
To conclude, my generated image looks as following:
As you can see, I have some issues with 2px stroke while using different font size. On big fonts, it looks nice but with smaller font there are some issues with the stroke and font.
Version1: resizing
Version 2: composite over and resizing
Version 2 gives a much better result. See code below. Depending on the final size, you need to play around with font and stroke size, as the resizing may give unwanted effects. You may also try version 2 without resizing.
Version 1: resizing
$draw = new ImagickDraw();
$draw->setFillColor('#fff');
$draw->setFont('impact.ttf');
$draw->setFontSize(100); //use a large font-size
$draw->setStrokeColor('#000');
$draw->setStrokeWidth(4);
$draw->setStrokeAntialias(true); //try with and without
$draw->setTextAntialias(true); //try with and without
$outputImage = new Imagick();
$outputImage->newImage(1400,400, "transparent"); //transparent canvas
$outputImage->annotateImage($draw, 20, 100, 0, 'STOP ME FROM MEMEING');
$outputImage->trimImage(0); //Cut off transparent border
$outputImage->resizeImage(300,0, imagick::FILTER_CATROM, 0.9, false); //resize to final size
/*
Now you can compositve over the image
*/
//Clean up
$draw->clear();
$draw->destroy();
$outputImage->clear();
$outputImage->destroy();
Version 2: composite over and resizing
$draw = new ImagickDraw();
$draw->setFont('impact.ttf');
$draw->setFontSize(100); //use a large font-size
$draw->setStrokeAntialias(true); //try with and without
$draw->setTextAntialias(true); //try with and without
//Create text
$draw->setFillColor('#fff');
$textOnly = new Imagick();
$textOnly->newImage(1400,400, "transparent"); /transparent canvas
$textOnly->annotateImage($draw, 21, 101, 0, 'STOP ME FROM MEMEING'); //parameters depend of stroke and text size
//Create stroke
$draw->setFillColor('#000'); //same as stroke color
$draw->setStrokeColor('#000');
$draw->setStrokeWidth(8);
$strokeImage = new Imagick();
$strokeImage->newImage(1400,400, "transparent");
$strokeImage->annotateImage($draw, 20, 100, 0, 'STOP ME FROM MEMEING');
//Composite text over stroke
$strokeImage->compositeImage($textOnly, imagick::COMPOSITE_OVER, 0, 0, Imagick::CHANNEL_ALPHA );
$strokeImage->trimImage(0); //cut transparent border
$strokeImage->resizeImage(300,0, imagick::FILTER_CATROM, 0.9, false); //resize to final size
/*
Now you can compositve over the image
*/
//Clean up
$draw->clear();
$draw->destroy();
$strokeImage->clear();
$strokeImage->destroy();
$textOnly->clear();
$textOnly->destroy();
Can you post your result or be more specific what doesn't look fine to you?
A solution could be to create the text (with stroke) first on a transaprent background and then composite it over the image. You could create the text in bigger font size and resize to make it look smoother on the final image.

Categories