Viewed   111 times

What's the proper way in PHP to create an image file (PNG), when I have the base64 encoding?

I've been playing around with:

file_put_contents('/tmp/'. $_REQUEST['id'].'.png', $_REQUEST['data']);

do I need to decode? should I be using the gd library?



My best guess is that you simply need to call base64_decode() on $_REQUEST['data'] before writing it to the file. That should be plenty enough :).

Thursday, November 3, 2022

Using imagesavealpha() and a transparent bg color should do the trick...

Based on dfilkovi's code:

// Create a new true color image
$im = new imagecreatetruecolor(100, 100);

// Fill with alpha background
$alphabg = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $alphabg);

// Convert to palette-based with no dithering and 255 colors with alpha
imagetruecolortopalette($im, false, 255);
imagesavealpha($im, true);

// Save the image
imagepng($im, './paletteimage.png');
Monday, November 21, 2022
 $real_price = 33.00;
 $real_option = 13.00;
 $option_value['price_prefix'] = "-";
 eval('$real_total = $real_price '.$option_value['price_prefix'].' $real_option'); 

Althrough I don't recommend doing it like that. Do it like this:

 $real_price = 33.00;
 $real_option = 13.00;
 $option_value['price_prefix'] = "-";
switch($option_value['price_prefix']) {
case '+': $real_total=$real_price+$real_option; break;
case '-': $real_total=$real_price-$real_option; break;
case '*': $real_total=$real_price*$real_option; break;
Saturday, November 12, 2022

GD does not support transparency in 32-bit PNG. You have to use either 8-bit with one transparent 'color' or 24-bit (officially 24-bit does not support transparency, but Photoshop can do it when using 'save for web' with 24bit png).

Friday, October 7, 2022

Try slashes rather than backslashes, and use an absolute path by leading with a slash:


That generally works for me.

Failing that, take a look at Pack URIs.

Tuesday, December 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :