简体   繁体   English

PHP中的GD支持

[英]GD support in php

I am using "xampp-win32-1.7.1-installer" as server & Dreamwaver cs5 for coding. 我正在使用“ xampp-win32-1.7.1-installer”作为服务器和Dreamwaver CS5进行编码。 I want to enable php GD support. 我想启用php GD支持。 I saw the 我看到了

phpinfo();

there is showing GD support is enable. 显示GD支持已启用。 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. 好吧,实际上我想用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. 它可以在许多其他平台上做,但是这次我想在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."

It is because your HTML form is appended to the top of the output of the image. 这是因为您的HTML表单被附加到图像输出的顶部。

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>';
}
?>

Are you actually sending HTML AFTER you've send the image/jpeg content header? 发送图像/ jpeg内容标头后,您实际上是在发送HTML吗?

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. 这首先打开输出缓冲,因此您可以在发送图像内容标头之前使用ob_clean()清除输出。

edit: corrected small error. 编辑:更正了小错误。

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>

your outputting the image as raw data. 您将图像输出为原始数据。 Thats ok, but your also sending out html code, thus corrupting your image. 没关系,但是您也会发送html代码,从而损坏了图像。

To start with you need to separate the two, and this should produce what you need assuming your existing php code works. 首先,您需要将两者分开,并且假设现有的php代码可以正常工作,这应该会产生所需的内容。

something.html 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 Creation_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. 一旦测试完,就可以使脚本和html代码在同一文件中运行。 You do this by checking the request information from your name field: 您可以通过在名称字段中检查请求信息来做到这一点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM