Asked  7 Months ago    Answers:  5   Viewed   34 times

I have a data URI I am getting from javascript and trying to save via php. I use the following code which gives a apparently corrupt image file:

  $data = $_POST['logoImage'];

  $uri = substr($data,strpos($data,",")+1);

  file_put_contents($_POST['logoFilename'], base64_decode($uri));

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs 9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAxklEQVQYlYWQMW7CUBBE33yITYUUmwbOkBtEcgUlTa7COXIVV5RUkXKC5AxU EdyZVD4kyKxkwIrr9vd0c7Oih aopinLNsF6Qkg2XW4XJ7LGFsAAcTV6lF5/jLdbALA9XDAXYfthFQVx OrmqKYK88/7rbbMFksALieTnzu9wDYTj6f70PKsp2kwAiSvjXNcvkWpAfNZkzWa/5a9yT7fdoX7rrB7hYh2fXo9HdjPYQZu3MIU8bYIlW20y0RUlXG2Kpv/vfwLxhTaSQwWqwhAAAAAElFTkSuQmCC

Below the code is the actual image as a Data-URI. 'logoImage' is the string above, and $uri is the string minus 'image/jpeg;base64,'.



A quick look at the PHP manual yields the following:

If you want to save data that is derived from a Javascript canvas.toDataURL() function, you have to convert blanks into plusses. If you do not do that, the decoded data is corrupted:

$encodedData = str_replace(' ','+',$encodedData);
$decodedData = base64_decode($encodedData);
Wednesday, March 31, 2021
answered 7 Months ago

In the $_FILES array, you've said you're getting [name]=>DSC_0770.JPG[type]=> [tmp_name]=>[error]=>1[size]=>0

If tmp_name is not given, that means the file was not uploaded.

The error you're given is 1, and according to the upload error messages explained page this means: that the uploaded file exceeds the upload_max_filesize directive in php.ini.

You can do the following to fix this.

  • Find your php.ini file and increase the size of upload_max_filesize.
  • Use ini_set('upload_max_filesize', '20M'); or whatever size you want at the start of your script.
  • Compress your images before uploading.
Wednesday, March 31, 2021
answered 7 Months ago

imagecreatetruecolor returns an image resource, what you need to create the data uri is an image file, also the data uri format is data:[<MIME-type>][;charset=<encoding>][;base64],<data>

echo 'data:image/png;base64,'.base64_encode(file_get_contents($new_image_file));

If you don't want to save a file to read, you can use imagepng and output buffering

 echo 'data:image/png;base64,'.base64_encode(ob_get_clean());
Saturday, May 29, 2021
answered 5 Months ago

I'm pretty sure you're hitting some regex limit. Heck, some time ago I hit a limit with 1000 chars... with 60Mb of input I bet you will likely hit regex limits everywhere also with really simple patterns. I will try at least to simplify it as much as possible, making it ungreedy with .*? instead of .* if possible.

To get more information, just check the return value of preg_last_error().

Friday, August 6, 2021
answered 3 Months ago

This is an almost exact duplicate of the following:
Python, Pandas : write content of DataFrame into text File

I report again here the answer from the cited SO question with some very small modifications to fit this case.
You can use two methods.

np.savetxt(), in which case you should have something like the following:

np.savetxt('xgboost.txt', a.values, fmt='%d', delimiter="t", header="XtYtZtValue")  

assuming a is the dataframe. Of course you can change the delimiter you want (tab, comma, space,etc.).
The other option, as mentioned in the answer I attached and in the answer here from @MYGz, is to use the to_csv method, i.e.:

a.to_csv('xgboost.txt', header=True, index=False, sep='t', mode='a')
Tuesday, September 14, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :