-http://www.thaiseoboard.com/index.php/topic,410617
uppic.php
<?php
require_once "photoinc.php"; //นำเข้าไฟล์ฟังก์ชั่น
$date = date('Y-m-dd');
$time = strftime("%H%M%S");
$seqid= "$date$time";
$photo = "$seqid".".jpg";
$SourceFile = 'photo.jpg'; //รูปที่ต้องการใส่วอเตอร์มาร์ค
$DestinationFile = "Folder ที่เก็บภาพ/"."$photo"; //ไฟล์รูปภาพใหม่ที่จะจัดเก็บ
$WaterMarkText = "สวัสดีทุกคน"; //ข้อความ 1 ที่จะใส่
$WaterMarkText2 = 'Happy New Year'; //ข้อความ 2 ที่จะใส่
watermarkImage ($SourceFile, $WaterMarkText,$WaterMarkText2, $DestinationFile);
print "<img src='$DestinationFile' border='0'><br>";
?>
photoinc.php
<?php
function watermarkImage ($SourceFile, $WaterMarkText,$WaterMarkText2, $DestinationFile) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);//กำหนดสี
$font = 'BROWAB.TTF';//กำหนดชื่อฟอนต์
$font_size = 60; //กำหนดขนาดฟอนต์ของ WaterMarkText
$font_size2 = 30; //กำหนดขนาดฟอนต์ของ WaterMarkText2
//--------เริ่มคำนวณ
$width = 470; //กำหนดความกว้างของภาพ
$box = imageTTFbbox( $font_size, 0, $font, $WaterMarkText );
$textwidth = abs( $box[2] ); //อยากรู้ความกว้างของตัวอักษร 1 กี่ Pixels ให้ echo "$textwidth";
$Xcenter = (int)($width/2 );
$width_x = $Xcenter-($textwidth/2);
$box2 = imageTTFbbox( $font_size2, 0, $font, $WaterMarkText2 );
$textwidth2 = abs( $box2[2] ); //อยากรู้ความกว้างของตัวอักษร 2 กี่ Pixels ให้ echo "$textwidth";
$Xcenter2 = (int)($width/2 );
$width2_x = $Xcenter2-($textwidth2/2);
//--------จบการคำนวณ
imagettftext($image_p, $font_size, 0, $width_x, 125, $black, $font, $WaterMarkText);
imagettftext($image_p, $font_size2, 0, $width2_x, 175, $black, $font, $WaterMarkText2);
//อธิบาย imagettftext($image_p,ขนาดฟอนต์,องศ์ษา,แนวนอน,แนวตั้ง,สี,ชื่อฟอร์ตที่ใช้,ข้อความ);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
?>