Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda