Combine 2 images side by side into 1 with ImageMagick (PHP)

后端 未结 4 1664
清酒与你
清酒与你 2021-02-08 09:28

I think this is an easy one.

I have 2 Pictures/JPGs and i want them to merge into one picture where the 2 are side by side.

So i have pic [A] and pic [B] and I w

相关标签:
4条回答
  • 2021-02-08 09:44

    Here the PHP Code I use at Kinoulink (a french start-up) :

    $im1 = new \Imagick($media1);
    $im2 = new \Imagick($media2);
    $imTotal = new \Imagick();
    
    $im1->cropthumbnailimage(62, 128);
    $im2->cropthumbnailimage(62, 128);
    
    $imTotal->newimage(128, 128, '#ffffffff');
    
    $imTotal->compositeimage($im1, \Imagick::COMPOSITE_DEFAULT, 0, 0);
    $imTotal->compositeimage($im2, \Imagick::COMPOSITE_DEFAULT, 66, 0);
    
    $imTotal->writeimage($albumCoverFilePath);
    
    0 讨论(0)
  • 2021-02-08 09:51

    Maybe you'll find the montage method easier to understand (this is probably what you had in mind when you tried it with composite -- but that one is for overlapping images, not for side-by-side montage...)

    montage                 \
      -background '#FFF9E3' \
      -geometry 200\!x\>    \
      -gravity west         \
       right+narrow.jpg     \
       left+wider.jpg       \
       result.jpg
    
    0 讨论(0)
  • 2021-02-08 09:55

    Here is a commandline to do the image appending due to the extended requirements, where the right picture should be offset by 200 pixels from the left edge, regardless of the (smaller) width of the left image:

     convert                          \
       -background '#FFF9E3'          \
        xc:none -resize 200x1\!       \
        right+narrow.png -append      \
        left+wider.png                \
       -gravity south                 \
       +append                        \
       -crop '400x +0+1'              \
       +repage                        \
        result.png
    

    The part xc:none -resize 200x1\! creates a 1 pixel high, 200 pixels long line and vertically appends the smaller (right) image to it.

    To this intermediate result the horizontally appending of the wider (left) image happens. We would now have a 401x100 picture with a maybe ugly line of transparent pixels on top.

    That's why we shave off this top pixel line with the -crop function.

    You should be able to translate that into PHP yourself... :-)

    0 讨论(0)
  • 2021-02-08 10:07

    No need to use -geometry if both files are the same size. Try

    exec($IMAGEMAGICK_PATH."convert picA.jpg picB.jpg +append picAB.jpg");
    

    Use -append if you want to merge pictures in a column.

    Add -background none or -background black or -background white or whatever, if your pictures are not the same size. In this case you may also want to add -gravity center or -gravity south or some such to control how exactly the two images merge. The -gravity needs to appear before the +-append on the commandline then:

    exec($IMAGEMAGICK_PATH."convert big.jpg small.jpg -gravity east -append 2x.jpg");
    
    0 讨论(0)
提交回复
热议问题