+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Trying to center text

  1. #1
    MicrotechXP's Avatar
    MicrotechXP is offline x10 Spammer MicrotechXP is an unknown quantity at this point
    Join Date
    Feb 2005
    Location
    USA,California
    Posts
    3,822

    Trying to center text

    I am trying to center text on an image and the problem is the text doesn't center.

    Here is the code:
    Code:
    <?php
    header("Content-type: image/png");
    //$mysql = mysql_connect("localhost","user","pass");
    //$db = mysql_select_db("migt");
    //$user = mysql_real_escape_string($_GET['uid']);
    //$query = "SELECT * FROM migt WHERE uid='" . $user . "'";
    //$query = mysql_query($query);
    //$query = @mysql_fetch_array($query);
    // THIS IS TEMPORARY:
    $query['display'] = "tech star";
    function hex2string($image, $hex){
    	$hex = str_split($hex,"2");
    	$hex[0]=hexdec($hex[0]);
    	$hex[1]=hexdec($hex[1]);
    	$hex[2]=hexdec($hex[2]);
    	$color=imagecolorallocate($image, $hex[0], $hex[1], $hex[2]);
    	return $color;
    }
    function write_text($image, $txtsize, $x, $y, $string, $hex, $font){
    	$color = hex2string($image, $hex);
    	imagettftext($image,$txtsize,0,$x,$y,$color,$font,$string);
    }
    function addimage($new, $image, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h){
    $new = imagecreatefrompng($new);
    imagecopy($image,$new,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h);
    imagedestroy($new);
    }
    function center($string,$start,$px){
    	$ssize = imagettfbbox($px, "0", "calibri.ttf", $string);
    	$dsize = $start - $ssize[4];
    	$dsize = $dsize / 2;
    	return $dsize;
    }
    $image = imagecreatefrompng("gt1.png");
    addimage("apache_pb.png",$image,2,3,1,1,76,71);
    write_text($image, 16,center($query['display'], 86, 225, 16),45,$query['display'],"FFFFFF","calibri.ttf");
    imagepng($image);
    imagedestroy($image);
    ?>
    //
    //
    Is there any way I can fix this?
    Last edited by MicrotechXP; 06-27-2007 at 07:11 PM.

  2. #2
    Chris Z's Avatar
    Chris Z is offline x10 Spammer Chris Z is an unknown quantity at this point
    Join Date
    Sep 2005
    Location
    Alabama, USA
    Posts
    2,802

    Re: Trying to center text

    I don't think that trying the center tag would work in this case. You'll probably have to manually center it by adjusting the x,y positions
    -Chris Z
    Retired Account Manager


  3. #3
    MicrotechXP's Avatar
    MicrotechXP is offline x10 Spammer MicrotechXP is an unknown quantity at this point
    Join Date
    Feb 2005
    Location
    USA,California
    Posts
    3,822

    Re: Trying to center text

    I know it won't work, but I want an automatic function to do it for me.

  4. #4
    Brandon's Avatar
    Brandon is offline Former Senior Account Rep Brandon is on a distinguished road
    Join Date
    Jun 2006
    Location
    Tewksbury, MA
    Posts
    9,589

    Re: Trying to center text

    Quote Originally Posted by MicrotechXP View Post
    I know it won't work, but I want an automatic function to do it for me.
    Fine tune that function I made, it'll work just show it who is the boss of PHP.
    Thanks,
    Brandon Long

  5. #5
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: Trying to center text

    I did not test this for errors much other than parser errors, so things may mess up if/when the text's width/height is greater than the width/height of the image itself.

    PHP Code:
    <?php

       header
    ("Content-type: image/png");

       function 
    writeText($image$fontSize$textString$color$font$centerText false$fontXPos false$fontYPos false) {
          if (
    $centerText == true) {
             
    $xyCoords centerText($image$fontSize$font$textString);
             
    $fontXPos $xyCoords['x'];
             
    $fontYPos $xyCoords['y'];
          }
          
    imagettftext($image$fontSize0$fontXPos$fontYPos $color$font$textString);
       }

       function 
    centerText($image$fontSize$font$textString) {
          
    $tmpVals imagettfbbox($fontSize0$font$textString);
          
    $textWidth abs($tmpVals[4] - $tmpVals[0]);
          
    $textHeight abs($tmpVals[5] - $tmpVals[1]);
          
    $textPos = array();
          
    $textPos['x'] = (((.5 imagesx($image))) - ($textWidth 2));
          
    $textPos['y'] = (((.5 imagesy($image))) - ($textHeight 2));
          return 
    $textPos;
       }

       
    $image imagecreatefrompng('image.png');

       
    $font '';
       
    $color imagecolorallocate($image0x000x000x00);
       
    $textString 'yo dawg';
       
    $fontSize = (int) 14;

       
    writeText($image$fontSize$textString$color$fonttrue);

       
    imagepng($image);
       
    imagedestroy($image);

    ?>
    Edit: Also, I forgot to mention that this won't do anything with anngglless or rotation with the text.

    Edit 2: I didn't really see a need for the invalid x,y coordinate check, so I just removed it.
    Last edited by Bryon; 06-28-2007 at 04:42 PM.

  6. #6
    mgw854 is offline x10Hosting Member mgw854 is an unknown quantity at this point
    Join Date
    Aug 2006
    Posts
    36

    Re: Trying to center text

    OK, I inserted your centering function into the script, and now it complains because it "can't modify headers". Any reasons this might have happened? Even when I comment out your script, it provides the error.

    EDIT:
    Hmm... I found the problem. SharePoint Designer must encode the files in some certain way that they contain special headers. However, the text is still unoriented. It is actually centered on the image, not on it's own little box.
    Last edited by mgw854; 06-28-2007 at 08:10 AM.

  7. #7
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: Trying to center text

    I'm not sure what you're asking, mgw. (It's own little box?)

    Do you have an example of what you mean?
    Last edited by Bryon; 06-28-2007 at 03:56 PM.

  8. #8
    Brandon's Avatar
    Brandon is offline Former Senior Account Rep Brandon is on a distinguished road
    Join Date
    Jun 2006
    Location
    Tewksbury, MA
    Posts
    9,589

    Re: Trying to center text

    The headers could be from it outputing png, but creating gif?

    PHP Code:
    header("Content-type: image/png"); 
    PHP Code:
    $image imagecreatefrompng('image.png'); 
    but then

    PHP Code:
    imagegif($image); 
    Last edited by Brandon; 06-28-2007 at 04:28 PM.
    Thanks,
    Brandon Long

  9. #9
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: Trying to center text

    Whoops

  10. #10
    mgw854 is offline x10Hosting Member mgw854 is an unknown quantity at this point
    Join Date
    Aug 2006
    Posts
    36

    Re: Trying to center text

    No, I caught that png/gif slip-up, so that wasn't the problem. It was, indeed, MS SharePoint. As for the box, here is the example, where the text needs to be in those boxes:
    http://i7.photobucket.com/albums/y27...TechXP/gt1.png
    Last edited by MicrotechXP; 06-28-2007 at 11:21 PM.

+ Reply to Thread
Page 1 of 3 123 LastLast

Similar Threads

  1. Hybrid's HTML Lessons
    By Hybrid in forum Tutorials
    Replies: 18
    Last Post: 11-28-2009, 02:12 PM
  2. Intellitxt VS Text Link
    By Corey in forum News and Announcements
    Replies: 19
    Last Post: 10-05-2006, 11:06 AM
  3. 500pts for Glowing Neon Text!
    By Josh in forum The Marketplace
    Replies: 12
    Last Post: 07-17-2006, 09:02 PM
  4. Replies: 0
    Last Post: 09-21-2005, 03:49 AM
  5. |PS|*Blur text Tutorial*
    By |Born2Shoot| in forum Tutorials
    Replies: 7
    Last Post: 06-04-2005, 10:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers