在服务器上传图像(正方形):我错过了什么?

<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>

我似乎无法让我的图像在线显示,我需要弄清楚如何在同一个函数中绘制其中的两个。我正在创建一个带有背景颜色的简单方形图像,并在圆圈内填充颜色。我似乎无法让它运行。在服务器上传图像(正方形):我错过了什么?

回答:

你告诉浏览器,它是期望HTML,而不是图像/ PNG(HTML标签)。

删除您的HTML代码,只是有一些PHP代码。如果你想将它分离到另一个文件“square.php”然后让你的头()告诉浏览器是什么类型的文件:

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

你重新格式化文件

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

}

回答:

工作细phpfiddle

或指定header ('Content-type: image/png');

以上是 在服务器上传图像(正方形):我错过了什么? 的全部内容, 来源链接: utcz.com/qa/265422.html

回到顶部