首页 > 技术文章 > Automatically watermark all uploaded photos (给所有上传的相片加水印)

94YY 2015-10-20 20:40 原文

Hello,

This mod automatically watermark all uploaded photos.

Price: FREE, enjoy.

You will have to edit 3 files:

1. ../classes/image.class.php - watermark function.
2. ../modules/upload/photo.php - when upload photos from upload page.
3. ../modules/album/addphotos.php - when add photos to existing album.
---------------------------------------------

1. In "../classes/images.class.php" find (~ line 195):

	
public function watermark()
{
}


and replace with:


public function watermark($SourceFile, $WatermarkFile, $SaveToFile = NULL)
{
$watermark = @imagecreatefrompng($WatermarkFile)
or exit('Cannot open the watermark file.');
imageAlphaBlending($watermark, false);
imageSaveAlpha($watermark, true);
$image_string = @file_get_contents($SourceFile)
or exit('Cannot open image file.');
$image = @imagecreatefromstring($image_string)
or exit('Not a valid image format.');
$imageWidth=imageSX($image);
$imageHeight=imageSY($image);
$watermarkWidth=imageSX($watermark);
$watermarkHeight=imageSY($watermark);
$coordinate_X = ( $imageWidth - 5) - ( $watermarkWidth);
$coordinate_Y = ( $imageHeight - 5) - ( $watermarkHeight);
imagecopy($image, $watermark, $coordinate_X, $coordinate_Y, 0, 0, $watermarkWidth, $watermarkHeight);
if(!($SaveToFile)) header('Content-Type: image/jpeg');
imagejpeg ($image, $SaveToFile);
imagedestroy($image);
imagedestroy($watermark);
if(!($SaveToFile)) exit;
}




2. In "../modules/upload/photo.php" find (~ line 90):


$dst = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
$image->process($src, $dst, 'MAX_WIDTH', 575, 0);
$image->resize(true, true);


and after "$image->resize(true, true);" insert this:


$image_location = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
// Locate the watermark file wherever you choose (remember PNG format). I put in ../media/photos/watermark.png
$watermark_location = $config['BASE_DIR']. '/media/photos/watermark.png';
$save_watermarked_file_to = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
$image->watermark($image_location, $watermark_location, $save_watermarked_file_to);



3. In "../modules/album/addphotos.php" find (~ line 40):


$dst = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
$image->process($src, $dst, 'MAX_WIDTH', 575, 0);
$image->resize(true, true);



and like the previous after "$image->resize(true, true);" insert this:


$image_location = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
// Locate the watermark file wherever you choose (remember PNG format). I put in ../media/photos/watermark.png
$watermark_location = $config['BASE_DIR']. '/media/photos/watermark.png';
$save_watermarked_file_to = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
$image->watermark($image_location, $watermark_location, $save_watermarked_file_to);


!!! ++$photos; must be below the pasted code.

There is screens from thats files after mods.

image.class.php

 

1. ../classes/image.class.php - watermark function.
i did a copy

2. ../modules/upload/photo.php - when upload photos from upload page.


$image->process($src, $dst, 'MAX_WIDTH', 575, 0);
$image->resize(true, true);
$dst = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
$image->process($src, $dst, 'MAX_WIDTH', 575, 0);
$image->resize(true, true);
$image_location = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
// Locate the watermark file wherever you choose (remember PNG format). I put in ../media/photos/watermark.png
$watermark_location = $config['BASE_DIR']. '/media/photos/watermark.png';
$save_watermarked_file_to = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
$image->watermark($image_location, $watermark_location, $save_watermarked_file_to);




3. ../modules/album/addphotos.php - when add photos to existing album.


$image->process($src, $dst, 'MAX_WIDTH', 575, 0);
$image->resize(true, true);
$dst = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
$image->process($src, $dst, 'MAX_WIDTH', 575, 0);
$image->resize(true, true);
$image_location = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
// Locate the watermark file wherever you choose (remember PNG format). I put in ../media/photos/watermark.png
$watermark_location = $config['BASE_DIR']. '/media/photos/watermark.png';
$save_watermarked_file_to = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
$image->watermark($image_location, $watermark_location, $save_watermarked_file_to);

++$photos;


think somewhere there is error


$image->process($src, $dst, 'MAX_WIDTH', 575, 0);
$image->resize(true, true);
$dst = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
$image->process($src, $dst, 'MAX_WIDTH', 575, 0);
$image->resize(true, true);


in my code not 2x "'MAX_WIDTH', 575, 0"

its looks like you added that to:


$dst = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
$image->process($src, $dst, 'MAX_WIDTH', 575, 0);
$image->resize(true, true);


but I was thinking to be pasted after that :) 

推荐阅读