标签:watermark image-processing php
我正在尝试为上传的图像加水印.
我有一个可以正常工作的多重上传脚本,请参见下文:
$gallery=$_GET["gallery"];
$album=$_GET["album"];
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="INSERT into commerce_images (`USER_ID`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`, `added_by`, `gallery_id`, `sub_gallery_id`)
VALUES('$user','$file_name','$file_size','$file_type', '$Fname $Sname', '$gallery', '$album'); ";
$desired_dir="uploads";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysql_query($query);
}else{
print_r($errors);
}
}
if(empty($error)){
?>
<script>location.assign("commerce-images.php?state=new");</script>
<?php
}
}
?>
是否可以对使用我的脚本上传的所有图像加水印?
解决方法:
尽管OP的问题在加水印的确切方法上并不清楚,但我在这里使用GD库显示了文本和图像加水印功能.
将文本添加为水印(使用TTF字体):Gist
function add_text_watermark($kep,$Text,$WatermarkNeeded = 1) {
list($img_type, $Image) = getImage($kep);
$sx = imagesx($Image) ;
$sy = imagesy($Image) ;
if ($WatermarkNeeded)
{
/* Set the font */
$Font="_arial.ttf";
$FontColor = ImageColorAllocate ($Image,204,204,204) ;
$FontShadow = ImageColorAllocate ($Image,100,100,100) ;
$Rotation = 0 ;
/* Make a copy image */
$OriginalImage = ImageCreateTrueColor($sx,$sy) ;
ImageCopy ($OriginalImage,$Image,0,0,0,0,$sx,$sy) ;
/* Iterate to get the size up */
$FontSize=1 ;
do
{
$FontSize *= 1.1 ;
$Box = @ImageTTFBBox($FontSize,0,$Font,$Text);
$TextWidth = abs($Box[4] - $Box[0]) ;
$TextHeight = abs($Box[5] - $Box[1]) ;
}
while ($TextWidth < $sx*0.9 && $FontSize < 30) ;
/* Awkward maths to get the origin of the text in the right place */
$x = $sx/2 - cos(deg2rad($Rotation))*$TextWidth/2 ;
$y = $sy/2 + sin(deg2rad($Rotation))*$TextWidth/2 + cos(deg2rad($Rotation))*$TextHeight/2 ;
/* Make shadow text first followed by solid text */
ImageTTFText ($Image,$FontSize,$Rotation,$x+1,$y+1,$FontShadow,$Font,$Text);
ImageTTFText ($Image,$FontSize,$Rotation,$x,$y,$FontColor,$Font,$Text);
/* merge original image into version with text to show image through text */
ImageCopyMerge ($Image,$OriginalImage,0,0,0,0,$sx,$sy,50) ;
imagejpeg($Image, $kep, 100);
}
}
function getImage($res) {
$img = "";
$type = "";
if (intval(@imagesx($res)) > 0) {
$img = $res;
} else {
$imginfo = getimagesize($res);
switch($imginfo[2]) { // Determine type
case 1:
$type = "GIF";
if (function_exists("imagecreatefromgif")) {
$img = imagecreatefromgif($res);
} else {
die("Unsupported image type: $type");
}
break;
case 2:
$type = "JPG";
if (function_exists("imagecreatefromjpeg")) {
$img = imagecreatefromjpeg($res);
} else {
die("Unsupported image type: $type");
}
break;
case 3:
$type = "PNG";
if (function_exists("imagecreatefrompng")) {
$img = imagecreatefrompng($res);
} else {
die("Unsupported image type: $type");
}
break;
}
}
return array($type, $img);
}
编辑:确保要添加为水印文本的ttf字体文件在路径中!
添加图像作为水印:Gist
function generate_watermarked_image($originalFileContents, $originalWidth, $originalHeight, $paddingFromBottomRight = 0, $watermarkFileLocation = 'logo.png') {
$watermarkImage = imagecreatefrompng($watermarkFileLocation);
$watermarkWidth = imagesx($watermarkImage);
$watermarkHeight = imagesy($watermarkImage);
$originalImage = imagecreatefromstring($originalFileContents);
$destX = $originalWidth - $watermarkWidth - $paddingFromBottomRight;
$destY = $originalHeight - $watermarkHeight - $paddingFromBottomRight;
// creating a cut resource
$cut = imagecreatetruecolor($watermarkWidth, $watermarkHeight);
// copying that section of the background to the cut
imagecopy($cut, $originalImage, 0, 0, $destX, $destY, $watermarkWidth, $watermarkHeight);
// placing the watermark now
imagecopy($cut, $watermarkImage, 0, 0, 0, 0, $watermarkWidth, $watermarkHeight);
// merging both of the images
imagecopymerge($originalImage, $cut, $destX, $destY, 0, 0, $watermarkWidth, $watermarkHeight, 100);
return $originalImage;
}
您可以将其称为:
$Image = "$desired_dir/".$file_name;
imagejpeg(generate_watermarked_image(file_get_contents($Image), imagesx($Image), imagesy($Image), 10), $Image."-watermarked.jpg", 100);
编辑:确保logo.png或您尝试添加为水印的任何文件都在路径中!
标签:watermark,image-processing,php 来源: https://codeday.me/bug/20191119/2038469.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。