Vaibhav Gautam
Vaibhav Gautam

Reputation: 43

superimposing images using GD Library makes them grayscale. Why?

I have 2 images both PNG and i want to create a new PNG image. on which first image will be super imposed. the codes works perfectly and is generating expected image but all the colors are lost and the output is a greyscale image.

here is my code

Phone.png file is "http://i44.tinypic.com/2vwilb8.png" and background.png is "http://i43.tinypic.com/2194eu9.png"

    $backgroundImage = 'background.png';
    $userImage = "http://graph.facebook.com/4/picture?type=large";
    $resultImage ='phone.png';


    $background = imagecreatefrompng($backgroundImage);
    $userResultImage = imagecreatefromjpeg($userImage);
    $result = imagecreatefrompng($resultImage);


    list($backgroundWidth, $backgroundHeight) = getimagesize($backgroundImage);
    list($userImageWidth, $userImageHeight) =  getimagesize($userImage);
    list($resultRightItemWidth, $resultRightItemHeight) = getimagesize($resultImage);

    $newWidth = 800;
    $newHeight = 800;

    $new = imagecreate($newWidth, $newHeight);
    imagealphablending($new, false);
    imagesavealpha($new, true);

    imagecopy($new, $background, 0, 0, 0, 0, $backgroundWidth, $backgroundHeight);
    imagecopy($new, $userResultImage, 40, 300, 0, 0, $userImageWidth, $userImageHeight);
    header('Content-type: image/png');

    imagepng($new,'asdas.png');

    imagecopy($new, $result, 490, 170, 0, 0, $resultRightItemWidth, $resultRightItemHeight);
    header('Content-type: image/png');
    imagepng($new, $userID.'.png');

Upvotes: 1

Views: 92

Answers (1)

MarcoS
MarcoS

Reputation: 17711

You should use imagecreatetruecolor() instead of imagecreate()...

Upvotes: 3

Related Questions