1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

someone help me on this php code

Discussion in 'PHP' started by mirosoft1, Sep 17, 2010.

  1. #1
    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 :rolleyes:
     
    mirosoft1, Sep 17, 2010 IP
  2. dreamconception

    dreamconception Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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).
     
    dreamconception, Sep 18, 2010 IP
  3. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    thank you for your reply, but can you give me code!!!
     
    mirosoft1, Sep 19, 2010 IP
  4. dreamconception

    dreamconception Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    dreamconception, Sep 19, 2010 IP
  5. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    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
     
    mirosoft1, Sep 19, 2010 IP
  6. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    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?????
     
    mirosoft1, Sep 19, 2010 IP
  7. dreamconception

    dreamconception Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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.
     
    dreamconception, Sep 19, 2010 IP
  8. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #8
    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
     
    mirosoft1, Sep 20, 2010 IP
  9. dreamconception

    dreamconception Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    dreamconception, Sep 20, 2010 IP
  10. dreamconception

    dreamconception Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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.
     
    dreamconception, Sep 20, 2010 IP
  11. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #11
    yes i want to check the text to not exceed the image width then enter to new line
     
    mirosoft1, Sep 21, 2010 IP
  12. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #12
    where are you dreamconception?????
     
    mirosoft1, Sep 22, 2010 IP
  13. ChPeter

    ChPeter Peon

    Messages:
    974
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #13
    mirosoft1

    If you still haven't sold your problem, feel free to
    contact our PHP programmers by following the signature below
     
    ChPeter, Sep 27, 2010 IP
  14. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #14
    yes i still have the problem
    where is this signature??
     
    mirosoft1, Sep 28, 2010 IP
  15. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #15
    up up up up up
     
    mirosoft1, Oct 4, 2010 IP
  16. dreamconception

    dreamconception Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #16
    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.
     
    dreamconception, Oct 5, 2010 IP