Superimposing a php-Generated Image on another image

510 Views Asked by At

I have a background jpg image on disk on which I want to superimpose a php-generated png image. Unfortunately, GD's imagepng() function outputs the data directly, so I can't store it in a variable to copy it using imagecopy() or imagecopymerge().

I want a function that generates the png, which I can use with one of the imagecopy() functions, but I don't know how to return the generated png.

Is there a way to do this without writing the generated image to disk? Thanks. Ray

2

There are 2 best solutions below

7
On
<?php
$dest = imagecreatefrompng('vinyl.png');
$src = imagecreatefromjpeg('cover2.jpg');

imagealphablending($dest, false);
imagesavealpha($dest, true);

imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); //have to play with these numbers for it to work for you, etc.

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

imagedestroy($dest);
imagedestroy($src);
?>

you can look at https://stackoverflow.com/a/3876446/1959508 for more info

0
On

I had the same problem and have just accomplished what you are after.

I call this function...

imagecopymerge($dest, $src, $x_pos, $y_pos, 0, 0, $src_width, $src_height, 100);

...where $src is the image resource that is created by imagecreatetruecolor() and (in my case, so not sure if this is mandatory) then edited by imagecopyresampled().

My full code block is...

    $image_resized = imagecreatetruecolor($final_width, $final_height);
    if (($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG)) {
        $transparency = imagecolortransparent($image);
        $palletsize = imagecolorstotal($image);

        if ($transparency >= 0 && $transparency < $palletsize) {
            $transparent_color = imagecolorsforindex($image, $transparency);
            $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
            imagefill($image_resized, 0, 0, $transparency);
            imagecolortransparent($image_resized, $transparency);
        } elseif ($info[2] == IMAGETYPE_PNG) {
            imagealphablending($image_resized, false);
            $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
            imagefill($image_resized, 0, 0, $color);
            imagesavealpha($image_resized, true);
        }
    }
    imagecopyresampled($image_resized, $image, 0, 0, $cropWidth, $cropHeight, $final_width, $final_height, $width_old - 2 * $cropWidth, $height_old - 2 * $cropHeight);

// Make white colour transparent
        $transparency_color_id = imagecolorallocate($src, 255, 255, 255);
        $res = imagecolortransparent($src, $transparency_color_id);

$dest = imagecreatefromjpeg($bg_image_dir . $bg_image_filename); 

$x_pos = 1400 - $prod_image_right_margin - $src_width;
        $y_pos = (700 - $src_height) / 2;
        imagecopymerge($dest, $src, $x_pos, $y_pos, 0, 0, $src_width, $src_height, 100);

...so hopefully you can dig out from that what you need.