PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 11, 2022

[FIXED] How do I create a transparant area inside a non transparant solid color image using GD or Imagick?

 October 11, 2022     gd, imagick, php     No comments   

Issue

Using imagick and GD Library I tried to make a square image with a transparent center. I tried to do it like this :

$img = imagecreatetruecolor($width, $height); 

imagefilledrectangle($img, $left, $top, $right, $bottom, imagecolorallocate($img, 255, 255, 255)); 
imagecolortransparent($img, imagecolorallocate($img, 255, 255, 255));   

ob_start();    
ini_set('memory_limit', '-1');              
imagepng($img, './imagecolortransparent.png');
$blob = ob_get_clean();     

However the end result is always not transparent for the center small box. Am I doing this wrong somewhere? Any help will be appreciated :(.

enter image description here


Solution

You must activate imagesavealpha to get transparency.

imagesavealpha($img,true);
imagepng($img, './imagecolortransparent.png');


Answered By - Lorenz Meyer
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing