Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php : display image resource

Tags:

html

php

image

I feel quite stupid but I can't understand how to display the output of the imagepng() function.

What I currently do is (and it works fine) :

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

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

But I would love to draw some circles on the image, so here is what I would like to do (and it does not work) :

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

But it does not display anything else than symbols.. I assume it returns a full image and not a path to the image.. so How should I display my image with the circle on it ?

ps : I also tried to create a page getImage.php that would be called by <img src=<?php echo 'getImage.php?element=' . $element; ?> /> but with no success

like image 498
Arcyno Avatar asked Nov 18 '25 21:11

Arcyno


2 Answers

You have to do something like this

<img src="image.php">

And in the image.php you use your code

$image = imagecreatefrompng($bdd[$element]);
$ellipseColor = imagecolorallocate($image, 0, 0, 255);
imagefilledellipse($image, 100, 100, 10, 10, $ellipseColor);
imagepng($image);
like image 84
Dave Plug Avatar answered Nov 21 '25 10:11

Dave Plug


I finally got my answer on how to display the image inside an HTML page : I need to put the code in another file displayImage.php and make a call to this page from the <img>tag :

Main file with html tag:

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

?>
like image 21
Arcyno Avatar answered Nov 21 '25 12:11

Arcyno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!