诸位:
我将一幅JPG图像,通过setWater函数嵌入水印后(图片),像素数没变,但是总大小变小了很多(从1.4M变为350k左右)!!应该是某个函数影响了图像每个像素的丰富度或色彩变化度。。是imagecopy吗?还是imagecreatetruecolor?
请高人指点!!!!下边贴出嵌入水印函数:
//嵌入水印程序, 文字水印和图片水印请分别添加!!!
function setWater($imgSrc,$markImg,$markText,$TextColor,$markPos,$fontType,$markType,$fontSize)
{
/*
参数说明:
$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代表以文字方式添加水印
*/
$srcInfo = @getimagesize($imgSrc);
$srcImg_w = $srcInfo[0];
$srcImg_h = $srcInfo[1];
switch ($srcInfo[2])
{
case 1:
$srcim =@imagecreatefromgif($imgSrc);
if($srcim==false)
echo "打开失败!";
break;
case 2:
$srcim =@imagecreatefromjpeg($imgSrc);
if($srcim==false)
echo "打开失败!";
break;
case 3:
$srcim =@imagecreatefrompng($imgSrc);
if($srcim==false)
echo "打开失败!";
break;
default:
die("不支持的图片文件类型");
exit;
}
if(!strcmp($markType,"img"))
{
//print_r(" hello ! img");
if(!file_exists($markImg) || empty($markImg))
{
return;
}
$markImgInfo = @getimagesize($markImg);
$markImg_w = $markImgInfo[0];
$markImg_h = $markImgInfo[1];
if($srcImg_w {
return;
}
switch ($markImgInfo[2])
{
case 1:
$markim =@imagecreatefromgif($markImg);
if($markim==false)
echo "打开失败!";
break;
case 2:
$markim =@imagecreatefromjpeg($markImg);
if($markim==false)
echo "打开失败!";
break;
case 3:
$markim =@imagecreatefrompng($markImg);
if($markim==false)
echo "打开失败!";
break;
default:
die("不支持的水印图片文件类型");
exit;
}
$logow = $markImg_w;
$logoh = $markImg_h;
}
if(!strcmp($markType,"text"))
{
if(!empty($markText))
{
if(!file_exists($fontType))
{
//print_r(" hello ! no fonttype");
return;
}
}
else
{
//print_r(" hello ! no marktext");
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 = +25;
$y = +95;
break;
case 2:
$x = ($srcImg_w - $logow) / 2;
$y = +5;
break;
case 3:
$x = $srcImg_w - $logow - 105;
$y = +85;
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"))
{
//print_r(" hello ! text2");
$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:
//print_r("hello!");
imagejpeg($dst_img, $imgSrc);
break;
case 3:
imagepng($dst_img, $imgSrc);
break;
default:
die("不支持的水印图片文件类型");
exit;
}
imagedestroy($dst_img);
imagedestroy($srcim);
}
回复讨论(解决方案)
imagejpeg
第三个参数设置的图片的质量,默认不是最佳。是不是这个原因导致的呢?
是的,多谢指教!网上找的函数,没有逐个函数推敲,只管能用就可以了。多谢多谢!

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


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

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
