in reply to Re: Merging png by using GD.
in thread Merging png by using GD.

Thanks for replying. > You start with an image: $myimage > You clone that image into another image: $copy The only reason that i clone that is to take the finally $genename.png with two png the one next to other.When i succeed on the finnaly $genename.png i will replace $copy with another image according to my goal on the script. Also i have tried to many values (such as $myimage->copyMerge($copy,10,10,0,0,25,25,50)) in order to take the two images, the one next to the other, as one.Any other ideas?

Replies are listed 'Best First'.
Re^3: Merging png by using GD.
by BrowserUk (Patriarch) on Mar 29, 2011 at 20:30 UTC

    Given the names of two image files as arguments, this script will create a third image with the two side by side:

    #! perl -slw use strict; use GD; my $im1 = GD::Image->new( $ARGV[ 0 ] ) or die; my $im2 = GD::Image->new( $ARGV[ 1 ] ) or die; my( $x1, $y1 ) = $im1->getBounds(); my( $x2, $y2 ) = $im2->getBounds(); my( $x3, $y3 ) = ( $x1 + $x2, $y1 > $y2 ? $y1 : $y2 ); my $im3 = GD::Image->new( $x3, $y3, 1 ); $im3->copy( $im1, 0, 0, 0, 0, $x1, $y1 ); $im3->copy( $im2, $x1, 0, 0, 0, $x2, $y2 ); open PNG, '>:raw', 'new.png' or die $!; print PNG $im3->png; close PNG; system 'new.png';;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Merging png by using GD.
by chrestomanci (Priest) on Mar 29, 2011 at 19:56 UTC

    It sounds to me like you are trying to create one large image that contains several smaller ones.

    The process to do that should be fairly simple:

    1. Create a large empty GD Image that is big enough to contain all your smaller images. You might want to set the background colour at this point.
    2. Load each image in turn, and paste it into your big image at the correct location.
    3. Save the big image in your prefered file format.

    Overall your code might look something like this: (Untested)

    use strict; use warnings; use GD; my $outImage = GD::Image->new(1000,1000); # Assume this image is 500x500 pixels my $imageTopLeft = GD::Image->newFromPng('/path/to/file/top/left.png') +; $outImage->copyResized($imageTopLeft, # Src Image; 0,0, # $dstX,$dstY, 0,0, # $srcX,$srcY, 500, 500, # $destW,$destH, 500, 500 ); # $srcW,$srcH, # Assume this image is 200x300 pixels my $imageBottomRight = GD::Image->newFromPng('/path/to/file/bottom/rig +ht.png'); $outImage->copyResized($imageBottomRight, # Src Image; 800,700, # $dstX,$dstY, 0,0, # $srcX,$srcY, 200, 300, # $destW,$destH, 200, 300 ); # $srcW,$srcH, open OUTIMAGE, ">", '/path/to/output/filename.png' or die "Error, unab +le to write output $!"; binmode OUTIMAGE; print OUTIMAGE $outImage->png; close OUTIMAGE;
      thank you very much.It really helps me.