Mon Jan 07, 2013 3:12 pm
<?php
// add watermark to your website photos.
// check if string end with specific substring
function endsWith($currentString, $target)
{
$length = strlen($target);
if ($length == 0) {
return true;
}
return (substr($currentString, -$length) === $target);
}
// Loop on all JPG image exists in a folder
if ($handle = opendir('user_images')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (endsWith($entry,'.jpg')==0){
// Skip images that are not JPG
echo "<br/> $entry: is ignored.\n ";
continue;
}
// Just printing the image file name
echo "<br/> $entry\n ";
// setting the image path
$image_path = "./user_images/".$entry;
// creating png image of watermark
$watermark = imagecreatefrompng('watermark.png');
// getting dimensions of watermark image
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatefromjpeg($image_path);
//something went wrong
if ($image === false) {
return false;
}
// getting the dimensions of original image
$tragetedImageSize = getImageSize($image_path);
// placing the watermark 6px from bottom and right
$wmPositionX = $tragetedImageSize[0]- $watermark_width - 6;
$wmPositionY = $tragetedImageSize[1] - $watermark_height - 6;
// blending the images together
imagealphablending($image, true);
imagealphablending($watermark, true);
// creating the new image with watermark on it,
imagecopy($image, $watermark, $wmPositionX, $wmPositionY, 0, 0, $watermark_width, $watermark_height);
// saving the created image into current directory
imagejpeg($image, "./$entry");
// Freeing memory
imagedestroy($image);
imagedestroy($watermark);
}
}
closedir($handle);
}
?>
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.