Category: PHP

Dynamically return an image in PHP

Create a page called image.php and paste the following code into it:

<? // create an image with width 100px, height 20px $image = imagecreate(100, 20); // create a red colour for the background // as it is the first call to imagecolorallocate(), this fills the background automatically $red_background = imagecolorallocate($image, 255, 0, 0); // create a black colour for writing on the image $black = imagecolorallocate($image, 0, 0, 0); // write the string "Hyve test" on the image // the top left of the text is at the position (10, 2) imagestring($image, 4, 10, 2,' hyve test', $black); // output the image // tell the browser what we.re sending it Header('Content-type: image/png'); // output the image as a png imagepng($image); // tidy up imagedestroy($image); ?>

Create a page called image.html and paste the following code into it:

src="https://via.placeholder.com/350x150"

Surf to the HTML page and you will see that the image appears.

Tags:

PHP

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count: