Create mosaic of images in PHP

3

What I want to do is to transform an image into a mosaico , where each pixel or group pixels of the image would be replaced by a photo of a flower of similar color. That is, transform any image into a mosaico of flowers. I already have all the logic planned, but the only question I have is how do I insert the photo of the flower in imagem .

What my código does currently is to replace every pixel with another in a shade of gray. What I want now is to replace this pixel with a imagem .

<?
$img = imagecreatefrompng('imagens/monalisa.png');
$largura = imagesx($img);
$altura = imagesy($img);

$c = 0;
$imagem = array();

$gd = imagecreatetruecolor($largura, $altura);

for ($j = 0; $j < $altura; $j++) {
for ($i = 0; $i < $largura; $i++) {
    $rgb = imagecolorat($img, $i, $j);
    $rgb = imagecolorsforindex($img, $rgb);

    $escala = $rgb['red'] + $rgb['green'] + $rgb['blue'];
    $nc = round($escala/3); 

    $cor = imagecolorallocate($gd, $nc, $nc, $nc);

    imagesetpixel($gd, $i,$j, $cor);
}
}

header('Content-Type: image/png');
imagepng($gd);
?>
    
asked by anonymous 03.10.2017 / 15:16

1 answer

0

I've been able to allocate each flower photo on the page giving echo tag img , but what I want is to transform exactly into an image just to save

<?
$ft = array("imagens/margarida.png","imagens/girassol.png","imagens/rosa.png");

$img = imagecreatefrompng('imagens/monalisa.png');
$largura = imagesx($img);
$altura = imagesy($img);

$c = 0;
$imagem = array();

$gd = imagecreatetruecolor($largura, $altura);

for ($j = 0; $j < $altura; $j++) {
for ($i = 0; $i < $largura; $i++) {
    $rgb = imagecolorat($img, $i, $j);
    $rgb = imagecolorsforindex($img, $rgb);

    $escala = $rgb['red'] + $rgb['green'] + $rgb['blue'];
    $nc = round((2 * $escala)/765); 

    //echo $nc . ' ';
    echo '<img src="'. $ft[$nc] .'"/>';

    //$cor = imagecolorallocate($gd, $nc, $nc, $nc);

    //imagesetpixel($gd, $i,$j, $cor);
}
echo '<br>';
}

//header('Content-Type: image/png');
//imagepng($gd);
?>

For now, it looks like this:

Normal Image

Mosaic

    
03.10.2017 / 16:40