With some research, I have created the following piece of code to generate a simple image with a text string right on the center showing the resolution of this image. The challenge is, when you change X & Y dimensions to larger or smaller values, the font size remains the same.
My question is, what would be a good way to “grow” or “shrink” the font size as the user chooses between image sizes?
One idea I had would be to calculate predetermined areas, add them to an array of key value pairs where the value is the hard-coded font size for a given range of areas.
Perhaps you have a simpler idea?
PS: The font I am using can be downloaded from Google fonts here:
The output looks like this:
And the code:
<?php // We Declare our canvas width & height dimensions $myCanvasWidth = 256; $myCanvasHeight = 256; // We Create an empty and dark canvas from these dimensions above $myCanvas = imagecreate($myCanvasWidth, $myCanvasHeight) or die("Can't create image!"); // We Allocate a color to be used as the canvas background $colorIndigo = imagecolorallocate($myCanvas, 0x3F, 0x51, 0xB5); // We Apply color as canvas background imagefill($myCanvas, 0, 0, $colorIndigo); // We Allocate a color to be used with the canvas text $colorWhite = imagecolorallocate($myCanvas, 0xFF, 0xFF, 0xFF); // We Declare our TTF font path in Windows 10... $myFont = 'J:Montserrat-SemiBold.ttf'; // We set the font size $myFontSize = 16; // We set the text angle $myTextAngle = 0; // We Declare the text string to be drawn on canvas... $myText = $myCanvasWidth . ' x ' . $myCanvasHeight; // We Calculate and return the bounding box in pixels for the text string to be drawn on canvas... $myTextBoundingBox = imageftbbox($myFontSize, $myTextAngle, $myFont, $myText); // Get the text upper, lower, left and right corner bounds of our text bounding box... $lower_left_x = $myTextBoundingBox[0]; $lower_left_y = $myTextBoundingBox[1]; $lower_right_x = $myTextBoundingBox[2]; $lower_right_y = $myTextBoundingBox[3]; $upper_right_x = $myTextBoundingBox[4]; $upper_right_y = $myTextBoundingBox[5]; $upper_left_x = $myTextBoundingBox[6]; $upper_left_y = $myTextBoundingBox[7]; // Get Text Width and Height $myTextWidth = $lower_right_x - $lower_left_x; //or $upper_right_x - $upper_left_x $myTextHeight = $lower_right_y - $upper_right_y; //or $lower_left_y - $upper_left_y //Get the starting position for centering $start_x_offset = ($myCanvasWidth - $myTextWidth) / 2; $start_y_offset = (($myCanvasHeight - $myTextHeight) + $myFontSize * 2) / 2; // Write text to the image using TrueType fonts imagettftext($myCanvas, $myFontSize, $myTextAngle, $start_x_offset, $start_y_offset, $colorWhite, $myFont, $myText); // Draw a horizontal dashed line for reference only imagedashedline($myCanvas, 0, $myCanvasHeight/2, $myCanvasWidth, $myCanvasHeight/2, $colorWhite); // Draw a vertical dashed line for reference only imagedashedline($myCanvas, $myCanvasWidth/2, 0, $myCanvasWidth/2, $myCanvasHeight, $colorWhite); // We se the correct http header for png images... header('Content-Type: image/png'); // We Output a PNG image to either the browser or a file imagepng($myCanvas); // Free any memory associated with myCanvas; image. imagedestroy($myCanvas); ?>
Advertisement
Answer
Here is the comment above implemented as an answer. Interestingly enough, even though the lines and the font don’t appear to align, they are indeed actually centered.
The “x” at the center creates the illusion of nonalignment; a possible side effect of NOT using mono-spaced fonts in order to achieve a more unnecessarily precise alignment..?!
Then again.. mono-spaced fonts are not that good looking…
Sample resize outputs:
<?php // We Declare our canvas width & height dimensions $myCanvasWidth = 640; $myCanvasHeight = 360; // We Create an empty and dark canvas from these dimensions above $myCanvas = imagecreate($myCanvasWidth, $myCanvasHeight) or die("Can't create image!"); // We Allocate a color to be used as the canvas background $colorIndigo = imagecolorallocate($myCanvas, 0x3F, 0x51, 0xB5); // We Apply color as canvas background imagefill($myCanvas, 0, 0, $colorIndigo); // We Allocate a color to be used with the canvas text $colorWhite = imagecolorallocate($myCanvas, 0xFF, 0xFF, 0xFF); // We Declare our TTF font path in Windows 10... $myFont = 'J:Montserrat-SemiBold.ttf'; // Static font seed value... $fontSize = 16; // We set the dynamic font size $myFontSize = $myCanvasWidth / $fontSize; // We set the text angle $myTextAngle = 0; // We Declare the text string to be drawn on canvas... $myText = $myCanvasWidth . ' x ' . $myCanvasHeight; // We Calculate and return the bounding box in pixels for the text string to be drawn on canvas... $myTextBoundingBox = imageftbbox($myFontSize, $myTextAngle, $myFont, $myText); // Get the text upper, lower, left and right corner bounds of our text bounding box... $lower_left_x = $myTextBoundingBox[0]; $lower_left_y = $myTextBoundingBox[1]; $lower_right_x = $myTextBoundingBox[2]; $lower_right_y = $myTextBoundingBox[3]; $upper_right_x = $myTextBoundingBox[4]; $upper_right_y = $myTextBoundingBox[5]; $upper_left_x = $myTextBoundingBox[6]; $upper_left_y = $myTextBoundingBox[7]; // Get Text Width and Height $myTextWidth = $lower_right_x - $lower_left_x; //or $upper_right_x - $upper_left_x $myTextHeight = $lower_right_y - $upper_right_y; //or $lower_left_y - $upper_left_y //Get the starting position for centering $start_x_offset = ($myCanvasWidth - $myTextWidth) / 2; $start_y_offset = (($myCanvasHeight - $myTextHeight) + $myFontSize * 2) / 2; // Write text to the image using TrueType fonts imagettftext($myCanvas, $myFontSize, $myTextAngle, $start_x_offset, $start_y_offset, $colorWhite, $myFont, $myText); // Draw a horizontal dashed line for reference only imagedashedline($myCanvas, 0, $myCanvasHeight/2, $myCanvasWidth, $myCanvasHeight/2, $colorWhite); // Draw a vertical dashed line for reference only imagedashedline($myCanvas, $myCanvasWidth/2, 0, $myCanvasWidth/2, $myCanvasHeight, $colorWhite); // We set the correct http header for png images... header('Content-Type: image/png'); // We Output a PNG image to either the browser or a file imagepng($myCanvas); // Finally, we free any memory associated with myCanvas; the image. imagedestroy($myCanvas); ?>
The imageftbbox() function illustrated.
The imageftbbox()
returns an array with 8 elements representing four points making the bounding box of the text: