3

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

5
  • 2
    what is the result here: Just read the Manual page, php.net/manual/en/function.imagepng.php, this function returns bool, true/false. Commented May 20, 2015 at 16:08
  • yes but what is$image ? Commented May 21, 2015 at 7:34
  • 1
    Have you read manual yet? Commented May 21, 2015 at 7:35
  • 1
    @Arcyno: $image is resource: php.net/manual/en/function.imagecreatefrompng.php Commented May 21, 2015 at 7:35
  • Actually I read that. Let's rephrase my question : how can I display this resource on my website ? Commented May 21, 2015 at 7:36

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I didn't know a PHP file could be used as "src=" (+1), first time I see this. For those who can't make "image.php" to work, I made it work by adding ob_start(); at the beginning and ob_end_flush(); and ob_end_clean(); at the end (just before imagepng($image);).
@JoseManuelAbarcaRodríguez could please give reference not docs how did you guessed or found about these ob_XXX() calls, because it's been a whole day of grief, trying different thumbnail creator codes that did not want to display this image resource. thanks
@Meryem, try this answer ► stackoverflow.com/a/3695085/3298930
1

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

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.