GD library does not work

Discussion in 'PHP' started by ycc, Jul 26, 2007.

  1. #1
    Hello,

    I am a complete beginner with php, please apologize stupid questions and erroneous posting :p, thanx ;)

    PHP5 and GD seems to be installed correctly in my system. I use Ubuntu Linux, 7.04, Apache2:
    GD Support 	enabled
    GD Version 	2.0 or higher
    FreeType Support 	enabled
    FreeType Linkage 	with freetype
    FreeType Version 	2.2.1
    T1Lib Support 	enabled
    GIF Read Support 	enabled
    GIF Create Support 	enabled
    JPG Support 	enabled
    PNG Support 	enabled
    WBMP Support 	enabled
    Code (markup):
    My knowledge of php is poor, the following code has been copied from the web.

    General php functions seem to work in my system, but I have problems with the GD library. The only reasonable thing I get is if I try:
    $im = @ImageCreateFromJPEG ("IMG_001.jpg"); 
    if(!$im) {
            $img_handle = ImageCreate (200, 20) or die ("Cannot Create image");
            $back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
        $txt_color = ImageColorAllocate ($img_handle, 235, 235, 51);
            ImageString ($img_handle, 10, 25, 5,  "Image Not Found", $txt_color);
        ImagePng ($img_handle); }
    Else {
        echo "GD library: Image is Found"; }
    
    Code (markup):
    The browser says:
    GD library: Image is Found
    Code (markup):

    Problems start immediately with GD library. Issuing:
    createthumb('IMG_001.jpg','tn_IMG_001.jpg',100,100);
    Code (markup):
    The browser says:
    Fatal error: Call to undefined function createthumb() in /home/yngve/public_html/index.php on line 89
    Code (markup):

    Trying the following, the browser replies with garbage:
    $height = 300;
    $width = 300;
    $im = ImageCreate($width, $height);
    $bck = ImageColorAllocate($im, 10,110,100);
    $white = ImageColorAllocate($im, 255, 255, 255);
    ImageFill($im, 0, 0, $bck);
    ImageLine($im, 0, 0, $width, $height, $white);
    for($i=0;$i<=299;$i=$i+10) {
    ImageLine($im, 0, $i, $width, $height, $white); }    
    ImagePNG($im);
    Code (markup):
    The browser replies with garbage:
    �PNG  ��� IHDR��,��,���C�6���PLTE nd������� EIDATh�Ř_h��g�3S�쎒�1]�#E6M�0�Z�a�lb�@)
    Code (markup):
    So, please, any advice is appreciated.
    According to the phpinfo() GD, is installed properly on my Linux Ubuntu Feisty Fawn 7.04, Apache2.
    However using GD functions there seems to be a problem. On the other hand, pdp commands outside GD seems OK.

    Thanx a lot. :D
     
    ycc, Jul 26, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    createthumb() is not a PHP function. It probably has been defined somewhere on the page where you found the rest of the code.

    And the garbage is actually your image. The reason you cannot see it properly is because you're not sending the appropriate content type header.

    
    header('Content-Type: image/png');
    
    PHP:
     
    nico_swd, Jul 26, 2007 IP
  3. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanx a lot for the quick and clear reply. However, there is still a problem. When I include the code suggested I get an error message. The error message suggests that "header is already sent". I therefore move the code suggested to the start of the file. I put the following code on line 4:
    <? header('Content-Type: image/png'); ?>
    Code (markup):
    However, the browser replies:
    Warning: Cannot modify header information - headers already sent by (output started at /home/yngve/public_html/index.php:4) in /home/yngve/public_html/index.php on line 4
    
    Code (markup):
    The code producing garbage is this:
    $height = 300;
    $width = 300;
    $im = ImageCreate($width, $height);
    $bck = ImageColorAllocate($im, 10,110,100);
    $white = ImageColorAllocate($im, 255, 255, 255);
    ImageFill($im, 0, 0, $bck);
    ImageLine($im, 0, 0, $width, $height, $white);
    for($i=0;$i<=299;$i=$i+10) {
    ImageLine($im, 0, $i, $width, $height, $white); }    
    ImagePNG($im); 
    
    Code (markup):
    (I removed createthumb(), of course.)

    And the image still comes out as garbage characters. Greatful for further advice :)
     
    ycc, Jul 26, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Make sure you call the header() function before sending ANY output to the browser. No HTML, no white spaces, nothing...
     
    nico_swd, Jul 26, 2007 IP
  5. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for having the patience with a n00b :)

    You are quite right, of course, the problem is not in the GD library. When I just put header and the drawing commands in a separate file, it gives a nice picture. it seems I have not yet understood how to enter the php-GD library code into my HTML.

    The start of my file now looks like this.
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    [COLOR="Red"]<? header('Content-Type: image/png'); ?>[/COLOR]
    
    	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
    	<TITLE></TITLE>
    	<META NAME="GENERATOR" CONTENT="OpenOffice.org 2.2  (Linux)">
    	<META NAME="CREATED" CONTENT="20070704;17040300">
    	<META NAME="CHANGEDBY" CONTENT="auth">
    	<META NAME="CHANGED" CONTENT="20070705;9455500">
    	<STYLE TYPE="text/css">
    	<!--
    		@page { size: 21cm 29.7cm; margin: 2cm }
    		P { margin-bottom: 0.21cm }
    	-->
    	</STYLE>
    </HEAD>
    Code (markup):
    However, I still get:
    Warning: Cannot modify header information - headers already sent by (output started at /home/yngve/public_html/index.php:4) in /home/yngve/public_html/index.php on line 4
    Code (markup):
     
    ycc, Jul 26, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Read my post again - send the header BEFORE sending ANY output to the browser.

    This:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    
    HTML:
    ... is HTML. :)


    But then again, you cannot inset images the way you're trying to do it. You have t make a separate file which ONLY sends the image data, and include this using the HTML <img> tag.
    
    <img src="script.php" />
    
    HTML:
     
    nico_swd, Jul 26, 2007 IP
  7. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That did it! :D I can only extend my deepest thanx and a virtual beer to nico_swd. Now the picture appears on the screen.

    You really saved me a lot of time! This forum rocks.

    (I tried to put the <? header('Content-Type: image/png'); ?> on the very first line of the file, but got another error message. - Sorry, I forgot that one.)


    My only small question now does not belong to this thread and I may come back about it if I don't solve it: Why does the picture appear on the right hand edge of the screen?

    However, if there is a simple answer to that one too, please shoot! ;)

    $height = 300;
    $width = 300;
    $im = ImageCreate($width, $height);
    $bck = ImageColorAllocate($im, 10,110,100);
    $white = ImageColorAllocate($im, 255, 255, 255);
    ImageFill($im, 0, 0, $bck);
    ImageLine($im, 0, 0, $width, $height, $white);
    for($i=0;$i<=299;$i=$i+10) {
    ImageLine($im, 0, $i, $width, $height, $white); }    
    ImagePNG($im);
    Code (markup):
    Thanx again ! :)
     
    ycc, Jul 26, 2007 IP