I am creating a dynamic Image on the base of User Input where I have 2 textboxes and 1 Image upload in form,
<?php /* Create some objects */ $customImage = new Imagick('test.png');//dynamic image $image = new Imagick(); $draw = new ImagickDraw(); $pixel = new ImagickPixel( '#FFF' ); /* New image */ $image->newImage(800, 75, $pixel); /* Black text */ $draw->setFillColor('#a28430'); /* Font properties */ $draw->setFont('Roboto-Black.ttf'); $draw->setFontSize( 30 ); /* Create text */ $image->compositeImage($customImage,Imagick::COMPOSITE_DEFAULT, 10, 20); $image->annotateImage($draw, 10, 50, 0, "First Line");// dynaimc text $image->annotateImage($draw, 10, 70, 0, "second line");// dynamic text /* Give image a format */ $image->setImageFormat('png'); /* Output the image with headers */ header('Content-type: image/png'); echo $image;
Current Image output of is
My sample uploaded Image
I already have pre-define font so I am using at as Roboto-Black.ttf
and font-size , here I am $customImage
getting as dynamic Image(where user uploaded) the Image and First Line
and Second Line
I am getting dynamic as well as test.png
is also dynamic ,
So, when I create the Image with I am defining the Image size $image->newImage(800, 75, $pixel);
sometimes Image size is too big when test.png
is small so after creating an Image is there any way to create the result Image as “auto” resize so it will auto adjust with the dynamic font and dynamic Image?
There is [resizeImage][3]
from the but as a parameter it is expecting the height and width and I want as “auto”
Advertisement
Answer
Instead of trying to dynamically size things, it is probably easier to just pick a fixed to work with and scale what the user provides to match that.
For the upload it would look something like this:
define('IDEAL_WIDTH', 800); define('IDEAL_HEIGHT', 800); $customImage = new Imagick('test.png');//dynamic image $customImage->scaleImage(IDEAL_WIDTH, IDEAL_HEIGHT);
And for your image where you are drawing text it would be:
$image->newImage(IDEAL_WIDTH, IDEAL_HEIGHT, $pixel);
You’ll need to adjust where you are drawing your text slightly, but that should be really easy because you are working in a fixed area.
Other versions of this are to get a little crazier and ask the user for certain things like where they want the text and what font sizes to use.