好得很程序员自学网
  • 首页
  • 后端语言
    • C#
    • PHP
    • Python
    • java
    • Golang
    • ASP.NET
  • 前端开发
    • Angular
    • react框架
    • LayUi开发
    • javascript
    • HTML与HTML5
    • CSS与CSS3
    • jQuery
    • Bootstrap
    • NodeJS
    • Vue与小程序技术
    • Photoshop
  • 数据库技术
    • MSSQL
    • MYSQL
    • Redis
    • MongoDB
    • Oracle
    • PostgreSQL
    • Sqlite
    • 数据库基础
    • 数据库排错
  • CMS系统
    • HDHCMS
    • WordPress
    • Dedecms
    • PhpCms
    • 帝国CMS
    • ThinkPHP
    • Discuz
    • ZBlog
    • ECSHOP
  • 高手进阶
    • Android技术
    • 正则表达式
    • 数据结构与算法
  • 系统运维
    • Windows
    • apache
    • 服务器排错
    • 网站安全
    • nginx
    • linux系统
    • MacOS
  • 学习教程
    • 前端脚本教程
    • HTML与CSS 教程
    • 脚本语言教程
    • 数据库教程
    • 应用系统教程
  • 新技术
  • 编程导航
    • 区块链
    • IT资讯
    • 设计灵感
    • 建站资源
    • 开发团队
    • 程序社区
    • 图标图库
    • 图形动效
    • IDE环境
    • 在线工具
    • 调试测试
    • Node开发
    • 游戏框架
    • CSS库
    • Jquery插件
    • Js插件
    • Web框架
    • 移动端框架
    • 模块管理
    • 开发社区
    • 在线课堂
    • 框架类库
    • 项目托管
    • 云服务

当前位置:首页>后端语言>PHP
<tfoot draggable='sEl'></tfoot>

php自动打水印 php print

很多站长朋友们都不太清楚php自动打水印,今天小编就来给大家整理php自动打水印,希望对各位有所帮助,具体内容如下:

本文目录一览: 1、 PHP如何根据图片色阶不同添加水印 2、 php如何实现自动加水印 3、 谁有能用的php给图片加文字水印,最好有使用方法,注意:一定是能用的,功能强不强大另说 PHP如何根据图片色阶不同添加水印

在使用php编程的时候, 很多时候需要对上传的图片加水印,来确定图片版权和出处. 但是,一般情况下加水印的位置是图片的右下角, 但是,不同图片的色阶不同,有时候我们 图片的水印和图片本身色阶相同,就会造成水印不明显.

下面这段代码可以实现自动识别图片的色阶,更加色阶差来添加图片的水印,这样可以避免水印和图片色阶一样的弊端.

<?php

function add_wm($nmw_water, $src_file, $output_file, $x, $y) {

if(file_exists($output_file))

return;

$w1 = MagickGetImageWidth($nmw_water);

$h1 = MagickGetImageHeight($nmw_water);

$nmw =NewMagickWand();

MagickReadImage($nmw, $src_file);

// 默认的加水印位置调整

$lt_w = 50;

$lt_h = 50;

if($x == 0){

$w = MagickGetImageWidth($nmw);

$h = MagickGetImageHeight($nmw);

$x = $w;

$y = $h;

}else{

// 根据具体情况调整

$lt_w = 30;

$lt_h = 40;

}

MagickCompositeImage($nmw, $nmw_water, MW_OverCompositeOp, $x - $w1 - $lt_w, $y - $h1 - $lt_h);

MagickWriteImage($nmw, $output_file);

DestroyMagickWand($nmw);

}

// 还是groovy的eachFileRecurse好用啊

function add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr) {

$dp = dir($to_dir);

while($file=$dp->read()){

if($file != '.' $file != '..'){

if(is_dir($to_dir . '/' . $file)){

mkdir($output_dir . '/' . $file);

add_wm_recurse($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $arr);

}else{

if(!array_key_exists($to_dir . '/' . $file, $arr)){

continue;

}

$sub_arr = $arr[$to_dir . '/' . $file];

if($sub_arr){

$x = intval($sub_arr[0]);

$y = intval($sub_arr[1]);

add_wm($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $x, $y);

}

}

}

}

$dp->close();

}

