Advertisement
michaelyuen

Watermark image with resized png - PHP

Oct 27th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors',1); // enable php error display for easy trouble shooting
  3. error_reporting(E_ALL); // set error display to all
  4.  
  5. function resizePng($file, $newfile, $nWidth) {
  6.     list($width, $height, $type, $attr) = getimagesize($file);
  7.     $ratio = $width / $height;
  8.     $nHeight = $nWidth * $ratio;
  9.     $data = imagecreatefrompng($file);
  10.     $newImg = imagecreatetruecolor($nWidth, $nHeight);
  11.     imagealphablending($newImg, false);
  12.     imagesavealpha($newImg,true);
  13.     $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
  14.     imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
  15.     imagecopyresampled($newImg, $data, 0, 0, 0, 0, $nWidth, $nHeight, $width, $height);
  16.     return imagepng($newImg, $newfile);
  17. }
  18.  
  19. function watermark($watermark, $image, $newfile) {
  20.     // Load the stamp and the photo to apply the watermark to
  21.     $stamp = imagecreatefrompng($watermark);
  22.     $data = imagecreatefromjpeg($image);
  23.     // Set the margins for the stamp and get the height/width of the stamp image
  24.     $marge_right = 10;
  25.     $marge_bottom = 10;
  26.     $sx = imagesx($stamp);
  27.     $sy = imagesy($stamp);
  28.  
  29.     // Copy the stamp image onto our photo using the margin offsets and the photo
  30.     // width to calculate positioning of the stamp.
  31.     imagecopy($data, $stamp, imagesx($data) - $sx - $marge_right, imagesy($data) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
  32.  
  33.     return imagejpeg($data, $newfile, 90);
  34. }
  35.  
  36. echo (resizePng('logo.png','nlogo.png', 200) && watermark('nlogo.png','photo.jpg','watermarked.jpg')) ? 'Completed' : 'Failed';
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement