Php 上传到服务器上的损坏图像(方形):我缺少什么?

Php 上传到服务器上的损坏图像(方形):我缺少什么?,php,function,drawing,Php,Function,Drawing,我似乎无法让我的图像在线显示,我需要弄清楚如何在同一个函数中绘制其中两个。我正在创建一个简单的正方形图像,背景颜色为画布,颜色填充在圆圈内。我似乎无法让它运行。您告诉浏览器,它需要的是HTML,而不是am图像/png HTML标记 删除HTML代码,只需要一些php代码。如果要将其拆分为另一个文件square.php,请让标题告诉浏览器它是什么类型的文件: <html> <header><title>My Square</title><hea

我似乎无法让我的图像在线显示,我需要弄清楚如何在同一个函数中绘制其中两个。我正在创建一个简单的正方形图像,背景颜色为画布,颜色填充在圆圈内。我似乎无法让它运行。

您告诉浏览器,它需要的是HTML,而不是am图像/png HTML标记

删除HTML代码,只需要一些php代码。如果要将其拆分为另一个文件square.php,请让标题告诉浏览器它是什么类型的文件:

<html>
<header><title>My Square</title><header>
<body>
<?php 

/* Build a square of supplied colour. */

// Call the function creating a square of 110 pixels with colour of green.
square(110, 0, 200, 0);

// Define the function that builds the square. The parameters are size in pixels and the red, green, blue colour intensity.

function square ($size, $r, $g, $b) {
// Output the header telling the browser this is a png image.
header ('Content-type: image/png');

// create the empty image.
$im = imagecreatetruecolor($size, $size) or die('Cannot Initialize new GD image stream');

/* Define the foreground and background colours and fill the image with the bacground colour.
The foreground colour is passed as a parameter, the background colour is white */
$fgcolor = imagecolorallocate($im, $r, $g, $b);
$bgcolor = imagecolorallocate($im, 255,255,255);
imagefill($im, 0, 0, $bgcolor); 

// Draw a rectangle. The upper left coordinate is 0,0. The lower right is size-1, size-1 since we started counting at 0. 
imagerectangle($im, 0, 0, $size-1, $size-1, $fgcolor);

/* Write the image to standard out - in the case of the web server to the network to the browser
and free up any memory used by the image */
imagepng($im);
imagedestroy($im);
}
?>
</body>
<html> 
重新格式化的文件

header ('Content-type: image/png');
在工作中表现出色


或者指定标题“内容类型:image/png”

非常感谢大家拿出了html,不管出于什么原因,它现在可以正常工作了,但以前没有,谢谢你们的帮助,很快,我们非常感谢!!!
    <?php 

    /* Build a square of supplied colour. */

    // Call the function creating a square of 110 pixels with colour of green.
    square(110, 0, 200, 0);

    // Define the function that builds the square. The parameters are size in pixels and the red, green, blue colour intensity.

    function square ($size, $r, $g, $b) {
    // Output the header telling the browser this is a png image.
    header ('Content-type: image/png');

    // create the empty image.
    $im = imagecreatetruecolor($size, $size) or die('Cannot Initialize new GD image stream');

    /* Define the foreground and background colours and fill the image with the bacground colour.
    The foreground colour is passed as a parameter, the background colour is white */
    $fgcolor = imagecolorallocate($im, $r, $g, $b);
    $bgcolor = imagecolorallocate($im, 255,255,255);
    imagefill($im, 0, 0, $bgcolor); 

    // Draw a rectangle. The upper left coordinate is 0,0. The lower right is size-1, size-1 since we started counting at 0. 
    imagerectangle($im, 0, 0, $size-1, $size-1, $fgcolor);

    /* Write the image to standard out - in the case of the web server to the network to the browser
    and free up any memory used by the image */
    imagepng($im);
    imagedestroy($im);
    }