2013-02-22 9 views
0

我有一個新聞網站與php & mysql如何在我的新聞站點添加自動水印腳本?

我怎麼能添加一個腳本來自動爲我的新聞圖像加水印?

這個代碼是我的網站的新聞連接到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」是我的新聞圖像文件

我如何可以添加「IMG」文件的圖像水印?

+0

你必須在數據庫中的圖像數據,或者某種路徑的文件? – complex857 2013-02-22 15:31:57

+0

[**請勿在新代碼**中使用'mysql_ *'函數](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。 – Kermit 2013-02-22 15:32:48

+0

@ complex857:我在數據庫中存儲圖像數據:列名是「img」 – 2013-02-22 15:51:57

回答

0

您可以使用GD在PHP創建文本水印。以下代碼使用PDO 代替已棄用的mysql_函數。水印添加到.png文件。如果使用其他文件類型,則需要更改imagecreatefrompng()和標題以適合。如果圖像存儲爲blob(不推薦),請使用imagecreatefromstring()以及與圖像文件類型相關的標題。

<?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

0

你可以使用GD或ImageMagick的

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

(代碼從上面的鏈接)

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 ?> 
+0

另一個指南:http://www.sitepoint.com/watermark-images-php/ – 2013-02-22 15:33:38

0

這將需要的圖像存儲在服務器上的文件。

首先,加載CodeIgniter的圖像庫,如果它尚未自動加載。

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

以下內容將在圖片底部的中間添加一個水印。

$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(); 

如果你想要一個圖像作爲水印而是改變

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

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