Php 如何在我的新闻网站中添加自动水印脚本?
我有一个带有php和mysql的新闻网站 如何添加脚本以自动为新闻图像添加水印 此代码是我与mysql的站点新闻连接:Php 如何在我的新闻网站中添加自动水印脚本?,php,mysql,Php,Mysql,我有一个带有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"); $sql=mysql\u查询(“从新闻中选择新闻ID、标题、img、stext、ltext
$sql = mysql_query("SELECT newsid,title,img,stext,ltext,count,date,time,source FROM news WHERE newsid='$newsid' AND cat <> '1' LIMIT 1");
$sql=mysql\u查询(“从新闻中选择新闻ID、标题、img、stext、ltext、计数、日期、时间、来源,其中新闻ID='$newsid'和类别“1”限制1”);
“img”是我的新闻图像文件
如何在“img”文件中添加图像水印?您可以使用GD或imageMagick 例如: (上面链接中的代码)
01
这需要将图像作为文件存储在服务器上
首先,加载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';
到
您可以在PHP中使用创建文本水印。下面的代码使用
取代不推荐使用的mysql\uuz
函数。水印将添加到.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;
?>
样本水印
数据库中有图像数据,或文件的某种路径?。它们不再得到维护。看到了吗?改为了解,并使用或-将帮助您确定数据库中的哪一个。@complex857:i图像数据存储:列名为“img”
$config['wm_overlay_path'] = '/path/to/overlay.png';
$config['wm_type'] = 'overlay';
<?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;
?>