1

Ok, so I'm learning how to use the GD library of PHP to generate polls, images, etc.. The thing is that when it comes to the image output using the imagejpeg function, nothing shows on the screen. For this exercise, I am typing text on a HTML forum, which needs to be put on an image using the GD library, basically putting text on an image. It's pretty straight-forward, I just don't understand why my image won't show. Below is both the HTML form code and the PHP code.

HTML form:

<html>
    <head> 
        <title> Create Buttons </title> 
    </head>
    <body>
        <form action ="button.php" method ="post">
            <p> Type button text here </p>
            <input type="text" name="button_text" size ="20" />
            <p> Choose button color: </p>
            <input type ="radio" name="color" value="red"> Red <br>
            <input type ="radio" name="color" value="green"> Green <br>
            <input type ="radio" name="color" value="blue"> Blue <br>
            <input type ="submit" value ="Create Button" />
        </form>    
    </body>
</html>

PHP code:

<?php
Header('Content-type: image/jpeg');
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$button_text = $_POST['button_text']; var_dump($button_text);
$color = $_POST['color']; var_dump($color);
if(empty($color) || empty($button_text)) {
    echo " Could not create image - form not filled correctly";
    exit;
}
$path = $DOCUMENT_ROOT."/uploads/$color-button.jpg";
$im = imagecreatefromjpeg ($path);
$width_image = imagesx($im);
$height_image = imagesy($im);

$width_image_wo_margins = $width_image-(2*18);
$height_image_wo_margins = $width_image-(2*18);

$font_size = 33;
putenv('GDFONTPATH=C:\Windows\Fonts');

$fontname = getenv('GDFONTPATH') . '\comic.ttf';
if(!is_file($fontname)) {
    die( "Missing Font");
}

do {
    $font_size--;
    $bbox = imagettfbbox($font_size,0,$fontname,$button_text);
    $right_text = $bbox[2];
    $left_text = $bbox[0];
    $width_text  = $right_text -$left_text;
    $height_text = abs($bbox[7] - $bbox[1]);
}

while($font_size > 8 && ( $height_text > $height_image_wo_margins || $width_text > $width_image_wo_margins));

if ( $height_text > $height_image_wo_margins || $width_text > $width_image_wo_margins) {
    echo "Text given wil not fit on button.<br />";
} else {
    $text_y = $width_image/2.0 - $width_text/2.0;
    $text_x = $height_image/2.0 - $height_text/2.0;

    if($left_text < 0) {
        $text_x += abs($left_text); //add factor for left overhang.  
    }
    $above_line_text = abs($bbox[7]);
    $text_y += $above_line_text;
    $text_y -=2;
    $white = imagecolorallocate($im,255,255,255);
    imagettftext($im, $font_size, 0, $text_x, $text_y, $white, $fontname, $button_text);

    imagejpeg($im, NULL,75); 
}
imagedestroy($im);
?>
gre_gor
  • 6,669
  • 9
  • 47
  • 52
user1787184
  • 83
  • 1
  • 4
  • 11
  • Most probably the image is corrupted along the line. These image function may return false if it fails to do its job. Trying checking the values return after function calls. Also dont forget to turn on all error reporting temporarily to see if any error will be displayed. – shawndreck Dec 16 '12 at 02:57

3 Answers3

0

It's not showing because you haven't actually done anything to display the image. While this script creates an image, you need another to actually call this just as in vanilla HTML.

This topic has been covered on SO previously: PHP GD Library: Output an image and text on same page.

I also recommend that you work through a tutorial or two. There are dozens available if you do a quick search. I won't post any here at the risk of turning this into a "20 Great GD Tutorials" post.

Community
  • 1
  • 1
Ian Atkin
  • 6,302
  • 2
  • 17
  • 24
  • I have called the function imagejpeg .. Can you suggest what I should do actually display it ? Thanks – user1787184 Dec 16 '12 at 03:03
  • You seem to be mixing two concepts. GD is usually used to either manipulate or create an image on disk (e.g., after an upload), or to actually create dynamic images using the JPEG header. You have the JPEG content header, so your script should only contain code to create an image. Say your script is called "foo.php", you would then use that in an img tag -- `` -- as if it were a normal image. Try it. I'm not kidding! – Ian Atkin Dec 16 '12 at 03:07
  • But that's for a separate if statement ... Does that actually affect the header at all ? – user1787184 Dec 16 '12 at 03:19
  • Once you have `Header('Content-type: image/jpeg');` in your script, you're done. It's set as an image mime type. No amount of conditions will change that. – Ian Atkin Dec 16 '12 at 03:25
  • I see .. So I actually forgot to add the – user1787184 Dec 16 '12 at 03:29
0

Remove the echo statements when using header('Content-Type: image/jpeg') that worked for me

Raven
  • 1
  • 1
  • 5
0

Your problem is the var_dump statement.

That is sending text to your output. The imagejpeg() call is probably creating the image fine, but it will not render with the var_dump statement.

anthony sottile
  • 61,815
  • 15
  • 148
  • 207