php:显示图像资源

时间:2015-05-20 16:01:41

标签: php html image

我觉得很蠢但我无法理解如何显示imagepng()函数的输出。

我目前所做的是(并且工作正常):

<img src=<?php echo getImage($element); ?> />

function getImage($element){
    return $bdd[$element]; // the result is a string with the path to the image
}

但是我想在图像上绘制一些圆圈,所以这就是我想要做的(并且它不起作用):

<img src=<?php echo getImage($element); ?> />

function getImage($element){
    $image = imagecreatefrompng($bdd[$element]);
    $ellipseColor = imagecolorallocate($image, 0, 0, 255);
    imagefilledellipse($image, 100, 100, 10, 10, $ellipseColor);
    imagepng($image);

    return $image // why does that image resource not display ?
}

但是除了符号之外它没有显示任何东西..我假设它返回一个完整的图像而不是图像的路径..所以我应该如何用圆圈显示我的图像呢?

ps:我还试图创建一个由<img src=<?php echo 'getImage.php?element=' . $element; ?> />调用但未成功的页面getImage.php

2 个答案:

答案 0 :(得分:3)

你必须做这样的事情

<img src="image.php">

在image.php中,您使用代码

$image = imagecreatefrompng($bdd[$element]);
$ellipseColor = imagecolorallocate($image, 0, 0, 255);
imagefilledellipse($image, 100, 100, 10, 10, $ellipseColor);
imagepng($image);

答案 1 :(得分:1)

我终于得到了关于如何在HTML页面中显示图像的答案: 我需要将代码放在另一个文件displayImage.php中,然后从<img>标记调用此页面:

带有html标记的主文件:

<img src="displayImage.php?element='<?php echo $element; ?> />

displayImage.php:

<?php

header('Content-Type: image/png');
$image = imagecreatefrompng($bdd[$element]);
$ellipseColor = imagecolorallocate($image, 0, 0, 255);
imagefilledellipse($image, 100, 100, 10, 10, $ellipseColor);
imagepng($image);
imagedestroy( $image );

?>
相关问题