0
<?php
if(isset($_GET['img']) && is_numeric($_GET['img'])){ 
$img = $_GET['img']; 

$imgarray = array ( 
'1' => 'http://www.path/to/image1.png', 
'2' => 'http://www.path/to/image2.png', 
'3' => 'http://www.path/to/image3.png'
);

$src = $imgarray[$img];
header('Content-type: image/png');
echo file_get_contents($src);
}
else
{
    header('Content-type: image/png');
echo 'Image could not be loaded';
}
?>

Hello again stackoverflow! Im having multiple problems. 1: When the $_GET['img'] is set and its numeric, the image will be displayed right, but i want to add text in the upper-right corner of the image... How can i do that? I've looked through multiple GD tutorials and examples but i can't find my answer. 2: When $_GET['img'] isn't set i want to display the text: Image could not be loaded. How cna i do that? Because this doesn't seem to work...

Greetings

2 Answers 2

1

What you'll have to do is use GD. Load up the requested image into PHP with imagecreatefrompng(), since you have listed pngs in your array, you'd have to use imagecreatefromjpeg() or whatever depending on their format. Then use one of the text writers like imagestring() to write the text to the location in the image resource returned by imagecreatefrompng(), then return the image resource to the browser.

Can also use one of the functions that uses an external font, like imagettftext(), but would need to have the appropriate font to use on the server.

For the error, if you want it to be an image, you'll need to use imagecreatetruecolor() to make a new image, then use imagecolorallocate() to assign a color palette to it, then use imagestring() to write the error message to the image and return it. Of course, probably be easier just to make an error image in GIMP or something and return it, rather than going through the trouble of generating a new error image each time.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, imagettftext() did the job. But now i still need to place it in the upper-right corner...
To put it in the upper left is easy, would just require some experimentation with the x/y coords. The upper right, not so much. You'll need to use that imagettfbbox and do some math to determine how far from the edge to put it, since the right side could be a variable distance away from the origin.
0

Just remove the line that says header('Content-type: image/png'); in your else{} block

That will do the trick. At the moment your are telling the user's browser to treat that text as an image, of course that can't work. If you want an image with the text "Image could not be loaded", it's more complicated than that...

1 Comment

Yes, but it isn't image then, and i want to place an replacement image when the image data could not be loaded...

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.