$to_dir = './resized';

$output_dir = './output';

// 这个是我用java的ImageIO遍历图片像素获取的符合裤子颜色的区域的坐标array(posX, posY)

$arr = array(

array(50, 50)

);

$water = './water.png';

$nmw_water =NewMagickWand();

MagickReadImage($nmw_water, $water);

add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr);

DestroyMagickWand($nmw_water);

?>

补充:

PHP图像处理模块 MagickWand用法

MagickWand 是一个PHP的模块,用来访问 ImageMagick 的图像处理库。下面是一个使用 MagicWand 的代码片段:

$magick_wand=NewMagickWand();

MagickReadImage($magick_wand,'rose.jpg');

$drawing_wand=NewDrawingWand();

DrawSetFont($drawing_wand,"/usr/share/fonts/bitstream-vera/Vera.ttf");

DrawSetFontSize($drawing_wand,20);

DrawSetGravity($drawing_wand,MW_CenterGravity);

$pixel_wand=NewPixelWand();

PixelSetColor($pixel_wand,"white");

DrawSetFillColor($drawing_wand,$pixel_wand);

if (MagickAnnotateImage($magick_wand,$drawing_wand,0,0,0,"Rose") != 0)

{

MagickEchoImageBlob( $magick_wand );

}

else

{

echo MagickGetExceptionString($magick_wand);

}

?>

安装方法:

1. 下载 php_magickwand_q16_st.dll for 5.2.x

2. 将其放在PHP的扩展目录

3. 在php.ini文件总增加 extension=php_magickwand_q16_st.dll

4. 重新启动apache

php如何实现自动加水印

加水印逻辑有两种

一种是上传直接加水印

另一种是利用伪静态将图片访问重定向到处理程序,临时加水印缓存并输出

php处理图片加水印可以使用gd库中的相关函数

以下为临时手打代码,可以按此思路优化,有问题可以联系本人

//此处需根据上传的图片格式使用对应函数实例化图片

$img=imagecreatefromjpg($imgurl);

//根据水印图片路径实例化水印

$waterImg=imagecreatefrompng($waterpath);

//获取原图及水印图片尺寸,用以计算是否需要缩放及放置位置

list($width, $height, $type, $attr) = getimagesize($imgurl);

list($waterw, $waterh, $type, $attr) = getimagesize($waterpath);

$scale=1;

$waterReleaseW=$waterw;

$waterReleaseH=$waterh;

if($waterReleaseW>$width*.5){

    $scale=$width*.5/$waterw;

    $waterReleaseW = $width*.5;

    $waterReleaseH = $waterh*$scale;

}

if($waterReleaseH>$height*.5){

    $scale *= $height*.5/$waterh;

    $waterReleaseH = $height*.5;

    $waterReleaseW = $waterw*$scale;

}

//将水印图片拷贝到原图指定位置(此示例为右下角)

imagecopyresized($img,$waterImg,

    $width-$waterReleaseW-10,$height-$waterReleaseH-10,

    0,0,

    $width-10,$height-10,

    $waterw,$waterh);

//销毁水印图片实例

imagedestroy($waterImg);

//水印后图片保存

imagejpeg($img,$newpath);

谁有能用的php给图片加文字水印,最好有使用方法,注意:一定是能用的,功能强不强大另说

<?php

/*PHP图片加文字水印类库

QQ:3697578482 伤心的歌

该类库暂时只支持文字水印,位置为右下角,颜色随机

调用方法:

1、在需要加水印的文件顶部引入类库:

include_once 'imageClass.php';

2、声明新类:

$tpl=new image_fu;

3、给图片水印提供参数:

$tpl->img(图片路径,水印文字,字体路径,字体大小,字体角度);

比如:$tpl->img('abc.jpg','这是水印文字','ziti.ttf',30,0)

*/

