Hi,
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
opened.
<?php
function watermark($imag e) {
//$image = $HTTP_GET_VARS['image']; // Useful if using in an img tag to
call images
//$image = str_replace(arr ay("/", ".."), "", $image); // Prevent abuse
$overlay =
'/home/httpd/vhosts/picmonkey.com/httpdocs/ADMIN/overlay.png';
$dir = '/home/httpd/vhosts/picmonkey.com/httpdocs/IMAGEUPLOADS/';
// A default image for the demo...remove if you wish.
// Set offset from bottom-right corner
$w_offset = 0;
$h_offset = 0;
$extension = strtolower(subs tr($image, strrpos($image, ".") + 1));
// Load image from file
switch ($extension)
{
case 'jpg':
$background = imagecreatefrom jpeg($image);
break;
case 'jpeg':
$background = imagecreatefrom jpeg($image);
break;
case 'png':
$background = imagecreatefrom png($image);
break;
case 'gif':
$background = imagecreatefrom gif($image);
break;
default:
die("Image is of unsupported type.");
}
// Find base image size
$swidth = imagesx($backgr ound);
$sheight = imagesy($backgr ound);
// Turn on alpha blending
imagealphablend ing($background , true);
// Create overlay image
$overlay = imagecreatefrom png($overlay);
// Get the size of overlay
$owidth = imagesx($overla y);
$oheight = imagesy($overla y);
// Overlay watermark
imagecopy($back ground, $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);
//imagejpeg($back ground,'/home/httpd/vhosts/picmonkey.com/httpdocs/ADMIN/test.jpg');
// Destroy the images
imagedestroy($b ackground);
imagedestroy($o verlay);
}
?>
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
opened.
<?php
function watermark($imag e) {
//$image = $HTTP_GET_VARS['image']; // Useful if using in an img tag to
call images
//$image = str_replace(arr ay("/", ".."), "", $image); // Prevent abuse
$overlay =
'/home/httpd/vhosts/picmonkey.com/httpdocs/ADMIN/overlay.png';
$dir = '/home/httpd/vhosts/picmonkey.com/httpdocs/IMAGEUPLOADS/';
// A default image for the demo...remove if you wish.
// Set offset from bottom-right corner
$w_offset = 0;
$h_offset = 0;
$extension = strtolower(subs tr($image, strrpos($image, ".") + 1));
// Load image from file
switch ($extension)
{
case 'jpg':
$background = imagecreatefrom jpeg($image);
break;
case 'jpeg':
$background = imagecreatefrom jpeg($image);
break;
case 'png':
$background = imagecreatefrom png($image);
break;
case 'gif':
$background = imagecreatefrom gif($image);
break;
default:
die("Image is of unsupported type.");
}
// Find base image size
$swidth = imagesx($backgr ound);
$sheight = imagesy($backgr ound);
// Turn on alpha blending
imagealphablend ing($background , true);
// Create overlay image
$overlay = imagecreatefrom png($overlay);
// Get the size of overlay
$owidth = imagesx($overla y);
$oheight = imagesy($overla y);
// Overlay watermark
imagecopy($back ground, $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);
//imagejpeg($back ground,'/home/httpd/vhosts/picmonkey.com/httpdocs/ADMIN/test.jpg');
// Destroy the images
imagedestroy($b ackground);
imagedestroy($o verlay);
}
?>