KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have an image (let's call it original image ) on which I want to watermark another image (let's call it logo ). The logo is a transparent PNG, whereas the original image can be png, jpg, or gif. I have the following code: function watermarkImage($originalFileContents, $originalWidth, $originalHeight) { $logoImage = imagecreatefrompng('logo.png'); imagealphablending($logoImage, true); $logoWidth = imagesx($logoImage); $logoHeight = imagesy($logoImage); $originalImage = imagecreatefromstring($originalFileContents); $destX = $originalWidth - $logoWidth; $destY = $originalHeight - $logoHeight; imagecopy( // source $originalImage, // destination $logoImage, // destination x and y $destX, $destY, // source x and y 0, 0, // width and height of the area of the source to copy $logoWidth, $logoHeight ); imagepng($originalImage); } This code works good (good = keep the transparency of the logo ) only when the original image is a JPG file. When the original file is a GIF or PNG, the logo has a solid white background, meaning the transparency is not working. Why ? What do I need to change so it'll work ? Thanks UPDATE: Here is my recoded version: function generate_watermarked_image($originalFileContents, $originalWidth, $originalHeight, $paddingFromBottomRight = 0) { $watermarkFileLocation = 'watermark.png'; $watermarkImage = imagecreatefrompng($watermarkFileLocation); $watermarkWidth = imagesx($watermarkImage); $watermarkHeight = imagesy($watermarkImage); $originalImage = imagecreatefromstring($originalFileContents); $destX = $originalWidth - $watermarkWidth - $paddingFromBottomRight; $destY = $originalHeight - $watermarkHeight - $paddingFromBottomRight;
Tags (comma-separated)
Save Edits
Cancel