dongtaihui5131 2017-02-22 08:04
浏览 19

PHP - 设法从数据库获取图像URL,但无法获取图像内容和编码

Here's the part of the code that gets the URL, get the file from the URL and encode it into the base64 string.

require_once 'getimage.php';
$getImage = new GetImage();
$imageUrl = $getImage->getImageUrlByUid($senderUid);

if(!empty($imageUrl)) {
    $file = file_get_contents($imageUrl);
    $encodedImage = base64_encode(file_get_contents($file));
    $payload['encoded_image'] = $encodedImage;
} else {
    $payload['encoded_image'] = '';
}

I managed to get the image URL, i.e. $imageUrl, but somehow I keep failing to get the file and get encoded image out of it.

I kept looking for help on the Internet, and every solution looked the same. I still have no idea why I keep failing, and it's been taking ages.

ADDED

I created another file and created this function under a class.

public function getBase64ProfileByUid($uid) {
    $result = $this->conn->query("SELECT profile_image FROM users WHERE unique_id = '$uid'");
    $row = mysqli_fetch_assoc($result);

    $imageUrl = $row['profile_image'];
    if($imageUrl != NULL) {
        $file = file_get_contents($imageUrl);
        $encodedImage = base64_encode($file);
    } else {
        $encodedImage = '';
    }
    return $encodedImage;
}

This doesn't seem to work, either.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
    • ¥15 unity连接Sqlserver
    • ¥15 图中这种约束条件lingo该怎么表示出来
    • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
    • ¥15 流式socket文件传输答疑
    • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
    • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
    • ¥15 win10,这种情况怎么办
    • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
    • ¥100 在连接内网VPN时,如何同时保持互联网连接