我正在开发的网站的其中一个页面应该显示漫画的相关信息,比如封面图片。
我想显示一个我通过提出卷曲请求得到的图像。
<?php
if(isset($_GET['info'])) {
$postedData = $_GET["info"]; //json object containing info about manga such as author/title etc.
$info = json_decode($postedData, true);
$mangacoverid = $info['im']; //id of the cover image that i'm getting from json object
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture; //displays image in browser
}
?>
-一些用于测试的“mangacoverid”:
ff/ff94bb880357b6b811bcbbfd3356c5ec41fbbb184291323f0ed6a86a.jpg
c1/c1b0173d8986681f23ecf5a69d26aa9dab4db40f99bed539198.jpg
0c/0cf6ebf78074e748ab2aea3a0fcb9e0dd43040974c66e24fa46703f.jpg
5d/5dcfed2e033c2da62e7ac89367533ebbc344373c46a005e274a16785.png
18/18b1f0b13bccb594c6daf296c1f9b6dbd83783bb5ae63fe1716c9091.jpg
35/35bf6095212da882f5d2122fd547800ed993c58956ec39b5a2d52ad4.jpg
-当我能够在页面中显示图像时,整个页面背景变黑,图像位于页面中间。(即style=“margin:0px;background:#0e0e0e;>”)。
我要做的是将图像插入到一个HTML标记中,这样我就可以将它放在页面的其他位置。
我试着把附加了“mangacoverid”的普通cdn链接放在一个标记中,但是图像提供者不允许热链接,它抛出了403个错误。
任何帮助都非常感谢!
最佳答案
我测试了你的代码,当封面ID是硬编码的时候,它看起来工作正常。
问题可能是通过query param传递的JSON没有被正确解析(由于URL编码)?
你用一个硬编码的封面身份证行得通吗?
文件image.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf('https://cdn.mangaeden.com/mangasimg/%s', $_GET['id']));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture;
在生成显示图像的页面的脚本中:
// Assuming $info holds the decoded json for the manga
echo(sprintf('<img src="image.php?id=%s>', $info['im']);