Skip to content

Instantly share code, notes, and snippets.

@abelperezlindo
Created January 30, 2023 18:19
Show Gist options
  • Save abelperezlindo/f3a6ef3c4042f13756c30887ca27374d to your computer and use it in GitHub Desktop.
Save abelperezlindo/f3a6ef3c4042f13756c30887ca27374d to your computer and use it in GitHub Desktop.
Merge images with GD in php
<?php
header('Content-type: image/png');
$mapa_base = imagecreatefrompng('/var/www/html/assets/images/a.png');
$rio_negro = imagecreatefrompng('/var/www/html/assets/images/b.png');
$bsas = imagecreatefrompng('/var/www/html/assets/images/c.png');
$cordoba = imagecreatefrompng('/var/www/html/assets/images/d.png');
imagealphablending($mapa_base, TRUE);
imagealphablending($rio_negro, TRUE);
imagealphablending($cordoba, TRUE);
imagealphablending($bsas, TRUE);
ReplaceColour($cordoba, 255, 255, 255, 255, 0, 0);
ReplaceColour($rio_negro, 255, 255, 255, 0, 255, 0);
ReplaceColour($bsas, 255, 255, 255, 0, 0, 255);
imagecopy($mapa_base, $rio_negro, 0, 0, 0, 0, 810, 1086);
imagecopy($mapa_base, $cordoba, 0, 0, 0, 0, 810, 1086);
imagecopy($mapa_base, $bsas, 0, 0, 0, 0, 810, 1086);
imagesavealpha($mapa_base, TRUE);
imagepng($mapa_base);
// Clear Memory
imagedestroy($mapa_base);
function ReplaceColour($img, $r1, $g1, $b1, $r2, $g2, $b2)
{
if(!imageistruecolor($img))
imagepalettetotruecolor($img);
$col1 = (($r1 & 0xFF) << 16) + (($g1 & 0xFF) << 8) + ($b1 & 0xFF);
$col2 = (($r2 & 0xFF) << 16) + (($g2 & 0xFF) << 8) + ($b2 & 0xFF);
$width = imagesx($img);
$height = imagesy($img);
for($x=0; $x < $width; $x++)
for($y=0; $y < $height; $y++)
{
$colrgb = imagecolorat($img, $x, $y);
if($col1 !== $colrgb)
continue;
imagesetpixel ($img, $x , $y , $col2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment