Home > PHP > Create watermark images using PHP

Create watermark images using PHP

Put watermark on images using PHP

<?php
// this script creates a watermarked image from an image file – can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark – goes in the same directory as this script
// where this script is named watermark.php
// call this script with an image tag

//put  your  main image into  : images/yourimage.jpg

//put your  watermark image  into  : images/watermark.gif

$imagesource = $_SERVER[‘DOCUMENT_ROOT’].”/”.”images/”.”yourimage.jpg”;
if (!file_exists($imagesource)) die();
$filetype = strtolower(substr($imagesource,strlen($imagesource)-4,4));
if($filetype == “.gif”) $image = @imagecreatefromgif($imagesource);
if($filetype == “.jpg”) $image = @imagecreatefromjpeg($imagesource);
if($filetype == “.png”) $image = @imagecreatefrompng($imagesource);
if (empty($image)) die();
$watermark = @imagecreatefromgif(‘images/watermark.gif’);
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth – $watermarkwidth)/2);
$startheight = (($imageheight – $watermarkheight)/2);
imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
header(“Content-type: image/jpeg”);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

?>

Advertisements
Categories: PHP
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: