3

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

rayvd
  • 113
  • 1
  • 2
  • 10
  • imagepng() can take an optional parameter to write it to disk. Also, imagecopy() is expecting a resource to work with from one of the imagecreatefrom*() functions. – Chris Rasco Sep 11 '13 at 21:19
  • Thank you, but the problem I am having is that I don't want to write it to disk because it is user-generated, and there could be many such images, that will not be needed except for a mere fraction of a second during processing. – rayvd Sep 11 '13 at 22:38

2 Answers2

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

Community
  • 1
  • 1
MZaragoza
  • 10,108
  • 9
  • 71
  • 116
  • 1
    Thank you, but the problem I am having is that I need to generate `vinyl.png` dynamically, rather than retrieve it from disk. Thus, I cannot access it using a filename, because there isn't a filename. I want to access it using `$vinyl`, for example. I'm looking for something like `$vinyl=new imageobject($params);` and then use this object variable to copy onto a background image file. – rayvd Sep 11 '13 at 22:40
  • you can create it, save it, get the fine name, and then merge them and a new image – MZaragoza Sep 11 '13 at 22:58
  • can you show the code that you are currently using and almost working – MZaragoza Sep 11 '13 at 22:59
  • I decided to do just what you said, but I'd prefer a memory-based solution, since writing to disk is slow and otherwise unnecessary. – rayvd Sep 12 '13 at 00:04
  • `$BGimg=imagecreatefromjpeg('background.jpg');` `createImage($params);//This_function_creates_tmp.png` `$foreground=imagecreatefrompng('tmp.png');` `imagecopymerge($BGimg,$foreground,$xc,$yc,0,0,$x,$y,50);` – rayvd Sep 12 '13 at 00:07
  • Well i am glad that i was able to help. if i did help out you can mark this as the answer and vote up the thread so that other with the same problem will know the solution :) – MZaragoza Sep 12 '13 at 01:00
  • Unfortunately, it does not really answer the question. Your comment above was my work-around solution, but I would still like to know whether it is possible to superimpose a **generated** image on another (saved) image by referencing it through an object variable or something **without** having to wait while it saves to disk first. – rayvd Sep 12 '13 at 23:12
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);

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

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253