将php生成的图像叠加在另一个图像上

时间:2021-09-29 10:07:17

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().

我在磁盘上有一个背景jpg图像,我想在其上叠加一个php生成的png图像。不幸的是,GD的imagepng()函数直接输出数据,因此我无法将其存储在变量中以使用imagecopy()或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.

我想要一个生成png的函数,我可以使用其中一个imagecopy()函数,但我不知道如何返回生成的png。

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

有没有办法在不将生成的图像写入磁盘的情况下执行此操作?谢谢。射线

2 个解决方案

#1


0  

<?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://*.com/a/3876446/1959508 for more info

您可以查看https://*.com/a/3876446/1959508了解更多信息

#2


0  

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);

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().

...其中$ src是由imagecreatetruecolor()和(在我的情况下,因此不确定这是否是必需的)创建的图像资源,然后由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.

...所以希望你可以从你需要的东西中挖掘出来。

#1


0  

<?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://*.com/a/3876446/1959508 for more info

您可以查看https://*.com/a/3876446/1959508了解更多信息

#2


0  

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);

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().

...其中$ src是由imagecreatetruecolor()和(在我的情况下,因此不确定这是否是必需的)创建的图像资源,然后由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.

...所以希望你可以从你需要的东西中挖掘出来。