webrider
webrider

Reputation: 95

GD support in php

I am using "xampp-win32-1.7.1-installer" as server & Dreamwaver cs5 for coding. I want to enable php GD support. I saw the

phpinfo();

there is showing GD support is enable. But it still doesn't work. I don't know why it doesn't work? What should I do?

Well, actually i want to create an image with php. There is text box & submit button. When i give an input & press submit, it appears in that image box. It can do in many others platform but this time i want to do it in php.

here is my code :

<?php
header("Content-type: image/jpeg");
?>
<form action="Creating_Images_with_PHP.php" method="get">
<input type="text" name="name" />
    <input type="submit" value="Enter" />
</form>

<?php
$name = $_GET['name'];
$message = "Welcome to php academy, $name";

$length = strlen($message) * 9.3;

$image = imagecreate($length, 20);
$background = imagecolorallocate($image, 0, 0, 0);
$foreground = imagecolorallocate($image, 255, 255, 255);

imagestring($image, 5,5,1, $message, $foreground);

imagejpeg($image)
?>

and the showing error is :

"The image http://localhost/www/...blaa blaa blaa cannot be displayed because it contains errors."

Upvotes: 0

Views: 545

Answers (4)

paulcol.
paulcol.

Reputation: 3000

your outputting the image as raw data. Thats ok, but your also sending out html code, thus corrupting your image.

To start with you need to separate the two, and this should produce what you need assuming your existing php code works.

something.html

<form action="Creating_Images_with_PHP.php" method="get">
<input type="text" name="name" />
    <input type="submit" value="Enter" />
</form>

Creating_Images_with_PHP.php

<?php

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

$name = $_GET['name'];
$message = "Welcome to php academy, $name";

$length = strlen($message) * 9.3;

$image = imagecreate($length, 20);
$background = imagecolorallocate($image, 0, 0, 0);
$foreground = imagecolorallocate($image, 255, 255, 255);

imagestring($image, 5,5,1, $message, $foreground);

imagejpeg($image)
?>

Once you've tested that you can work on making the script and html code live in the same file. You do this by checking the request information from your name field:

if(isset($_GET['name'][1])){
    /* generate image */
}else{
    /* output form */
}

Upvotes: 0

Prasad Rajapaksha
Prasad Rajapaksha

Reputation: 6190

I tried with your code. It works fine for me.

<?php 

if(isset($_GET['name']))
{
header("Content-type: image/jpeg");
$name = $_GET['name'];
$message = "Welcome to php academy, $name";

$length = strlen($message) * 9.3;

$image = imagecreate($length, 20);
$background = imagecolorallocate($image, 0, 0, 0);
$foreground = imagecolorallocate($image, 255, 255, 255);

imagestring($image, 5,5,1, $message, $foreground);

imagejpeg($image);
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="get">
<input type="text" name="name" />
    <input type="submit" value="Enter" />
</form>
</body>
</html>

Upvotes: 1

Dennis Jamin
Dennis Jamin

Reputation: 386

Are you actually sending HTML AFTER you've send the image/jpeg content header?

Try as followes:

<?php
    ob_start();
?>

<form action="Creating_Images_with_PHP.php" method="get">
    <input type="text" name="name" />
    <input type="submit" value="Enter" />
</form>

<?php
    if (isset($_GET['name']) && !empty($_GET['name']))
    {
        ob_clean();
        header("Content-type: image/jpeg");
        $name = $_GET['name'];
        $message = "Welcome to php academy, $name";

        $length = strlen($message) * 9.3;

        $image = imagecreate($length, 20);
        $background = imagecolorallocate($image, 0, 0, 0);
        $foreground = imagecolorallocate($image, 255, 255, 255);

        imagestring($image, 5,5,1, $message, $foreground);

        imagejpeg($image);
    }
?>

This first turns on output buffering so you can clear the output using ob_clean() before sending image content headers.

edit: corrected small error.

Upvotes: 1

Alasdair
Alasdair

Reputation: 14103

It is because your HTML form is appended to the top of the output of the image.

Make them separate scripts, or change it to this:

<?php
if (isset($_GET['name']) && $_GET['name']!='')
 {
 header("Content-type: image/jpeg");
 $name = $_GET['name'];
 $message = "Welcome to php academy, $name";

 $length = strlen($message) * 9.3;

 $image = imagecreate($length, 20);
 $background = imagecolorallocate($image, 0, 0, 0);
 $foreground = imagecolorallocate($image, 255, 255, 255);

 imagestring($image, 5,5,1, $message, $foreground);

 imagejpeg($image);
 }
else
{
echo '<html><body><form action="Creating_Images_with_PHP.php" method="get">
<input type="text" name="name" />
    <input type="submit" value="Enter" />
</form></body></html>';
}
?>

Upvotes: 1

Related Questions