Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14

Thread: GD help

  1. #1
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post

    GD help

    I'm trying to make the following image


    I have some code but I need help with the text and the placing the text and the image of the icon on in the right spot.

    PHP Code:
    <?php 
    $town 
    'BoothabyHarbor';
    $temp '90';

    // Generate Image & set width
    $image imagecreatefrompng("../images/weather/wBack.png"); 
    $imageWidth '230';

    // Generate Town
    $color imagecolorallocate($image255255255); 
    $font "fonts/hyo.ttf"
    $fontSize "18";
    $ret imagettfbbox($fontSize0$font$town);
    imagettftext($image$fontSize0, ($imageWidth $ret[2]) / 212$color$font$month);

    // Generate Temperature
    $color imagecolorallocate($image255255255); 
    $font "fonts/arial.ttf"
    $fontSize "14";
    $ret imagettfbbox($fontSize0$font$temp);
    imagettftext($image$fontSize0, ($imageWidth $ret[2]) / 238$color$font$day);

    // Generate Weather Icon
    $image2 imagecreatefrompng("../images/weather/wx/40.png");
    //This where I'm not sure about
    //My text isn't showing up thier

    // Output & Free Memory
    header("Content-type: image/png");
    imagepng($image); 
    imagedestroy($image); 
    ?>
    Help please... i have been googling all day trying to figure this out and can't find a answer. any ideas.

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    got an online example?

  • #3
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post

  • #4
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    any ideas or help?

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    any ideas or help?
    Get one piece of it working, then go on to the next piece. Get the first piece of text to work, then the second, then go on to figure out how to add the weather icon at the coordinates that you want it.

    If you have not done so, check your web server log for errors. There will be at least two notice messages about undefined variables. You are referencing $month and $day variables, but these do not exist, nor do their names make sense in the context of what you are doing. So, I also recommend that you review your code to make sure that the logic is doing what you intend and that variables are being used as you intend.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    Another issue I see is your background image is only 196 pixels wide, but you have a variable in the code that is set to 230.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    ok heres a Updated version of my code with working text positioned correctly.
    PHP Code:
    <?php 
    $town 
    'Boothaby Harbor';
    $temp '90';

    // Generate Image & set width
    $image imagecreatefrompng("../images/weather/wBack.png"); 
    //$image2 = imagecreatefrompng("../images/weather/wx/40.png");
    $imageWidth '230';
    //echo $imageWidth;

    // Generate Town
    $color imagecolorallocate($image255255255); 
    $font "fonts/hyo.ttf"
    $fontSize "15";
    $ret imagettfbbox($fontSize0$font$town);
    imagettftext($image$fontSize0, ($imageWidth $ret[2]) / 1240$color$font$town);

    // Generate Temperature
    $color imagecolorallocate($image255255255); 
    $font "fonts/arial.ttf"
    $fontSize "14";
    $ret imagettfbbox($fontSize0$font$temp);
    imagettftext($image$fontSize0, ($imageWidth $ret[2]) / 260$color$font$temp);

    // Output & Free Memory
    header("Content-type: image/png");
    imagepng($image); 
    imagedestroy($image); 
    ?>
    you can see it working at http://pornandfriedchicken.com:8080/includes/wx.php how do I hand putting the second image on the top of that image?

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #9
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    Updated code but its cutting the other image off;
    PHP Code:
    <?php 
    $town 
    'Boothaby Harbor';
    $temp '90';

    // Generate Image & set width
    $image imagecreatefrompng("../images/weather/wBack.png"); 
    $image2 imagecreatefrompng("../images/weather/wx/40.png");
    imagecolortransparent($image2,imagecolorat($image2,0,0));
    $imageWidth '230';
    //echo $imageWidth;

    // Generate Town
    $color imagecolorallocate($image255255255); 
    $font "fonts/hyo.ttf"
    $fontSize "15";
    $ret imagettfbbox($fontSize0$font$town);
    imagettftext($image$fontSize0, ($imageWidth $ret[2]) / 1240$color$font$town);

    // Generate Temperature
    $color imagecolorallocate($image255255255); 
    $font "fonts/arial.ttf"
    $fontSize "14";
    $ret imagettfbbox($fontSize0$font$temp);
    imagettftext($image$fontSize0, ($imageWidth $ret[2]) / 260$color$font$temp);

    // Store the image
    $insertx imagesx($image2);
    $inserty imagesy($image2);
    //  Combine The images
    imagecopymerge($image,$image2,150,0,0,50,$insertx,$inserty,100);
    // Output & Free Memory
    //var_dump($ret);
    header("Content-type: image/png");
    imagepng($image);
    imagedestroy($image);
    ?>
    Any Ideas?

  • #10
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    the image is stuck at 196x71 instead of the correct size of 230x116. I have tried setting it in the imagecopymerge() but its not working. So I changed to imagecopy() that fixed my colors, but not image size. still open for Ideas.

    PHP Code:
    imagecopy($image,$image2,130,0,0,48,230,116); 
    For some reason the image is the size of the base image,
    Last edited by moos3; 09-06-2007 at 03:50 AM. Reason: more code

  • #11
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    You actually need to create an overall blank "canvas" image of the size necessary to hold the other images/text and then place them onto this blank canvas. See this link to create a blank "canvas" - http://php.net/imagecreatetruecolor
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #12
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    Updated Code, But something is isn't right.
    PHP Code:
    <?php 
    $town 
    'Boothaby Harbor';
    $temp '90';
    $icon '40';
    // Generate Image canvas
    $canvas imagecreatetruecolor(260,130);
    imagecolortransparent($canvas,imagecolorat($canvas,0,0));

    // Combine Canvas and base
    $image imagecreatefrompng("../images/weather/wBack.png"); 
    imagecopy($canvas,$image,0,50,0,0,230,120);

    // Weather Image
    $image2 imagecreatefrompng("../images/weather/wx/{$icon}.png");
    imagecolortransparent($image2,imagecolorat($image2,0,0));

    //  Base Image Width
    $imageWidth '196';

    // Generate Town
    $color imagecolorallocate($image255255255); 
    $font "fonts/hyo.ttf"
    $fontSize "15";
    $ret imagettfbbox($fontSize0$font$town);
    imagettftext($canvas$fontSize0, ($imageWidth $ret[2]) / 1293$color$font$town);

    // Generate Temperature
    $color imagecolorallocate($canvas255255255); 
    $font "fonts/arial.ttf"
    $fontSize "14";
    $ret imagettfbbox($fontSize0$font$temp);
    imagettftext($canvas$fontSize0150110$color$font$temp);

    //  Combine The images
    imagecopy($canvas,$image2,130,0,15,5,230,120);

    // Output & Free Memory
    header("Content-type: image/png");
    imagepng($canvas);
    imagedestroy($canvas);
    ?>
    Isn't rendering correctly any Ideas?

  • #13
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    One thing that will help would be to use variables for almost everything, most importantly, the overall canvas size, so that you can use them in the calculations used to position the elements (I did not do this in the following minimal hacked code due to time constraints.) Also, use the actual image sizes in the offsets... It is likely that your weather icons have various sizes.

    The next problem I found is that you need to make the canvas transparent.

    The following is the "fairly close" code I hacked up (tested and outputs the whole images that look correct, but may need some positioning adjustments) -
    PHP Code:
    <?php 
    $town 
    'Boothaby Harbor';
    $temp '90';
    $icon '40';
    // Generate Image canvas
    $canvas imagecreatetruecolor(260,130);
    imagesavealpha($canvastrue);

    $trans_colour imagecolorallocatealpha($canvas000127);
    imagefill($canvas00$trans_colour);

    // Combine Canvas and base
    $image imagecreatefrompng("../images/weather/wBack.png"); 

      
    $insert_x imagesx($image); 
      
    $insert_y imagesy($image); 
    // imagecopy (dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h )
    imagecopy($canvas,$image,0,130-$insert_y,0,0,$insert_x,$insert_y);

    // Weather Image
    $image2 imagecreatefrompng("../images/weather/wx/{$icon}.png");

    //  Base Image Width
    $imageWidth '196';

    // Generate Town
    $color imagecolorallocate($image255255255); 
    $font "fonts/hyo.ttf"
    $fontSize "15";
    $ret imagettfbbox($fontSize0$font$town);
    imagettftext($canvas$fontSize0, ($imageWidth $ret[2]) / 1293$color$font$town);

    // Generate Temperature
    $color imagecolorallocate($canvas255255255); 
    $font "fonts/arial.ttf"
    $fontSize "14";
    $ret imagettfbbox($fontSize0$font$temp);
    imagettftext($canvas$fontSize0150110$color$font$temp);

    //  Combine The images

      
    $insert_x imagesx($image2); 
      
    $insert_y imagesy($image2); 
    // imagecopy (dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h )
    imagecopy($canvas,$image2,260-$insert_x,130-$insert_y,0,0,$insert_x,$insert_y);

    // Output & Free Memory
    header("Content-type: image/png");
    imagepng($canvas);
    imagedestroy($canvas);
    ?>
    Edit: I edited the posted code to update the last imagecopy(...) statement. There were some parameters that were left over from what you had that were cutting off things and some of the parameters needed to be the image size variables.
    Last edited by CFMaBiSmAd; 09-06-2007 at 05:33 AM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    moos3 (09-06-2007)

  • #14
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    Works prefect thanks for the help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •