Jump to content

[SOLVED] Help Changing picture with php


pmiller624

Recommended Posts

I would to add text to a picture using info from mysql. I am able to make the text over the image work but then i try to make a mysql connection it say the image has errors. Heres what I got

 

This one works

<?php


Header ("Content-type: image/png");
$img_handle = imageCreateFromPNG

("http://www.icemelon.com/images/tutorials/bannerboy.png");
$color = ImageColorAllocate ($img_handle, 100, 100, 100);


ImageString ($img_handle, 3, 10, 9,  "Your IP: 5", $color);
ImagePng ($img_handle);
ImageDestroy ($img_handle); 
?>

This one doesn't and I don't know why

<?php
mysql_connect("localhost", "*****", "******") or die(mysql_error());
echo "Connected to MySQL<br />";

Header ("Content-type: image/png");
$img_handle = imageCreateFromPNG

("http://www.icemelon.com/images/tutorials/bannerboy.png");
$color = ImageColorAllocate ($img_handle, 100, 100, 100);


ImageString ($img_handle, 3, 10, 9,  "Your number: 5", $color);
ImagePng ($img_handle);
ImageDestroy ($img_handle); 
?>

Link to comment
Share on other sites

It's because you're sending a header out after printing to the screen...

 

try...

 

<?php
Header ("Content-type: image/png");

mysql_connect("localhost", "*****", "******") or die(mysql_error());
echo "Connected to MySQL<br />";

$img_handle = imageCreateFromPNG

("http://www.icemelon.com/images/tutorials/bannerboy.png");
$color = ImageColorAllocate ($img_handle, 100, 100, 100);


ImageString ($img_handle, 3, 10, 9,  "Your number: 5", $color);
ImagePng ($img_handle);
ImageDestroy ($img_handle); 
?>

Link to comment
Share on other sites

try it without the echo

 

<?php
Header ("Content-type: image/png");

mysql_connect("localhost", "*****", "******") or die(mysql_error());

$img_handle = imageCreateFromPNG

("http://www.icemelon.com/images/tutorials/bannerboy.png");
$color = ImageColorAllocate ($img_handle, 100, 100, 100);


ImageString ($img_handle, 3, 10, 9,  "Your number: 5", $color);
ImagePng ($img_handle);
ImageDestroy ($img_handle); 
?>

Link to comment
Share on other sites

dont echo anything bar the image, if there is an error, try to display an error image instead.

 

Take a look at php.nets manual:

php.net/imagecreatefrompng

 

Example #1:

<?php
function LoadPNG($imgname)
{
    /* Attempt to open */
    $im = @imagecreatefrompng($imgname);

    /* See if it failed */
    if(!$im)
    {
        /* Create a blank image */
        $im  = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);

        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

        /* Output an error message */
        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }

    return $im;
}

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

$img = LoadPNG('bogus.image');

imagepng($img);
imagedestroy($img);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.