Kylo Ren Kylo Ren - 1 year ago 83
PHP Question

PHP string concatenation as image watermark

I have a function that successfully inserts text into an image, based on the font size, font color, text coordinates, the text string itself, and the background image. The function is working perfectly. However, I have an option to concatenate the username with some sample text, so that the string can be generated in the image. This happens in two different ways:

1.Username + Text -> this is the text to be generated in the image.

2.Text + Username -> this is the text to be generated in the image.

This is code sample for the options:

//option 1
$textValue = $username . ' ' . $textValue;

//option 2
$textValue = $textValue . ' ' . $username;

So this should be working perfectly as PHP concatenates the strings in this way (Right?)
See the results in the image below:

So you see in the first line the username is before the text and the result is perfectly fine, but in the second line the text it is messed up.
I am using PHP GD to write the text into the image. And it has nothing to do with the cyrillic characters. I have tested with cyrillic characters and the problem is the same. I have removed the actual username for the test image and replaced it with the string 'username' just to get the idea.

Answer Source

Next time please put the code in the comment image in the question. It is also much easier to mark code out and for me to copy anything.

In the code you show ( you call ->textBox() with both heigth, width and fontSize specified.

If you leave out the height, the code will just wrap the string for you over multiple lines, so that is one solution.

At the moment the fontSize is reduced in size by the ->fitToWidth() function until it should fit. Either the problem is that this function works incorrectly because it does not work well with Unicode characters or because the font itself cannot be drawn at the size calculated and this is written larger.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download