Created
September 12, 2015 15:45
How to add a watermark image as a transparent layer to an existing image in PHP using GD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generate_watermarked_image($originalFileContents, $originalWidth, $originalHeight, $paddingFromBottomRight = 0, $watermarkFileLocation = 'logo.png') { | |
$watermarkImage = imagecreatefrompng($watermarkFileLocation); | |
$watermarkWidth = imagesx($watermarkImage); | |
$watermarkHeight = imagesy($watermarkImage); | |
$originalImage = imagecreatefromstring($originalFileContents); | |
$destX = $originalWidth - $watermarkWidth - $paddingFromBottomRight; | |
$destY = $originalHeight - $watermarkHeight - $paddingFromBottomRight; | |
// creating a cut resource | |
$cut = imagecreatetruecolor($watermarkWidth, $watermarkHeight); | |
// copying that section of the background to the cut | |
imagecopy($cut, $originalImage, 0, 0, $destX, $destY, $watermarkWidth, $watermarkHeight); | |
// placing the watermark now | |
imagecopy($cut, $watermarkImage, 0, 0, 0, 0, $watermarkWidth, $watermarkHeight); | |
// merging both of the images | |
imagecopymerge($originalImage, $cut, $destX, $destY, 0, 0, $watermarkWidth, $watermarkHeight, 100); | |
return $originalImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment