Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

GD Library basics

Options
  • 18-04-2008 11:11am
    #1
    Registered Users Posts: 673 ✭✭✭


    Hi,

    I'm working with the GD Library for the first time but am getting problems at the first step. I am using the code below as which is from a tutorial but when i view the page i just see gibberish instead of the image. I have set the Content-Type so i dont know why its coming up like this. I have also tried it within the body of a full html/text file but thats doing the same thing.


    [PHP]
    <?php

    header ('Content-Type: image/jpeg');

    $im = @imagecreatefromjpeg('../images/sig-test.jpg');

    if ($im === false) {

    die ('Unable to open image');
    }


    // $im is now an 'Image Resource', which can be used
    // with the other image functions

    echo 'Opened image';

    imagejpeg($im);

    ?>

    [/PHP]

    Can anyone see where im going wrong?

    Thanks


Comments

  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    Are you 100% sure you have jpeg support enabled? Either look at the 'gd' table in the output of phpinfo(), or do a print_r gd_info() to check.

    Oh yeah, and get rid of this:
    echo 'Opened image';

    Remember that this text is being echoed into your image code.


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    In addition to what FruitLover said, I'd also be worried about your comment:
    I have also tried it within the body of a full html/text file but thats doing the same thing

    The GD processing should be a separate file (e.g. "createimage.php") and then the HTML file (or whatever) should reference that in the same way as a standard image:

    <img src="createimage.php" alt="whatever" title="whatever" />


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    What the lads said. The GD library doesn't actually paint a picture on the screen, instead it allows you to output the binary code that makes up a valid image.

    So if the browser is expecting text and you output the binary code of an image, it'll try to interpret that as text and give you gibberish.

    In essence what you're doing is opening up a JPEG file in notepad and then pasting it straight into your HTML document, instead of linking to that JPEG file in an <img> tag.


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Ahhh, thanks lads. I've been looking at this the wrong way.

    I have now setup a file where im creating the jpg image with GD Library. I am then refererencing this file in a html document with the <img> tag. Im still getting problems though as the image is not appearing, just coming up blank. Also, i have checked and jpeg support is turned on.

    The code is as follows:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <img src="http://www.mysite.com/s/g.php">
    </body>
    </html>
    

    [PHP]<?php

    $image = 'http://www.mysite.com/ps3-images/sig-test.jpg';

    $im = @imagecreatefromjpeg($image);

    header("Content-Type: image/jpeg");

    imagejpeg($im);

    ?>[/PHP]

    Actually, does anyone know a good basic tutorial for the GD library. I have been trying to follow online tutorials but as im starting from absolute scratch im just getting lost.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    I think the best thing you can do is look for working examples (there were some on php.net iirc) and strip them for parts... cut out all the things you don't need until you're left with the working skeleton.
    You'll know for sure then if your setup is working properly, and you'll have the working skeleton to boot. :)


  • Advertisement
  • Registered Users Posts: 673 ✭✭✭Bananna man


    Hmmm, just tried the above code on another of my sites thats on a different server and its working fine. Must be something to do with the server its on.

    Thanks for the help!!


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Hmmm, just tried the above code on another of my sites thats on a different server and its working fine. Must be something to do with the server its on.

    Thanks for the help!!

    If allow_url_fopen is disabled in the PHP config on the server, then the below code:
    $image = 'http://www.mysite.com/ps3-images/sig-test.jpg';
    
    $im = @imagecreatefromjpeg($image);
    
    header("Content-Type: image/jpeg"); 
    
    will fail.

    Because you're calling socket/http library each time you load the image, I would recommend always calling it from the filesystem instead of as a url, if it's on the same machine as the script.


Advertisement