2

Let's suppose I want to create a face generator, where I would design several pictures for face form, ears, noses, mouth, hair. Given a combination of those pictures how can I create a new picture in PHP? For instance, a simplified version:

There is face1.png, face2.png, nose1.png and nose2.png. How could I programmatically merge face1.png with nose2.png, so the result picture would hold content from both picture?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Found this and thought it might help http://stackoverflow.com/questions/13007463/image-transparency-and-alpha-when-merging-images-with-php – ka_lin Dec 25 '15 at 21:14
  • Take a look at http://php.net/manual/en/function.imagecopy.php and the rest of the [php gd](http://php.net/manual/en/book.image.php) functions – Musa Dec 25 '15 at 21:14

3 Answers3

2

You've basically got three options: GD, Cairo or ImageMagic. I'd recommend using the Imagick class if it's available. If not, ImageMagick through PHP system calls. If that's not available, GD will probably suffice.

It depends on your server configuration, which of these are available and which would require additional packages to be installed.

There's a very simple example in the Imagick documentation of combining images: https://secure.php.net/manual/en/imagick.compositeimage.php

I also found this example for GD: Merge two PNG images with PHP GD library

Tuure
  • 521
  • 4
  • 12
1

There is a function named imagecopy. This function overrides a part of the destination image using a source image. The given part is specified as parameters. Before you tell me that this does not solve your problem, I have to add that the pixels in the destination picture will not be overriden by the pixels of the source picture if the pixels are transparent. You need to use imagealphablending and imagesavealpha on the source picture, like this:

public static function experimental($images, $width, $height, $dest = null) {

        $index = 0;

        if ($dest === null) {
            $dest = $images[$index++];
        }

        while ($index < count($images)) {
            imagealphablending($images[$index], true);
            imagesavealpha($images[$index], true );
            imagecopy($dest, $images[$index++], 0, 0, 0, 0, $width, $height);
        }

        return $dest;
    }

If we have these two pictures:

enter image description here enter image description here

The result will be this:

enter image description here

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Kicsi Viziló
  • 675
  • 5
  • 15
0

You really want make it with PHP? Ok.

1) You can use GD library for image processing. 2) You can use imagemagic PHP wrapper -- imagic.

But I think you should use canvas on client side. And for saving result you can send base64 png image representation of canvas (or separated layers/pictures) to backend.

Deep
  • 2,472
  • 2
  • 15
  • 25
  • Image generation should not really be given into the hands of the client-side for security purposes. Also, a user agent with Javascript disabled would not be helpful. This should be done on the server, using the GD library. I was asking how can you do it? – Lajos Arpad Dec 25 '15 at 21:21
  • @LajosArpad How? And for client and for backend you must calculate bounds/positions of pictures and merge this layers. You want example for GD library? – Deep Dec 25 '15 at 21:25
  • I am afraid you did not understand the question. Let us suppose that there are more pictures, each contain some useful content and some transparent background. The useful contents do not overlap. I want to merge the useful contents into a single picture. I could calculate the bounds, but I think this was already done by someone. So, if and only if you know the answer, then you can tell me what functions should be combined and how. If not, then I am afraid that I cannot accept this answer, although, I appreciate the effort. – Lajos Arpad Dec 25 '15 at 21:39
  • Im understand your question. This work must be handmade (you or other people later), because cant correct automatically add bounds/coords of all source pictures. – Deep Dec 25 '15 at 21:52
  • And for complete stored bounds/coord/images you can generate any variations of merge layers. – Deep Dec 25 '15 at 22:00
  • Someone found an answer. I asked her to write her answer here. It was tested and it works. – Lajos Arpad Dec 26 '15 at 18:58