gpt4 book ai didi

php - 即使 url 有效,也使用 imgur api 获取损坏的图像

转载 作者:行者123 更新时间:2023-11-30 21:59:28 25 4
gpt4 key购买 nike

这是我的代码,我使用 imgur api 上传图像并通过我的表单将它们显示在网页上。而不是显示实际图像,只显示损坏的页面图标,如果我将损坏的图像复制到 word 或记事本中,则会显示实际图像。显然图像存在并且如果当我将它复制到它显示的 word 或记事本时它是正确的路径。此外,我非常确定该 api 可以正常工作,因为保存在我的数据库中的 imgur url 可以正常工作并显示图像。有什么遗漏或我应该更改显示图像的方式吗?

if (isset($_POST['post'])) {
if ($_FILES['postimg']['size'] == 0) {
$postbody = $_POST['postbody'];
$loggedInUserId = check::isLoggedIn();
if (strlen($postbody) > 160 || strlen($postbody) < 1) {
die('Incorrect length!');
}

connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(), 0,:postimg)', array(':postbody' => $postbody));

// Post::createPost($_POST['postbody']);

} else {

$url = 'https://api.imgur.com/3/image'; // API endpoints, info: https://api.imgur.com/endpoints/image#image-upload
$client_id = ''; // Get client_id here: https://api.imgur.com/#registerapp

$fh = fopen($_FILES['postimg']['tmp_name'], 'r');
$read = fread($fh, $_FILES['postimg']['size']);
fclose($fh);
$post = array(
'image' => base64_encode($read)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Client-ID '.$client_id
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch); // Response, info: https://api.imgur.com/#responses
curl_close($ch);
// $image = json_decode($json, true); // Array
$image = json_decode($json); // Object
// var_dump($image);
//$postimg = $image['data']['link']; // Array-style
$postimg = $image - > data - > link; // Object-style

connect::query('INSERT INTO dry_posts VALUES (null, \'\', NOW(), 0, :postimg)', array(':postimg' => $postimg));

}


}
$dbposts = connect::query('SELECT * FROM dry_posts ORDER BY id DESC');
$posts = "";
//here's how I display the images/posts
foreach($dbposts as $p) {
if (!connect::query('SELECT post_id FROM post_likes WHERE post_id=:postid', array(':postid' => $p['id']))) {
$posts. = "<img src='".$p['postimg'].
"'>".htmlspecialchars($p['body']).
" <
form action = 'try.php?postid=".$p['
id ']."'
method = 'post' >
<
input type = 'submit'
name = 'like'
value = 'Like' >
<
span > ".$p['likes']."
likes < /span> <
/form> <
hr / > < /br / >
";

} else {
$posts. = "<img src='".$p['postimg'].
"'>".htmlspecialchars($p['body']).
" <
form action = 'try.php?postid=".$p['
id ']."'
method = 'post' >
<
input type = 'submit'
name = 'unlike'
value = 'Unlike' >
<
span > ".$p['likes']."
likes < /span> <
/form> <
hr / > < /br / >
";
}
}

这是表格

<form action="try.php" class = "forum" method="post" enctype="multipart/form-data">
<textarea name="postbody" rows="4" cols="60" class = "text"></textarea>
<br />Upload an image:
<input type="file" name="postimg">
<input type="submit" name="post" value="Post">
<input type="submit" value="Upload">
</form>

<div class="posts">
<?php echo $posts; ?>
</div>

最佳答案

我认为这叫做anonymous use of the API ,因此所有上传的图片都是匿名共享的,不会附加到您的帐户。

为了将图片上传到您的私有(private)帐户,您可能需要获取访问 token ,并使用Bearer 身份验证。

关于php - 即使 url 有效,也使用 imgur api 获取损坏的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43903898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com