从 url 创建图像:多个问题
<?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';
}
?>
再次你好 stackoverflow! 我有很多问题。 1:当设置$_GET['img']及其数字时,图像将显示在右侧,但我想在图像的右上角添加文本...我该怎么做?我浏览了多个 GD 教程和示例,但找不到答案。 2:当 $_GET['img'] 未设置时,我想显示文本:无法加载图像。我怎么做到这一点?因为这似乎不起作用...
问候
<?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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你所要做的就是使用 GD。使用 imagecreatefrompng() 将请求的图像加载到 PHP 中,因为您已在数组中列出了 png,所以您必须使用 imagecreatefromjpeg() 或其他取决于其格式的内容。然后使用 imagestring() 等文本编写器之一将文本写入 imagecreatefrompng() 返回的图像资源中的位置,然后将图像资源返回到浏览器。
还可以使用使用外部字体的函数之一,例如 imagettftext(),但需要在服务器上使用适当的字体。
对于该错误,如果您希望它是图像,则需要使用 imagecreatetruecolor() 制作新图像,然后使用 imagecolorallocate() 为其分配调色板,然后使用 imagestring() 写入错误向图像发送消息并将其返回。当然,在 GIMP 或其他东西中制作错误图像并返回它可能更容易,而不是每次都生成新的错误图像。
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.
只需删除
else{}
块中的header('Content-type: image/png');
行即可解决问题。目前,您告诉用户的浏览器将该文本视为图像,当然这是行不通的。如果您想要带有文本“图像无法加载”的图像,则情况比这更复杂......
Just remove the line that says
header('Content-type: image/png');
in yourelse{}
blockThat 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...