开发者

Create image from url: multiple problems

开发者 https://www.devze.com 2023-01-31 23:11 出处:网络
<?php if(isset($_GET[\'img\']) && is_numeric($_GET[\'img\'])){ $img = $_GET[\'img\'];开发者_如何学Go
<?php
if(isset($_GET['img']) && is_numeric($_GET['img'])){ 
$img = $_GET['img'];开发者_如何学Go 

$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


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.


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消