Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Merge two images.png to output.png using gd

I have been trying to merge two images using gd lib. A card (background), and a "new" frame for that card (foreground, with transparent middle).

This is my code:

<?php
    
    $card = imagecreatefrompng('img/folder1/card.png');
    $frame = imagecreatefrompng('img/folder2/frame.png');
    
    list($width,$height) = getimagesize($frame);
    
    imagealphablending($card, true);
    imagesavealpha($card, true);
    
    imagecopy($card, $frame, 0, 0, 0, 0, $width, $height, 100);
    header('Content-Type: image/png');
    imagepng($card);
    

I do get an output, but it is only the card without the frame.

Kind regards, ZF

Edit (12h later): I figured out why my output is the card (background) only, regardless of my changes.

I deleted the last value 100 after $height:

imagecopy($card, $frame, 0, 0, 0, 0, $width, $height);

Now my output is only frame.png. It doesn't matter if I use ImageAlphaBlending.

But I know that card.png is definitely under frame.png, because I moved the frame.png to the side on an axis, and the card.png gets visible. So it fails on merging the images together, respecting the transparency of frame.png

If I save the output to a file, it is a transparent .png tho.

Is imagecopy even suitable to merge frame.png with a transparent middle onto a background (card.png)?

Edit: Sorry for the mess! I was working with two versions at the same time. I have cleared up my current workspace now and wrote a new version from scratch.

I did delete the 100 after $height in the version I posted here:

imagecopy($card, $frame, 0, 0, 0, 0, $width, $height);

Clarification to previous edit: Nothing changed at all. The output was still only card.png.

BUT the code of my new version is (Final & Working):

<?php

$card = imagecreatefrompng('img/folder1/card.png');
$frame = imagecreatefrompng('img/folder2/frame.png');

$width = "402"; $height = "599";

imagealphablending($card, true);
imagesavealpha($card, true);

imagecopy($card, $frame, 0, 0, 0, 0, $width, $height);

header('Content-Type: image/png');
imagepng($card);

imagedestroy($card);
imagedestroy($frame);

As you may notice I hardcoded the image width and height. Instead of using getimagesize

list($width,$height) = getimagesize($frame);

I did check the output from $width, $height before and they are correct, but that seemed to be my problem. If someone has an answer to that, feel free to explain!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I abandoned the project for quite a while. As Maik suggested, it could have been a .png format problem.

What should I say... advice, and therefore knowledge, comes with time:

In fact the error was not produced by getimagesize and hardcoding the image size is not necessary.

It was a plain image format problem.

The mentioned "New Version" I wrote from scratch simply used different .png images with the right format.

I just didn't know this at that time, because I apparently got the same image from the creator, but it have been different renderings and thereby different format types.

Upvotes to Maik, and check your .png format type (type 3).

Edit: Oh and maybe take a look at imagick ;)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda