Question

I want to watermark an image and save it. here is the code i am using for it. Here it is outputting the image and is storing that output to the file. I want to save it without outputting it.

// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('proverbs.jpeg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
  imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy -   $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
ob_start();
// output jpeg (or any other chosen) format & quality
imagejpeg($im, NULL, 85);
// capture output to string
$contents = ob_get_contents();
// end capture
ob_end_clean();
//imagepng($im);
imagedestroy($im);
$fh = fopen("proverbs.jpeg", "w" );
fwrite( $fh, $contents );
fclose( $fh );
Was it helpful?

Solution

Saving it to a file?

Simply change

imagejpeg($im, NULL, 85);

to

imagejpeg($im, 'image.jpg', 85);

OTHER TIPS

This is the code,

<?php

header('content-type: image/jpeg');
$src = $_GET['src'];
$path = pathinfo($src);
$watermark = imagecreatefrompng('watermark.png'); 
$watermark_width = imagesx($watermark); 
$watermark_height = imagesy($watermark); 
$image = imagecreatetruecolor($watermark_width, $watermark_height); 
if ($path['extension']=='png') 
$image = imagecreatefrompng($src); 
else if ($path['extension']=='jpg'||$path['extension']=='jpeg') 
$image = imagecreatefromjpeg($src); 
$size = getimagesize($_GET['src']); 
$dest_x = $size[0] - $watermark_width-10; 
$dest_y = $size[1] - $watermark_height-10; 
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50); 
imagejpeg($image); 
imagedestroy($image); 
imagedestroy($watermark); 

?> 

Details Here, Image watermark in PHP

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top