在图片拼贴中添加蒙版

时间:2016-06-01 10:00:52

标签: php image image-processing gd

我想从我存储在服务器上的一些图片中创建拼贴画,如下所示:

collage

图像具有不同的宽高比,因此需要对它们进行遮罩,否则它们会在它们周围形成一些难看的空间,并且它们需要彼此相邻。因此我试图掩盖它们,但不幸的是,它们不再像你在下面的结果中看到的那样重新调整大小(图像不与上面拼贴中的图像相对应)。

result

谁能帮助我?

提前致谢!

<?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();
?>

0 个答案:

没有答案