Hi guys -

I found this code..

</center>

but no luck.

It's for showing the stats on my shoutcast station - here's the code

$px = (imagesx($im)-5*strlen($string2))/2;
ImageString($im,4,$px,25,$string2,$text_color2);

i want to get the string centered

Any help really would be appreciated
🆒

    Fatal error: Call to undefined function: imagesx() in /root/public_html/testing.php on line 4

      How about using CSS? Just wrap your code in a p.../p tag.

      p.centered {
      text-align: center;
      }

        p.centered {
        $px = (imagesx($im)-5*strlen($string2))/2;
        ImageString($im,4,$px,25,$string2,$text_color2);
        text-align: center;
        }

        That sound about right?? 🙁

          As you are using font 4, the width of each character is given by

          $w = imagefontwidth(4);

          So

          $textwidth = $w * strlen($string2);
          $imagewidth = imagesx($im);

          So to centre horizontally,

          $px = ($imagewidth - $textwidth) / 2;

          ImageString($im,4,$px,25,$string2,$text_color2);

          hth

            just wrap the code you output with

            <p align="center"><center> ...code... </center></p>

            that will align it in the center. works for both images and text.

            Harry

              DONT use ANY html tags in your image code!
              if you are writing a php-image its the wrong place for html.

              of course you can only align the whole image in the page that contains the <img src=""> tag.

              so use barands version or imageTTfbBox() when using TTF fonts.

                i didnt think the align method would work - since it's php.

                Barands version was abit sketchy - i copied his code, and implemented it.

                Uploaded and ran the code but it was still NOT centered...

                This is the the entire code for the strings -

                // positioning below
                $px = (imagesx($im)-5strlen($string1))/2;
                ImageString($im,5,$px,10,$string1,$text_color1);
                // (image,font (from 1 to 5),starting x position, y position,line of text,color)
                $px = (imagesx($im)-5
                strlen($string2))/2;
                ImageString($im,4,$px,25,$string2,$text_color2);
                $px = (imagesx($im)-5strlen($string3))/2;
                ImageString($im,4,$px,40,$string3,$text_color2);
                $px = (imagesx($im)-5
                strlen($string4))/2;
                ImageString($im,2,$px,55,$string4,$text_color1);

                it isnt centered...so how do you think - using barands method it could be done (i've tried substitution)

                  // 1st font with font index 5
                  $w1 = imagefontwidt(5);
                  $px = (imagesx($im) - $w1*strlen($string1))/2; 
                  ImageString($im, 5, $px, 10, $string1, $text_color1); 
                  
                  // 2nd font with index 4
                  $w2 = imagefontwidth(4);
                  $px = (imagesx($im) - $w2*strlen($string2))/2; 
                  ImageString($im, 4, $px, 25, $string2, $text_color2); 
                  
                  // 3rd font has index 4 too
                  $w3 = $w2;
                  $px = (imagesx($im) - $w3*strlen($string3))/2; 
                  ImageString($im, 4, $px, 40, $string3, $text_color2); 
                  
                  // 4th with font index 2
                  $w4 = imagefontwidth(2);
                  $px = (imagesx($im) - $w4*strlen($string4))/2; 
                  ImageString($im, 2, $px, 55, $string4, $text_color1); 
                  

                  that should do it...

                    Sir - i love you!

                    Thanks very much for taking the time to reply to this post.

                    You've helped so much 🙂 Thanks again from the UK 🙂

                    Hooje.

                      Write a Reply...