Pages

Thursday 12 July 2012

How to combine and merge two images using php GD


Hi, friends we all know that we can easily combine and merge two images using Photoshop and other designing software’s,  but if we want to combine and merge two image at run time using some programming code. Like here I am using PHP GD library to generate image output after merging two images. Let’s check it out.
Here we have two images















Now what I want, I want to merge this pocket on this dress at run time. Let’s check the code.
<?php
  // Set the content type header - in this case image/jpeg

  header ("Content-type: image/jpeg");

  // Defining the background image. Optionally, a .png image
  // could be used using imagecreatefrompng
  $background = imagecreatefromjpeg("bg.jpg");

  // Defining the pocket image
 
  $pkt = imagecreatefrompng("pkt.png");

  // Select the first pixel of the overlay image (at 0,0) and use
  // it's color to define the transparent color
  imagecolortransparent($pkt,imagecolorat($pkt,0,0));

  // Get pocket image width and hight for later use

  $insert_x = imagesx($pkt);
  $insert_y = imagesy($pkt);


  // Combine the images into a single output image
  imagecopymerge($background,$pkt,50,150,0,0,$insert_x,$insert_y,100);


  // Output the results as a jpg image,
  //you can also generate output as png, gif as per your requirement
   imagejpeg($background,"",100);
?>
After running following script your output will be something like this.

3 comments:

  1. Nice post. A good way to merge images using GD library.

    You can also see the below link:
    http://www.namasteui.com/php-thumb-creation-and-image-merge/

    ReplyDelete
  2. Lucky Club - Live Casino and Dining Review
    Find the latest Lucky Club casino site! luckyclub.live There is a large selection of slots and table games, but there are also plenty of table games. There are

    ReplyDelete