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
https://forums.phpfreaks.com/topic/143532-solved-help-changing-picture-with-php/
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); 
?>

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); 
?>

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.