How to merge images in PHP

Asked

Viewed 32 times

0

Guys , as I could join these 3 images in one to download it , my code is like this :

<div class="imgtmn"> <?php echo "<img id='imgpos1' src='img/$cod.png' alt='' />"?></div>
          <div class="imgtmn"> <?php echo "<img id='imgpos2' src='img/$cod.png' alt='' />"?></div>
           <div class="imgtmn"> <?php echo "<img id='imgpos' src='img/$cod.png' alt='' />"?></div>

1 answer

0

I don’t know exactly how you want to join the images, but I created a code to put one image next to the other... I mean, join two images in one image.

I left some comments in the code to help you understand.

First image:

Angular

Second image:

Spring

Using code to join images:

Angular + Spring

Code:

<?php

// Foto Angular
$img_angular = imagecreatefrompng($_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . "angular.png");
$img_angular_largura = imagesx($img_angular);
$img_angular_altura = imagesy($img_angular);

// Foto Spring
$img_spring = imagecreatefrompng($_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . "spring.png");
$img_spring_largura = imagesx($img_spring);
$img_spring_altura = imagesy($img_spring);

if ($img_angular_altura >= $img_spring_altura) {
    $img_altura = $img_angular_altura;
} else {
    $img_altura = $img_spring_altura;
}

// Cria uma imagem de transparente com o tamanho das duas imagens (imagem x do lado da imagem y)
$img = imagecreatetruecolor($img_spring_largura + $img_angular_largura, $img_altura);
imagefill($img, 0, 0, imagecolorallocatealpha($img, 0, 0, 0, 127));
imagesavealpha($img, true);

// Insere a primeira imagem (angular) na imagem criada (para juntar as duas imagens)
$img_largura = 0;
imagecopyresampled($img, $img_angular, $img_largura, 0, 0, 0, $img_angular_largura, $img_angular_altura, $img_angular_largura, $img_angular_altura);

// Insere a segunda imagem (spring) a partir do último pixel que a primeira imagem ocupou
$img_largura = $img_angular_largura;
imagecopyresampled($img, $img_spring, $img_largura, 0, 0, 0, $img_spring_largura, $img_spring_altura, $img_spring_largura, $img_spring_altura);

// Cria a imagem
imagepng($img, $_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . "angular_e_spring.png");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.