Need Help with GD Creating a Watermarked Image

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

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

Site Timeline