【发布时间】:2019-02-16 23:15:53
【问题描述】:
我想对现有图像进行编码,这是我当前的功能:
public function convertUploadedFileToBase64(UploadedFile $file) {
return base64_encode(file_get_contents($file->getClientFilename()));
}
所以我知道 $file 是一个正确的变量,我正在使用 Postman,它通过 var_dump 向我显示 $file 包含图像,但看起来编码不起作用。
在此之前,我使用了一个检查文件是否为有效图像的函数:
public function checkIfUploadedFileIsValid(UploadedFile $file)
{
$imageInfo = getimagesize($file->file);
if ($imageInfo['mime'] == ("image/png") ||
$imageInfo['mime'] == ("image/jpeg") ||
$imageInfo['mime'] == ("image/gif") ||
$imageInfo['mime'] == ("image/psd") ||
$imageInfo['mime'] == ("image/bmp")) {
return $file;
}
throw new \Exception("Image Invalid", 400);
}
这确实有效,但base64编码似乎是错误的。
非常感谢!
【问题讨论】:
-
但看起来编码不起作用 这对您的问题的描述不够好。请更具体
-
查看更多代码并了解
$file->getClientFilename()中的内容也会很有用 -
@RiggsFolly 好吧,我猜上面的代码有问题。有一个名为文件的变量,其中包含一个图像(.png),我想对这个图像进行编码。
-
据我们所知,您显示的小代码没有任何问题。我们不知道什么是进去的,我们不知道什么是出来的,我们不知道什么是行不通的。
-
欢迎,为了改善您的 SO 体验,请阅读如何询问 On Topic question、Question Check list 和 the perfect question 以及如何创建 Minimal, Complete and Verifiable Example