php实现给一张图片加上水印效果
<?php /** * 功能:给一张图片加上水印效果 * $i 要加水印效果的图片 * $t 水印文字 * $size 文字大小 * $pos 水印的位置 * $color 文字的颜色 * $flag 是布尔值,主要用来区分是不是原图上加水印 * $type 如果$flag等于false 则新图上加上水印 新文件名为 原名_txt.jpg */ function txt($i,$t='版权所有',$size=25,$pos=5,$color='rand',$flag=true,$type='_txt'){ $img = imagecreatefromjpeg($i); $w = imagesx($img); $h = imagesy($img); $font = dirname(__FILE__).'/font/ls.ttf'; $ps = imagettfbbox($size,0,$font,$t); $tw = $ps[4]; $th = abs($ps[5]); switch($pos){ case 1:break; case 2:break; case 3:break; case 4:break; case 5:$x=($w-$tw)/2;$y=($h-$th)/2+$th;break; case 6:break; case 7:break; case 8:break; case 9:break; default:break; } $c = getcolor($img,$color); imagettftext($img,$size,0,$x,$y,$c,$font,$t); if($flag){ imagejpeg($img,$i); }else{ $ext = ext($i); $ppp = rtrim($i,'.'.$ext); $ppp = $ppp.$type.'.'.$ext; imagejpeg($img,$ppp); } } function getcolor($i,$c='rand',$a=50){ $cc = ''; switch($c){ case 'white':$cc=imagecolorallocatealpha($i,255,255,255,$a);break; case 'black':$cc=imagecolorallocatealpha($i,0,0,0,$a);break; case 'red':$cc=imagecolorallocatealpha($i,255,0,0,$a);break; case 'green':$cc=imagecolorallocatealpha($i,0,255,0,$a);break; case 'blue':$cc=imagecolorallocatealpha($i,0,0,255,$a);break; case 'orange':$cc=imagecolorallocatealpha($i,0xff,0x66,0x33,$a);break; case 'yellow':$cc=imagecolorallocatealpha($i,255,255,0,$a);break; case 'rand':$cc=imagecolorallocatealpha($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),$a);break; default: $cs = substr($c,1); $ok = str_split($cs,2); $cc = imagecolorallocatealpha($i,hexdec($ok[0]),hexdec($ok[1]),hexdec($ok[2]),$a); break; } return $cc; } /** * 功能是:返回扩展名 */ function ext($f){ $exts = explode('.',$f); return end($exts); } /** * 功能是:返回文件名,不含扩展名 */ function name($f){ $s = explode('/',$f); $fn = end($s); return rtrim($fn,'.'.ext($f)); }
我们再来看一个支持以图片和文字两种方式给图片添加水印。图片支持GIF,PNG,JPG三种格式,水印图片支持PNG和GIF
function setWater($imgSrc,$markImg,$markText,$TextColor,$markPos,$fontType,$markType) { $srcInfo = @getimagesize($imgSrc); $srcImg_w = $srcInfo[0]; $srcImg_h = $srcInfo[1]; switch ($srcInfo[2]) { case 1: $srcim =imagecreatefromgif($imgSrc); break; case 2: $srcim =imagecreatefromjpeg($imgSrc); break; case 3: $srcim =imagecreatefrompng($imgSrc); break; default: die("不支持的图片文件类型"); exit; } if(!strcmp($markType,"img")) { if(!file_exists($markImg) || empty($markImg)) { return; } $markImgInfo = @getimagesize($markImg); $markImg_w = $markImgInfo[0]; $markImg_h = $markImgInfo[1]; if($srcImg_w < $markImg_w || $srcImg_h < $markImg_h) { return; } switch ($markImgInfo[2]) { case 1: $markim =imagecreatefromgif($markImg); break; case 2: $markim =imagecreatefromjpeg($markImg); break; case 3: $markim =imagecreatefrompng($markImg); break; default: die("不支持的水印图片文件类型"); exit; } $logow = $markImg_w; $logoh = $markImg_h; } if(!strcmp($markType,"text")) { $fontSize = 16; if(!empty($markText)) { if(!file_exists($fontType)) { return; } } else { return; } $box = @imagettfbbox($fontSize, 0, $fontType,$markText); $logow = max($box[2], $box[4]) - min($box[0], $box[6]); $logoh = max($box[1], $box[3]) - min($box[5], $box[7]); } if($markPos == 0) { $markPos = rand(1, 9); } switch($markPos) { case 1: $x = +5; $y = +5; break; case 2: $x = ($srcImg_w - $logow) / 2; $y = +5; break; case 3: $x = $srcImg_w - $logow - 5; $y = +15; break; case 4: $x = +5; $y = ($srcImg_h - $logoh) / 2; break; case 5: $x = ($srcImg_w - $logow) / 2; $y = ($srcImg_h - $logoh) / 2; break; case 6: $x = $srcImg_w - $logow - 5; $y = ($srcImg_h - $logoh) / 2; break; case 7: $x = +5; $y = $srcImg_h - $logoh - 5; break; case 8: $x = ($srcImg_w - $logow) / 2; $y = $srcImg_h - $logoh - 5; break; case 9: $x = $srcImg_w - $logow - 5; $y = $srcImg_h - $logoh -5; break; default: die("此位置不支持"); exit; } $dst_img = @imagecreatetruecolor($srcImg_w, $srcImg_h); imagecopy ( $dst_img, $srcim, 0, 0, 0, 0, $srcImg_w, $srcImg_h); if(!strcmp($markType,"img")) { imagecopy($dst_img, $markim, $x, $y, 0, 0, $logow, $logoh); imagedestroy($markim); } if(!strcmp($markType,"text")) { $rgb = explode(',', $TextColor); $color = imagecolorallocate($dst_img, $rgb[0], $rgb[1], $rgb[2]); imagettftext($dst_img, $fontSize, 0, $x, $y, $color, $fontType,$markText); } switch ($srcInfo[2]) { case 1: imagegif($dst_img, $imgSrc); break; case 2: imagejpeg($dst_img, $imgSrc); break; case 3: imagepng($dst_img, $imgSrc); break; default: die("不支持的水印图片文件类型"); exit; } imagedestroy($dst_img); imagedestroy($srcim); }
参数说明:
$imgSrc:目标图片,可带相对目录地址,
$markImg:水印图片,可带相对目录地址,支持PNG和GIF两种格式,如水印图片在执行文件mark目录下,可写成:mark/mark.gif
$markText:给图片添加的水印文字
$TextColor:水印文字的字体颜色
$markPos:图片水印添加的位置,取值范围:0~9
0:随机位置,在1~8之间随机选取一个位置
1:顶部居左 2:顶部居中 3:顶部居右 4:左边居中
5:图片中心 6:右边居中 7:底部居左 8:底部居中 9:底部居右
$fontType:具体的字体库,可带相对目录地址
$markType:图片添加水印的方式,img代表以图片方式,text代表以文字方式添加水印
代码注释:
第4~6行:获取目标图片的宽度和高度
第8~22行:根据图片类型调用不同的函数,获得操作图像标识符
GetImageSize函数知识点:GetImageSize不需要安装 GD度就可使用,其返回值数组有四个元素。索引值0是图片高度。索引值1是图片的宽度。索引值2是图片的文件格式,其值1为GIF格式、2为JPEG/JPG格式、3为PNG格式。索引值3为图片的高与宽字符串,height=xxx width=yyy。返回的图片宽度和高度单位都是像素(pixel)
第24~58行:当选择图片方式给目标图片添加水印时,获取水印图片的宽度和高度,通常情况都是网站的logo。如果目标图片比水印图片宽度或者高度小或者水印图片不存在,则跳出这个函数。
return语句知识点:直接return 表示什么都不返回,直接结束这个函数。也可以理解成返回 NULL。
第60~77行:当选择文字方式给目标图片添加水印时,首先设定水印文字的大小,默认我设置为16px,你可以根据需要自行调整字体大小。如果字体文件不存在,跳出函数,最后通过imagettfbbox函数获得此设定格式的文字的虚拟长宽。
imagettfbbox函数知识点:此函数返回一个含有8个单元的数组表示文本外框的四个角,索引值含义:0代表左下角 X 位置,1代表坐下角 Y 位置,2代表右下角 X 位置,3代表右下角 Y 位置,4代表右上角 X 位置,5代表右上角 Y 位置,6代表左上角 X 位置,7代表左上角 Y 位置。此函数同时需要GD 库和FreeType库的支持
max函数返回参数中数值最大的值。
第79~125行:根据设定的图片水印位置计算具体坐标值,你可以根据效果具体细化水印的位置。
第127~129行:新建一个和目标图片大小一致的图片。
注:由于imagecreatetruecolor函数范围的是一个黑色图片,所以如果你的目标图片是透明的,则生成的新图将不会是透明色。
第131~162行:根据图片或者文字方式,最终生成添加了水印的图片。
调用说明:
以函数调用方式调用即可,当然你也可以以类的方式封装,或者你也可以根据需要将此函数进一步细分模块也可以。当然你现在这样用也是没有任何问题的,我已测试过,请放心使用。
其他说明:
由于imagettftext和imagettfbbox函数需要GD库和FreeType库的支持,如果你的运行环境不支持GD库和FreeType库则文字方式就无法实现,你可以用imagestring函数实现给图片添加文字水印,同时设定下text方式下的$logow和$logoh值即可。
imagejpeg函数也可以设置合成的图片质量。

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools