PHP でGDを使用 して、テキストの透かしを作成できます。次のコードでは
、非推奨の関数の代わりにPDOmysql_
を使用しています。透かしは .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;
?>
サンプル透かし
