Watermarking Images in PHP

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

Watermarking Images in PHP

Postby MT Shahzad on June 4th, 2008, 12:04 pm

Do you ever need to watermark images used on your website for any reason. If you need to do that, then that is not a problem. You don't have to change your files.

Please find the attached files showing you a demo on how to water mark images in php.

demo.php
Code: Select all
<html>

<head>
   <title>Water Marking Demo</title>
</head>

<body>

<h2>Original Image</h2>
<img src="shop.jpg" width="306" height="160" alt="" title="" />
<br />
<br />
<h2>Image with Water-mark</h2>
<img src="watermark.php?img=shop.jpg" width="306" height="160" alt="" title="" />

</body>

</html>


watermark.php
Code: Select all
<?
   $sSrcImagePath = $_REQUEST['img'];

   $sImgWatermark    = imagecreatefrompng('watermark.png');
   $iWatermarkWidth  = imagesx($sImgWatermark);
   $iWatermarkHeight = imagesy($sImgWatermark);

   $sImgSource = imagecreatefromjpeg($sSrcImagePath);

   list($iSourceWidth, $iSourceHeight) = getimagesize($sSrcImagePath);

   $iWatermarkX = 0;
   $iWatermarkY = ($iSourceHeight - $iWatermarkHeight);

   imagecopy($sImgSource, $sImgWatermark, $iWatermarkX, $iWatermarkY, 0, 0, $iWatermarkWidth, $iWatermarkHeight);

   header('content-type: image/jpeg');

   imagejpeg($sImgSource, NULL, 100);
   imagedestroy($sImgSource);
   imagedestroy($sImgWatermark);
?>
You do not have the required permissions to view the files attached to this post.
MT Shahzad
Web/Software Developer
http://mts.sw3solutions.com
User avatar
MT Shahzad
Site Admin
 
Posts: 300
Joined: February 29th, 2008, 8:11 am
Location: Muridke, Pakistan

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests

cron