Dear all; i have a problem on calculation i made page to write a text on image and want to determine the position of the text 1.top-center the image 2.center the image 3.bottom-center the image this is the first problem the second when i write long text ,it disappear i want to show over the image in multi lines here is the code $im = imagecreatefromjpeg($image); $width = imagesx($im); $height = imagesy($im); //i try to write text on the center $box = @imageTTFBbox($fontsize,$fontangle,$font,$text); $x = $box[0] + ($width / 2) - ($box[4] / 2); $y = $box[1] + ($height / 2) - ($box[5] / 2); imagettftext($im, $fontsize, 0, $x,$y, $color, $font, $text); PHP: please help me on this problem urgent because i'm silly on calculation and i will go mad
About the text, you will need to find out how long a character is in pixels that you print and then calculate from there to insert linebreaks. Then the positioning; 1.top-center the image = ($x,0) 2.center the image = ($x,$y) 3.bottom-center the image ($x, $height) Of course you will need to adjust with the text height (either substract or add depending where).
Hmm, let me try adjust your code quick. Then you will get the idea. $im = imagecreatefromjpeg($image); $width = imagesx($im); $height = imagesy($im); //i try to write text on the center $box = @imageTTFBbox($fontsize,$fontangle,$font,$text); $box_width = $box[4]-$box[0]; $box_height = $box[1]-$box[4]; // Top-center //$x = ($width / 2) - ($box_width / 2); //$y = 0; // Center/center $x = ($width / 2) - ($box_width / 2); $y = ($height / 2) - ($box_height / 2); // Bottom-center //$x = ($width / 2) - ($box_width / 2); //$y = $height - $box_height; imagettftext($im, $fontsize, 0, $x,$y, $color, $font, $text); Code (markup): Try work out with this, I haven't tested it, but the calculations of placement should be about right. Maybe for the top-center it works better if you in use $y = $box[5]; Not sure which one is better. This is done quick and dirty so if you get some problems with still displaying right try adjust the values a bit, however you should be on the way now. I hope this can help you.
i'm sorry for that but it give me wrong place 1-top-center didnot appear 2-center center appear on the botton of the image 3-botton-center didnot appear i think because this code $box_width = $box[4]-$box[0]; $box_height = $box[1]-$box[4]; PHP: may be we must change it on the top and botton i still have a problem
Dear dreamconception, i change the code $box_width = $box[4]-$box[0]; $box_height = $box[1]-$box[4]; PHP: with $box_width = $box[4]-$box[0]; $box_height = $box[1]-$box[5]; PHP: and the top and center and botton appear on right place i have only the problem of how to write the text on multi lines?????
yes, is a typo, good you found the fix For the multi-lines, first you need to count number of characters that can be shown. I'm not sure if you use different font sizes, it should be possible to calculate the pixels width of the font size somehow but I have no idea. However the code would be something along the line $max_chars = 10; $text = wordwrap($text, $max_chars, "\n"); Code (markup): Where max_chars is number of characters to be shown on each line. \n is the line break code.
thank you dreamconception; i run this code but it take ervery word put on line on align center and the lines appear on left side not on center and on the align top and botton didnot appear i want to count the words and not exceed the image then enter to new line
Here is a big assumption how to do it, as I do not know how to know the width in pixels of the characters; $max_chars = intval($width/$fontsize); $text = wordwrap($text, $max_chars, "\n"); $box = @imageTTFBbox($fontsize,$fontangle,$font,$text); Code (markup): Regarding the center of the text I looked up and found this in comments; php.net/manual/en/function.imagettfbbox.php I hope this could help you.
I'm sorry that code doesn't help I realize, is only what we have already done. I think you will need to loop the function and cut the text into pieces instead.
mirosoft1 If you still haven't sold your problem, feel free to contact our PHP programmers by following the signature below
Sorry for the late reply; Try work with this; You have to get the width of the character, then divide the image width with that, it should do the trick. I'm unsure if $fontsize is enough, but try it out.