-1

I am trying to merge two images using PHP. One is a jpg that I have pulled in the form of a Facebook URL, the other is a "watermark" logo that is a png that I hope to apply in the bottom right hand corner of the Facebook image before posting the image back to Facebook.

I'm having trouble merging the two images. The function below is passed the url of the Facebook image (which is a jpg) $imagedata = the Facebook image URL. The function is currently returning: Resource id#1. I am hoping to save the merged image to the /uploads/ folder as the same previously randomly generated filename and return it's location from the function.

I have previously referenced this page: Merge a png ontop of a jpg and retain transparency with php

function createImage($imagedata) {

    $upload_dir = ($_SERVER['DOCUMENT_ROOT'] .'/uploads/');

    $filename =generateFilename().'.jpg'; //generateFilename() does nothing more than create a random string to use as a unique filename

    $filelocation=$upload_dir . $filename;

    file_put_contents($filelocation,file_get_contents($imagedata));

    $localimage = $filename;

    $dest = imagecreatefromjpeg('/uploads/' . $localimage);

    $src = imagecreatefrompng('images/overlay.png');

    imagecopyresampled($dest, $src, $src2x, $src2y, 0, 0, $src2w, $src2h, $src2w, $src2h); 

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

    imagejpeg($dest, 'dickshlitz.jpg', 100);

    imagedestroy($dest);

    imagedestroy($src);            }

Any and all help is deeply appreciated. Please ask questions if I have not been clear enough.

Community
  • 1
  • 1
  • Two things - Are you getting "Resource ID..." in text? Do you actually have "$function" in your real code? – Mat Carlson Nov 21 '13 at 03:00
  • THe $Function was a typo, I've made the edit. I get: Resource id #14 when I: print_r($dest); after the imagecopyresampled() – Scrolf Harris Nov 21 '13 at 18:56
  • That's correct - `$dest` is a resource ID and can't be printed. The image should be outputted by `imagejpeg()` - See [Example #1](http://php.net/manual/en/function.imagejpeg.php) – Mat Carlson Nov 21 '13 at 20:05
  • Ah, that would make sense. I thought it might return the created filename. I'm now using imagejpeg to create the image but the png is not overlayed, it outputs an unedited version of the original JPG. I think the issue is that the variables in imagecopyresampled() $src2x, $src2y, 0, 0, $src2w, $src2h, $src2w, etc. haven't been declared. I just took them from the example found at http://stackoverflow.com/questions/15035632/merge-a-png-ontop-of-a-jpg-and-retain-transparency-with-php – Scrolf Harris Nov 21 '13 at 23:56

1 Answers1

0

Okay, I got this working. My first issue was that I was confused by the output of imagecopyresampled() because I was trying to print it, which isn't what it's designed to do. At the time of posting the initial question I hadn't noticed the .jpg file was being output to my server, so really the code was already doing its job.

However, once I noticed the output and inspected it I noticed it wasn't functioning as expected. This was the fault of my not understanding imagecopyresampled() and passing it variables that hadn't been declared. After much tinkering the working code is below.

$dest = imagecreatefromjpeg('background.jpg');
$src = imagecreatefrompng('watermark.png');

    imagesavealpha($src, true); 
    imagealphablending($src, true);
    imagesavealpha($dest, true); 
    imagealphablending($dest, true);
    list($newwidth, $newheight, $type, $attr) = getimagesize('overlay.png');

    imagecopyresampled($dest, $src, 200 , 100, 0, 0, $newwidth , $newheight, $newwidth , $newheight); 

    imagepng($dest, $localimage);

    imagedestroy($dest);
    imagedestroy($src);

If anyone can make commnent to improve this code I would appreciate it.