I have a base64 encoded png, how do I write the image to a file in PHP?

You need to use base64_decode(). AND. Sometimes it is not sufficient. Here is all code that you need: $img = $_POST[‘data’]; $img = str_replace(‘data:image/png;base64,’, ”, $img); $img = str_replace(‘ ‘, ‘+’, $img); $fileData = base64_decode($img); //saving $fileName=”photo.png”; file_put_contents($fileName, $fileData); P.S. I used this code to get PNG image from html canvas.

Convert tiff to jpg in php?

In the forum at http://www.php.net/gd the following comment is written: IE doesn’t show TIFF files and standard PHP distribution doesn’t support converting to/from TIFF. ImageMagick (http://www.imagemagick.org/script/index.php) is a free software that can read, convert and write images in a large variety of formats. For Windows users it includes a PHP extension php_magickwand_st.dll (and yes, it … Read more

PHP GD Use one image to mask another image, including transparency

Matt, If you make your png with the oval white fill on black background instead of black fill with transparent background the following function does it. <?php // Load source and mask $source = imagecreatefrompng( ‘1.png’ ); $mask = imagecreatefrompng( ‘2.png’ ); // Apply mask to source imagealphamask( $source, $mask ); // Output header( “Content-type: … Read more

php GD create a transparent png image

Set imagealphablending($image,true); on each new layer. Try this: <?php $image = imagecreatetruecolor(485, 500); imagealphablending($image, false); $col=imagecolorallocatealpha($image,255,255,255,127); imagefilledrectangle($image,0,0,485, 500,$col); imagealphablending($image,true); /* add door glass */ $img_doorGlass = imagecreatefrompng(“glass/$doorStyle/$doorGlass.png”); imagecopyresampled($image, $img_doorGlass, 106, 15, 0, 0, 185, 450, 185, 450); imagealphablending($image,true); /* add door */ $img_doorStyle = imagecreatefrompng(“door/$doorStyle/$doorStyle”.”_”.”$doorColor.png”); imagecopyresampled($image, $img_doorStyle, 106, 15, 0, 0, 185, 450, 185, 450); … Read more

RGB to closest predefined color

You have to calculate the distance to each color, and pick the smallest. There are a few ways to do this. A simple method would be to calculate the distance would be: sqrt((r-r1)^2+(g-g1)^2+(b-b1)^2) A better method might be to incorporate the weighted values to calculate a distance, for instance the values used when converting RGB->YUV: … Read more

Crop whitespace from image in PHP

To trim all whitespace, as you call it, surrounding the interesting part of the image, first we find out where the “whitespace” stops, and then we copy everything inside of those borders. //load the image $img = imagecreatefromjpeg(“http://ecx.images-amazon.com/images/I/413XvF0yukL._SL500_AA280_.jpg”); //find the size of the borders $b_top = 0; $b_btm = 0; $b_lft = 0; $b_rt = … Read more