title: Change the opacity of an image in PHP
body: <p>I am trying to change the opacity of an image using GD, Almost all the solutions that I found are similar to the code below, where you create a white transparent background and merge it with your image.</p>
  
  <p>But this doesn't make the image transparent, the image just gets brighter, you can't actually look through it.</p>
  
  <p>So my question is, how to change the opacity of an image so that you can look through it?
  Or am i doing something wrong with this code? </p>
  
  <pre><code>//Create an image with a white transparent background color
  $newImage = ImageCreateTruecolor(300, 300);
  $bg        = ImageColorAllocateAlpha($newImage, 255, 255, 255, 127);
  ImageFill($newImage, 0, 0, $bg);
  
  //Get the image
  $source = imagecreatefrompng('my_image.png');
  
  $opacity = 50;    
  
  //Merge the image with the background
  ImageCopyMerge($newImage,
                 $source,
                 0, 0, 0, 0,
                 300,
                 300,
                 $opacity);
  
  header('Content-Type: image/png');
  imagepng($newImage);
  imagedestroy($newImage);
  </code></pre>
  
  <p>Thank you!</p>