Skip to content

Instantly share code, notes, and snippets.

@RakibSiddiquee
Created July 17, 2018 10:47
Show Gist options
  • Save RakibSiddiquee/3d337f7ac3faa8e86a075e2c65920e46 to your computer and use it in GitHub Desktop.
Save RakibSiddiquee/3d337f7ac3faa8e86a075e2c65920e46 to your computer and use it in GitHub Desktop.
Place watermark over an image in PHP or place any image over another image
<?php
// Load the logo stamp and the photo to apply the watermark to
$logo = imagecreatefrompng('https://cdn.jagonews24.com/media/common/og-logo.png');
//$logo = imagecolorallocate($logo,0,0,0);
$img = imagecreatefromjpeg('https://cdn.jagonews24.com/media/imgAll/2018June/shahin-cover-20180711084009.jpg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($logo);
$sy = imagesy($logo);
$bg = imagecreatetruecolor(imagesx($img), 45);
// sets background color
$color = imagecolorallocatealpha($bg, 97, 24, 0, 60);
imagefill($bg, 0, 0, $color);
imagecopy($bg, $logo, imagesx($bg) - $sx - $marge_right, imagesy($bg) - $sy - $marge_bottom, 0, 0, imagesx($logo), imagesy($logo));
$catName = 'Women';
$width = 200;
$height = 45;
$fontSize = 15;
$xPosition = (($width/2)-((imagefontwidth($fontSize)*strlen($catName))/2));
$yPosition = (($height/2)-(imagefontheight($fontSize)/2));
$text = imagecreate($width, $height);
imagecolorallocate($text, 255, 255, 255);
$textcolor = imagecolorallocate($text, 0, 0, 0);
imagestring($text, $fontSize, $xPosition, $yPosition, $catName, $textcolor);
imagecopy($bg, $text, 0, 0, 0, 0, imagesx($text), imagesy($text));
//$logobc = imagecolorallocatealpha($logo,0,0,0,1);
////imagefill($logo, 0, 0, $logobc);
//imagefilledrectangle($logo, 0, 0, 750, 45,$logobc);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($img, $bg, imagesx($img) - imagesx($bg), imagesy($img) - imagesy($bg), 0, 0, imagesx($bg), imagesy($bg));
// Output and free memory
header('Content-type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
//var_dump($iii);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment