在图像拼贴中添加蒙版

I want to create a collage from some images I've stored on my server like so:

collage

The images have a different aspect ratio, so they needed to be masked or else they get some ugly space around them, and they need to be next to each other. Therefore I tried to mask them but unfortunately they won't resize anymore as you see in the result below (the images don't correspondent with the images in the collage above).

result

Who can help me out?

Thanks in advance!

<?php
class imageGrid
{

    private $realWidth;
    private $realHeight;
    private $gridWidth;
    private $gridHeight;
    private $image;

    public function __construct($realWidth, $realHeight, $gridWidth, $gridHeight)
    {
        $this->realWidth = $realWidth;
        $this->realHeight = $realHeight;
        $this->gridWidth = $gridWidth;
        $this->gridHeight = $gridHeight;

        // create destination image
        $this->image = imagecreatetruecolor($realWidth, $realHeight);

        // set image default background
        $white = imagecolorallocate($this->image, 255, 255, 255);
        imagefill($this->image, 0, 0, $white);
    }

    public function __destruct()
    {
        imagedestroy($this->image);
    }

    public function putImage($img, $sizeW, $sizeH, $posX, $posY)
    {
        // Cell width
        $cellWidth = $this->realWidth / $this->gridWidth;
        $cellHeight = $this->realHeight / $this->gridHeight;

        // Conversion of our virtual sizes/positions to real ones
        $realSizeW = ceil($cellWidth * $sizeW);
        $realSizeH = ceil($cellHeight * $sizeH);
        $realPosX = ($cellWidth * $posX);
        $realPosY = ($cellHeight * $posY);

        $newImg = imagecreatetruecolor( $realSizeW, $realSizeH );
        imagesavealpha( $newImg, true );
        imagefill( $newImg, 0, 0, imagecolorallocatealpha( $newImg, 0, 0, 0, 127 ) );

        // Perform pixel-based alpha map application
        for( $x = 0; $x < $realSizeW; $x++ ) {
            for( $y = 0; $y < $realSizeH; $y++ ) {
                $color = imagecolorsforindex( $img, imagecolorat( $img, $x, $y ) );
                imagesetpixel( $newImg, $x, $y, imagecolorallocatealpha( $newImg, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], 0 ) );
            }
        }

        imagedestroy( $img );
        $img = $newImg;

        // Copying the image
        imagecopyresampled($this->image, $img, $realPosX, $realPosY, 0, 0, $realSizeW, $realSizeH, imagesx($img), imagesy($img));
    }

    public function showGrid()
    {
        $black = imagecolorallocate($this->image, 0, 0, 0);
        imagesetthickness($this->image, 3);
        $cellWidth = ($this->realWidth - 1) / $this->gridWidth;   // note: -1 to avoid writting
        $cellHeight = ($this->realHeight - 1) / $this->gridHeight; // a pixel outside the image
        for ($x = 0; ($x <= $this->gridWidth); $x++)
        {
            for ($y = 0; ($y <= $this->gridHeight); $y++)
            {
                imageline($this->image, ($x * $cellWidth), 0, ($x * $cellWidth), $this->realHeight, $black);
                imageline($this->image, 0, ($y * $cellHeight), $this->realWidth, ($y * $cellHeight), $black);
            }
        }
    }

    public function display()
    {
        header("Content-type: image/png");
        imagepng($this->image);
    }
}

$imageGrid = new imageGrid(851, 315, 5, 3);

$imageGrid->showGrid();

$img = imagecreatefromjpeg("001.jpg");
$imageGrid->putImage($img, 1, 1, 1, 1);

$img = imagecreatefromjpeg("002.jpg");
$imageGrid->putImage($img, 1, 1, 1, 2);

$img = imagecreatefromjpeg("004.jpg");
$imageGrid->putImage($img, 1, 1, 2, 2);

$imageGrid->display();
?>