Watermarks .png, .gif or .jpg with .png, .gif or .jpg

I am trying to write a function that will tag any image (.png, .jpg or .gif) with another image, which can also be one of those formats. I want both images to preserve transparency. I don't care about gif animation. Unfortunately, the watermark becomes transparent where it shouldn't, and not completely transparent where it is. I have used some random images available to me to test this script and my result looks like this:

enter image description here

Whereas the watermark images look like this:

enter image description here

enter image description here

Here's my code:

  function watermark_get_left()
  {
    return NCFG_TEMPLATE_DIR . 'images/watermark_left.png';
  }

  function watermark_get_right()
  {
    return NCFG_TEMPLATE_DIR . 'images/watermark_right.png';
  }

  function create_img($path, $type)
  {
    switch($type)
    {
      case NCFG_IMAGE_JPG:
        $img = imagecreatefromjpeg($path);
        return $img;
      case NCFG_IMAGE_GIF:
        $img = imagecreatefromgif($path);
        return $img;
      case NCFG_IMAGE_PNG:
        $img = imagecreatefrompng($path);
        imagealphablending($img, false);
        imagesavealpha($img, true);
        return $img;
      default:
        return FALSE;
    }
  }

  function img_transparency($new_img, $img, $type)
  {
    if($type == NCFG_IMAGE_GIF)
    {
      $transparent_index = imagecolortransparent($img);

      if($transparent_index != -1)
      {
        $transparent_color = imagecolorsforindex($img, $transparent_index);
        $transparent_new = imagecolorallocate($new_img, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
        $transparent_new_index = imagecolortransparent($new_img, $transparent_new);
        imagefill($new_img, 0, 0, $transparent_new_index);
      }
    }else if($type == NCFG_IMAGE_PNG){
      imagecolortransparent($new_img, imagecolorallocatealpha($new_img, 0, 0, 0, 127));
      imagealphablending($new_img, false);
      imagesavealpha($new_img, true);
    }
  }

  function image_watermark($path, $new_path, &$w_height)
  {
    $type = get_image_type($path);
    list($width, $height) = getimagesize($path);

    if(!($img = create_img($path, $type)))
    {
      return FALSE;
    }

    $l_path = watermark_get_left();
    $r_path = watermark_get_right();

    if($l_exists = file_exists($l_path))
    {
      $l_type = get_image_type($l_path);
      list($l_width, $l_height) = getimagesize($l_path);

      if(!($l_img = create_img($l_path, $l_type)))
      {
        return FALSE;
      }
    }

    if($r_exists = file_exists($r_path))
    {
      $r_type = get_image_type($r_path);
      list($r_width, $r_height) = getimagesize($r_path);

      if(!($r_img = create_img($r_path, $r_type)))
      {
        return FALSE;
      }
    }

    $w_height = max(($r_exists ? $r_height : 0), ($l_exists ? $l_height : 0));

    echo("w_height: $w_height<br/>");

    $new_width = $width;
    $new_height = $height + $w_height;

    $new_img = imagecreatetruecolor($new_width, $new_height);

    $bg_color = imagecolorallocate($new_img, 255,255,255);
    imagefilledrectangle($new_img, 0, $height, $new_width - 1, $new_height - 1, $bg_color);

    img_transparency($new_img, $img, $type);
    imagecopy($new_img, $img, 0, 0, 0, 0, $width, $height);

    if($l_exists)
    {
      imagecopy($new_img, $l_img, 0, $height, 0, 0, $l_width, $l_height);
    }

    if($r_exists)
    {
      imagecopy($new_img, $r_img, $new_width - $r_width, $height, 0, 0, $r_width, $r_height);
    }

    switch($type)
    {
      case NCFG_IMAGE_JPG:
        imagejpeg($new_img, $new_path);
        break;
      case NCFG_IMAGE_GIF:
        imagegif($new_img, $new_path);
        break;
      case NCFG_IMAGE_PNG:
        imagepng($new_img, $new_path, 9);
        break;
    }

    imagedestroy($new_img);
    imagedestroy($img);

    return TRUE;
  }

      

I've tried imagecopymerge too but to no avail. I would appreciate any help on this.

Edit: My images were shown here as opposed to what I expected. The watermarked image looks correct on a white background, but not on a different background.

+3


source to share


1 answer


You can use the CLI ImageMagick for this. Here's one liner:

composite -dissolve 50% -geometry +20+10 -gravity SouthWest watermark.png input.png output.png

      

This will lay watermark.png

on top input.png

with opacity 50%

in the pixel SouthWest

offset corner +20, +10

. You can customize it to your liking and use it in PHP with shell_exec

:

shell_exec('composite ...');

      

Link: http://www.imagemagick.org/script/command-line-options.php



Image example:

Edit: If you also want to increase the height of the original image (by 20px for example), you need to do this first:

convert original.png -bordercolor transparent -border 0x20 -background transparent -gravity NorthWest -extent +0+20 input.png

      

+1


source







All Articles