Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php使用gd库函数嵌入水印后,图像变小了

php使用gd库函数嵌入水印后,图像变小了

Jun 23, 2016 pm 02:16 PM

诸位:
     我将一幅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 
第三个参数设置的图片的质量,默认不是最佳。是不是这个原因导致的呢?

是的,多谢指教!网上找的函数,没有逐个函数推敲,只管能用就可以了。多谢多谢!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles