By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

Need Help with GD Creating a Watermarked Image

P: n/a
Im pretty new at PHP and need help with something very simple. I wrote
a function which watermarks an image. The function works, but i can't
figure out how to output the image into a file.

I want to feed this function a URL, $image..and at the end of this
code, I want to overwrite the $image file with the new watermarked
$image file. Im getting an error stating that this file cannot be


function watermark($image) {
//$image = $HTTP_GET_VARS['image']; // Useful if using in an img tag to
call images
//$image = str_replace(array("/", ".."), "", $image); // Prevent abuse
$overlay =
$dir = '/home/httpd/vhosts/';

// A default image for the demo...remove if you wish.
// Set offset from bottom-right corner
$w_offset = 0;
$h_offset = 0;

$extension = strtolower(substr($image, strrpos($image, ".") + 1));

// Load image from file
switch ($extension)
case 'jpg':
$background = imagecreatefromjpeg($image);
case 'jpeg':
$background = imagecreatefromjpeg($image);
case 'png':
$background = imagecreatefrompng($image);
case 'gif':
$background = imagecreatefromgif($image);
die("Image is of unsupported type.");

// Find base image size
$swidth = imagesx($background);
$sheight = imagesy($background);

// Turn on alpha blending
imagealphablending($background, true);

// Create overlay image
$overlay = imagecreatefrompng($overlay);

// Get the size of overlay
$owidth = imagesx($overlay);
$oheight = imagesy($overlay);

// Overlay watermark
imagecopy($background, $overlay, $swidth - $owidth - $w_offset,
$sheight - $oheight - $h_offset, 0, 0, $owidth, $oheight);

// Output header and final image
//header("Content-type: image/jpeg");
//header("Content-Disposition: filename=" . $image);

// Destroy the images

Oct 1 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.