class image_fu{

private $image;

private $img_info;

private $img_width;

private $img_height;

private $img_im;

private $img_text;

private $img_ttf='';

private $img_new;

private $img_text_size;

private $img_jd;

function img($img='',$txt='',$ttf='',$size=12,$jiaodu=0){

if(isset($img)file_exists($img)){//检测图片是否存在

$this->image =$img;

$this->img_text=$txt;

$this->img_text_size=$size;

$this->img_jd=$jiaodu;

if(file_exists($ttf)){

$this->img_ttf=$ttf;

}else{

exit('字体文件:'.$ttf.'不存在!');

}

$this->imgyesno();

}else{

exit('图片文件:'.$img.'不存在');

}

}

private function imgyesno(){

$this->img_info =getimagesize($this->image);

$this->img_width =$this->img_info[0];//图片宽

$this->img_height=$this->img_info[1];//图片高

//检测图片类型

switch($this->img_info[2]){

case 1:$this->img_im = imagecreatefromgif($this->image);break;

case 2:$this->img_im = imagecreatefromjpeg($this->image);break;

case 3:$this->img_im = imagecreatefrompng($this->image);break;

default:exit('图片格式不支持水印');

}

$this->img_text();

}

private function img_text(){

imagealphablending($this->img_im,true);

//设定颜色

$color=imagecolorallocate($this->img_im,rand(0,255),rand(0,255),rand(0,255));

$txt_height=$this->img_text_size;

$txt_jiaodu=$this->img_jd;

$ttf_im=imagettfbbox($txt_height,$txt_jiaodu,$this->img_ttf,$this->img_text);

$w = $ttf_im[2] - $ttf_im[6];

$h = $ttf_im[3] - $ttf_im[7];

//$w = $ttf_im[7];

//$h = $ttf_im[8];

unset($ttf_im);

$txt_y =$this->img_height-$h;

$txt_x =$this->img_width-$w;

//$txt_y =0;

//$txt_x =0;

$this->img_new=@imagettftext($this->img_im,$txt_height,$txt_jiaodu,$txt_x,$txt_y,$color,$this->img_ttf,$this->img_text);

@unlink($this->image);//删除图片

switch($this->img_info[2]) {//取得背景图片的格式

case 1:imagegif($this->img_im,$this->image);break;

case 2:imagejpeg($this->img_im,$this->image);break;

case 3:imagepng($this->img_im,$this->image);break;

default: exit('水印图片失败');

}

}

//显示图片

function img_show(){echo '<img src="'.$this->image.'" border="0" alt="'.$this->img_text.'" />';}

//释放内存

private function img_nothing(){

unset($this->img_info);

imagedestroy($this->img_im);

}

}

?>

关于php自动打水印的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。

查看更多关于php自动打水印 php print的详细内容...

声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did196053
更新时间:2023-04-26   阅读:28次

上一篇: php前后端分离框 php前后端分离和不分离的优劣

下一篇:php汇率rmb php货币转换

最新资料更新

  • 1.手机文件php怎么打开 手机php格式文件怎么打开
  • 2.phpu=13647的简单介绍
  • 3.搭建分站源码php 建立分站怎么建
  • 4.phpoutfile的简单介绍
  • 5.影视php解析api php解析vip视频
  • 6.php大牛交流教程 php大神
  • 7.php导出cvs php导出csv大数据
  • 8.php下面有哪些技术 php运用的技术php开发有哪些实用的技术
  • 9.csrfphp防的简单介绍
  • 10.phpmp3播放 php播放器本地视频
  • 11.php根据逗号分割 php分割文本
  • 12.php构造方法重写 php 构造方法
  • 13.php引用vendor php 引用
  • 14.php解压gz效率 rarphp文件怎么解压
  • 15.php物业台账公式 物业台账是什么意思
  • 16.iisphpma的简单介绍
  • 17.phpsql添加记录 php数据表里怎么添加数据
  • 18.怎么写php需求 php市场需求
  • 19.php跳转url源码 网页跳转源码
  • 20.php技术都有什么 php技术

CopyRight:2016-2025好得很程序员自学网 备案ICP:湘ICP备09009000号-16 http://www.haodehen.cn
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。

网站内容来源于网络分享,如有侵权发邮箱到:kenbest@126.com,收到邮件我们会即时下线处理。
网站框架支持:HDHCMS   51LA统计 百度统计
Copyright © 2018-2025 「好得很程序员自学网」
[ SiteMap ]