$xoffset=0;
$yoffset=0;
$opacitypercent = 80;
$scaling = 480;
$Photos=GetFileList();
foreach ($Photos as $original){
$watermark='logo.png';
AddWM($original,$watermark);
echo '*';
echo '*';
}
function AddWM($photo,$watermark){
//xoffset of the watermark
global $xoffset;
//yoffset of the watermark
global $yoffset;
//the original image to be watermarked
$originalimage=$photo;
//the watermark to apply
$watermarkimage = $watermark;
//top end opacity number (totally transparent)
$opacity0 = @MagickGetQuantumRange();
//bootom opacity (totally visible)
$opacity100 = 0;
//desired opacity percentage
// THIS IS THE ONE TO SET
global $opacitypercent;
//gather the actual opacity number
$opacity = $opacity0 * ( 1 - $opacitypercent/100 ) ;
//initialize the wands
$sourceWand = NewMagickWand();
$compositeWand = NewMagickWand();
//read in the images
@MagickReadImage($sourceWand, $originalimage);
@MagickReadImage($compositeWand, $watermarkimage);
//resize the original photo
global $scaling;
$PhotoWidth = MagickGetImageWidth($sourceWand);
$PhotoHeight = MagickGetImageHeight($sourceWand);
$WvsH = $PhotoWidth/$PhotoHeight;
if($WvsH>=1){
$resizeresult = MagickScaleImage( $sourceWand, $scaling, $scaling/$WvsH);
}else{
$resizeresult = MagickScaleImage( $sourceWand, $scaling*$WvsH, $scaling);
}
//setting the image index
MagickSetImageIndex($compositeWand, 0);
MagickSetImageType($compositeWand, MW_TrueColorMatteType);
//seting the opacity level
MagickEvaluateImage($compositeWand, MW_SubtractEvaluateOperator, $opacity, MW_OpacityChannel) ;
//combining the images
@MagickCompositeImage($sourceWand, $compositeWand, MW_OverCompositeOp, $xoffset, $yoffset);
//create a subdirectory to store photos with watermark
if(!is_dir("PhotosWithWaterMark")){
mkdir("PhotosWithWaterMark", 0755);
}
//generate the image
MagickWriteImage( $sourceWand, "PhotosWithWaterMark/"."WM_".$photo);
}
function GetFileList(){
$dir = "./";
$PhotoFormats = array('jpg','jpeg');
// Open a known directory, and proceed to read its contents
if ($dh = opendir($dir)) {
$PhotosList = '';
while (($file = readdir($dh)) !== false) {
$NameExt = explode(".",$file);
if (in_array(strtolower($NameExt[count($NameExt)-1]),$PhotoFormats)){
echo "filename: $file" . "\n";
$PhotosList[] = $file;
}
}
closedir($dh);
}
return $PhotosList;
}
?>
No comments:
Post a Comment