php 图片 位图,将Base-64图像字符串解码为位图 - php(Decode Base-64 image string to bitmap - php)...

本文档指导如何在PHP中正确处理Base64编码的图像数据,修复了isset()函数误用的问题,确保上传和解码过程顺利。通过$_POST['image_encoded']获取数据,创建空文件并保存解码后的图片。
摘要由CSDN通过智能技术生成

将Base-64图像字符串解码为位图 - php(Decode Base-64 image string to bitmap - php)

我有这个php文件:UploadToServer.php解码base 64一个图像字符串并将其保存为位图图像,当我用Postman测试并给它一个字符串时,这个错误弹出,我对php不是很熟悉。 这是UploadToServer.php:

if (isset($_POST('image_encoded'))) {

$data = $_POST('image_encoded');

$file_path = "C:/wamp/www/android_api/Uploads/test.jpg";

// create a new empty file

$myfile = fopen($filePath, "wb") or die("Unable to open file!");

// add data to that file

file_put_contents($filePath, base64_decode($data));

}

?>

这是错误:

Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead)

in C:\wamp\www\android_api\UploadToServer.php on line

I have this php file : UploadToServer.php that decode base 64 an image string and save it as a bitmap image, when I test it with Postman and give it a string, this error popups, I am not very familiar with php. This is the UploadToServer.php :

if (isset($_POST('image_encoded'))) {

$data = $_POST('image_encoded');

$file_path = "C:/wamp/www/android_api/Uploads/test.jpg";

// create a new empty file

$myfile = fopen($filePath, "wb") or die("Unable to open file!");

// add data to that file

file_put_contents($filePath, base64_decode($data));

}

?>

And this is the error :

Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead)

in C:\wamp\www\android_api\UploadToServer.php on line

原文:https://stackoverflow.com/questions/31875212

更新时间:2020-01-29 05:52

最满意答案

你应该使用$_POST['image_encoded'] 。 $ _POST标识符实际上是一个数组,因此括号应为方括号。 要自己测试一下,你可以输出print_r($_POST); 曾经,因为你永远不会忘记它。

代码将变为:

if (isset($_POST['image_encoded'])) {

$data = $_POST['image_encoded'];

$file_path = "C:/wamp/www/android_api/Uploads/test.jpg";

// create a new empty file

$myfile = fopen($filePath, "wb") or die("Unable to open file!");

// add data to that file

file_put_contents($filePath, base64_decode($data));

}

?>

You should use $_POST['image_encoded']. The $_POST identifier is actually an array, so the brackets should be square brackets. To test this yourself you could output print_r($_POST); once, as you'll never forget it again then.

The code would then become:

if (isset($_POST['image_encoded'])) {

$data = $_POST['image_encoded'];

$file_path = "C:/wamp/www/android_api/Uploads/test.jpg";

// create a new empty file

$myfile = fopen($filePath, "wb") or die("Unable to open file!");

// add data to that file

file_put_contents($filePath, base64_decode($data));

}

?>

2015-08-07

相关问答

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)

{

ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();

image.compress(compressFormat, quality, byteArrayOS);

return Base64.enc

...

摆脱字符串的开头部分:“data:image / svg + xml; base64”,那部分不是base64,只是剩下的部分。 您也不需要使用HttpUtility.UrlDecode。 您应该将TransferEncoding指定为Base64: res.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

然而,尽管如此, 在电子邮件中使用SVG还是有一些强烈的警告 。 因此,您可能需要考虑不同的格式,例如JPG或PNG。

...

证书不匹配和saml断言格式存在问题。 所以我们切换到asp.net并停止使用PHP来完成任务。 现在我们在asp.net中使用COMPONENTSPACE SAML2.0库 问题是: 我们如何设法用asp.net运行php代码 ANS:我们通过登录凭证和asp.net呼叫服务提供商向asp.net代码发送api请求,并向我们返回重定向网址。 希望这可能有所帮助 There was issues with the certificate mismatch and saml assertion fo

...

无法在不丢失某些数据的情况下“缩短”任何字符串。 如果要实际缩短编码字符串(最终结果仅为该字符串的一部分),请应用substr()而不是编码版本:您需要先解码它,然后重新编码缩短的版本。 另一种选择是压缩字符串。 这可能会缩短它:看看gzcompress() 。 但是,您的里程可能会有所不同:压缩率取决于您拥有的数据类型。 对于小输入字符串,结果甚至可能比原始字符大。 如果要在多页进程中重用变量 ,并且不想通过链接或表单传输变量 ,请考虑生成一个较短的随机密钥并将数据存储在用户的会话中: $_SE

...

您的代码中存在错误,但不是您发布的代码。 检查您编码的位图的内容,并在解码时检查您是否使用了正确的输入字符串变量。 您的编码,解码部分工作正常。 但是,如果您不依赖成员字段,那么代码会更好。 public String getStringImage(Bitmap bmp)

{

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bmp.compress(Bitmap.CompressFormat.JPEG, 100,

...

所以有两个问题: 你的字符串不是4的倍数。 它需要用'='字符填充到4的倍数。 它看起来像是用于URL等的base 64的格式,“修改了URL的Base64” 。 这使用-而不是/而不是+和_ 。 所以为了解决这个问题,你需要交换-和+和_来填充它,就像这样: public static byte[] DecodeUrlBase64(string s)

{

s = s.Replace('-', '+').Replace('_', '/').PadRight(4*((s.Length+3)/

...

使用iconv() : <?php

$input = base64_decode('GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==');//$BB6B>8l8@B,;n(B - testing

$input_encoding = 'iso-2022-jp';

echo iconv($input_encoding, 'UTF-8', $input); //其他語言測試 - testing

?>

Use iconv(): <?php

$input = base64_

...

你应该使用$_POST['image_encoded'] 。 $ _POST标识符实际上是一个数组,因此括号应为方括号。 要自己测试一下,你可以输出print_r($_POST); 曾经,因为你永远不会忘记它。 代码将变为: <?php

if (isset($_POST['image_encoded'])) {

$data = $_POST['image_encoded'];

$file_path = "C:/wamp/www/android_api/Uploads/test.jp

...

首先上传您的图像并将其存储到您的服务器中并将图像URL存储到您的数据库中。要上传图像,请使用以下代码 public void UploadFile() {

try {

// Set your file path into imagepath

FileInputStream fstrm = new FileInputStream(imagepath);

String[] urlSplit = imagepath.split("/");

...

好吧我所做的就是我在编码到base64之前计算了mime类型并将其作为变量发送 这就是我做到的 public static String getMimeType(String url) {

String type = null;

String extension = MimeTypeMap.getFileExtensionFromUrl(url);

if (extension != null) {

MimeTypeMap mime = MimeTypeMa

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值