Quantcast
Channel: PHP Image Cant Make background transparent - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Answer by Matías Cánepa for PHP Image Cant Make background transparent

$
0
0

This should work for you!

<?$image = "your_image.png";$image_info = getimagesize($image);$width = $image_info[0];$height = $image_info[1];$mime_type = $image_info["mime"];$maxWidth = 250;$maxHeight = 250;$quality = 9;header("Content-type: $mime_type");$canvas = imagecreatetruecolor($maxWidth, $maxHeight);imagealphablending($canvas, false);$background = imagecolorallocatealpha($canvas, 255, 255, 255, 127);imagefilledrectangle($canvas, 0, 0, $maxWidth, $maxHeight, $background);imagealphablending($canvas, true);$leftOffset = ($maxWidth / 2) - ($width / 2);$topOffset = ($maxHeight / 2) - ($height / 2);switch($mime_type){    case "image/jpeg":        //JPG code        break;    case "image/gif":        //GIF code        break;    case "image/png":        $new_image = imagecreatefrompng($image);        imagecopyresampled($canvas, $new_image, $leftOffset, $topOffset, 0, 0, $width, $height, $width, $height);        imagealphablending($canvas, true);                imagesavealpha($canvas, true);        imagepng($canvas, null, $quality);        imagedestroy($canvas);                break;}?>

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>