简体   繁体   中英

how can add auto watermark script in my news site?

i have a news site with php & mysql

how can i add a script to auto watermark my news images?

this code is my site news connection to mysql :

$sql = mysql_query("SELECT newsid,title,img,stext,ltext,count,date,time,source FROM news WHERE newsid='$newsid' AND cat <> '1' LIMIT 1");

"img" is my news image file

How can i add an image watermark on "img" file?

You could use GD or imageMagick

For example: http://permadi.com/blog/2010/03/using-php-gd-to-add-watermark-to-images/

(code from link above)

01  <?php
02     // Load the image where the logo will be embeded into
03     $image = imagecreatefromjpeg($_GET['imageURL']);
04   
05   
06     // Load the logo image
07     $logoImage = imagecreatefrompng("logo.png");
08     imagealphablending($logoImage, true);
09        
10     // Get dimensions
11     $imageWidth=imagesx($image);
12     $imageHeight=imagesy($image);
13        
14     $logoWidth=imagesx($logoImage);
15     $logoHeight=imagesy($logoImage);    
16        
17     // Paste the logo
18     imagecopy(
19        // destination
20        $image,
21        // source
22        $logoImage,
23        // destination x and y
24        $imageWidth-$logoWidth, $imageHeight-$logoHeight,   
25        // source x and y
26        0, 0,
27        // width and height of the area of the source to copy
28        $logoWidth, $logoHeight);
29           
30     // Set type of image and send the output
31     header("Content-type: image/png");
32     imagePng($image);
33    
34     // Release memory
35     imageDestroy($image);
36     imageDestroy($imageLogo);    
37  ?>

This will require images to be stored as files on the server.

First, load CodeIgniter's image library if it's not already autoloaded.

$this->load->library('image_lib');

The following will add a watermark at the bottom of your image in the center.

$config['source_image'] = '/path/to/image.jpg';
$config['wm_text'] = 'Your watermark text';
$config['wm_type'] = 'text';
$config['wm_font_path'] = './system/fonts/texb.ttf';
$config['wm_font_size'] = '16';
$config['wm_font_color'] = 'ffffff';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_padding'] = '20';

$this->image_lib->initialize($config); 

$this->image_lib->watermark();

If you want an image as a watermark instead, change

$config['wm_text'] = 'Your watermark text';
$config['wm_type'] = 'text';

to

$config['wm_overlay_path'] = '/path/to/overlay.png';
$config['wm_type'] = 'overlay';

You can use GD in PHP to create text watermark. The following code uses PDO in place of deprecated mysql_ functions. The watermark is added to a .png file. If other file type is used you will require to change imagecreatefrompng() and header to suit.If the image is stored as blob (Not recommended) use imagecreatefromstring() and the header relevant to the file type of image.

<?php
function ImageStringCenter($image, $fontSize, $lineNumber, $totalLines, $text, $color ) { 
    $centerX = ceil( ( imagesx($image) - ( ImageFontWidth($fontSize) * strlen($text) ) ) / 2 ); 
    $centerY = ceil( ( ( imagesy($image) - ( ImageFontHeight($fontSize) * $totalLines ) ) / 2)  + ( ($lineNumber-1) * ImageFontHeight($fontSize) ) ); 
    ImageString($image, $fontSize, $centerX, $centerY, $text, $color ); 
} 
require("dbinfo.php");//database connection paramerters 
$id = 1;
//Connect to database
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
    // Prepare statement
    $stmt = $dbh->prepare("SELECT * FROM images WHERE id = ?");
    // Assign parameters
    $stmt->bindParam(1,$id);
    //Execute query
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $stmt->execute();
    // Fetch Result
    $result = $stmt -> fetch();
    $image1 = $result["image2"];
    $im = imagecreatefrompng($image1);//for .png file
    $text_color = imagecolorallocate($im, 266, 266, 266);
    ImageStringCenter($im, 5, 1, 2,  "Watermark", $text_color);
    ImageStringCenter($im, 5, 2, 2,  "20/02/2013", $text_color);
    header("Content-Type: image/png");//for .png file
    imagepng($im);
    imagedestroy($image1);
}


catch(PDOException $e) {
    echo "The following error occured.". $e->getMessage() ;// Remove or modify after testing 
    file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", mapSelect.php, ". $e->getMessage()."\r\n", FILE_APPEND);  
 }
//Close the connection
$dbh = null; 
?>

Sample watermark 样本水印

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM