gpt4 book ai didi

php - imagecreatefromstring(): Data is not in a recognized format in

转载 作者:可可西里 更新时间:2023-11-01 13:59:51 25 4
gpt4 key购买 nike

以下是我获取图片的方式:

$coverurl  = 'https://api.someurl/api/v1/img/' . $somenumber . '/l';
//$iheaders contains: 'Content-type' => 'image/jpeg'
$iresponse = wp_remote_get($coverurl, $iheaders);
$img = $iresponse['body'];
$testimg = base64_encode($img);

当我用 img 标签回显 $testimg 时,一切正常。

echo '<img class="attachment-shop_single size-shop_single wp-post-image" src="data:image/jpeg;base64,'.$testimg.'" width="274" />';

因为我需要将字符串转换为 jpg 并将其保存到我的上传文件夹中,所以我尝试使用 imagecreatefromstring()

$imgx = imagecreatefromstring($testimg);
if ($imgx !== false) {
header('Content-Type: image/jpeg');
imagejpeg($imgx);
imagedestroy($imgx);
} else {
echo 'An error occurred.';
}

但由于以下警告,我从来没有达到保存任何东西的地步:

 Warning: imagecreatefromstring(): Data is not in a recognized format in /etc.

当我回显 $testimg 时,我得到:

/9j/4AAQSkZJRgABAQAAAQABAAD ...Many number and characters.. Ggm2JUsEvfqnxAhGFDP/9k=

我必须做什么才能使 createimagefromstring 正常工作?我必须修改 $testimg 字符串吗?感谢您的关注。

最佳答案

方法imagecreatefromstring不采用 base_64 编码的字符串。试试这个:

$imgx = imagecreatefromstring($img); // Contents of $iresponse['body']

您可以在文档页面(上面的链接)最上面的评论中看到这一点:

<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);

关于php - imagecreatefromstring(): Data is not in a recognized format in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42228473/

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