Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php可以去除jpg格式图片的背景并加水印吗?

php可以去除jpg格式图片的背景并加水印吗?

Jun 23, 2016 pm 02:38 PM

从网上找到了这段,但我运行并不能实现,而且这个是针对png和gif格式的
$fnew = "a.png";
$img = file_get_contents($fnew);
$im = imagecreatefromstring($img);
$bg = imagecolorat($im, 0, 0);
imagecolorset($im, $bg, 0, 0, 255);
imagepng($im);
imagedestroy($im);
请问大家有好方法吗?谢谢


回复讨论(解决方案)


假如这张图片,我想把后面的各种蓝色背景变成白色,然后上面的苹果图案整体缩小可以缩小网上挪动一下,我可以在最下面加上黑色文字水印,把图案往上挪是因为给水印留位置,别遮挡住这段文字,谢谢。

加水印是可以的

但是去背景都可以的话,拿PS来干什么?

那如果背景是单一颜色呢,是否可以?

bool imagefill ( resource image, int x, int y, int color )


imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。


那如果背景是单一颜色呢,是否可以?

function addBg($src,$w,$h)
    {
        $bg = imagecreatetruecolor($w,$h);
        $white = imagecolorallocate($bg,255,255,255);
        imagefill($bg,0,0,$white);//填充背景
 
        //获取目标图片信息
        $info=getimagesize($src);
        $width=$info[0];//目标图片宽度
        $height=$info[1];//目标图片高度
        switch ($info[2]){
            case 1:
                $img = imagecreatefromgif($src);
            break;
            case 2:
                $img = imagecreatefromjpeg($src);
                break;
            case 3:
                $img = imagecreatefrompng($src);
                break;
            default:
                exit('不支持的图像格式');
            break;
        }
        if($height          {
            $x=0;
            $y=($h-$height)/2;//垂直居中
        }
        if($width          {
            $x=($w-$width)/2;//水平居中
            $y=0;
        }
        if($height              $x = ($w-$width)/2;
            $y = ($h-$height)/2;
        }
        imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);
        imagejpeg($bg,$src,100);
        imagedestroy($bg);
        imagedestroy($img);
        return $src;
    }
我代入进去没有任何效果啊
addBg("97972188.jpg",$pieces[0],$pieces[1]);

加水印很简单……去背景比较复杂……


加水印我已经实现了,主要是把背景去掉,背景是单一颜色能去掉也行,假如是灰色,变成白色底就行。谢谢各位高手了

我对图像操作不是很熟悉。不过可以给一点我的想法:

如果是单一背景,那么我建议你创建一个透明的png可能更快捷,我记得在创建png时可以指定一个颜色为透明色的。

我找到一段代码,应该感觉比较像,但怎么改还是没有头绪,应该怎么改好呢,请高手指教,谢谢
$o_pic = '97972188.jpg';

//图像中要处理的色阶
$begin_r = 178;
$begin_g = 178;
$begin_b = 178;

list($src_w,$src_h,$src_type) = getimagesize($o_pic);// 获取原图像信息

$file_ext = get_ext($o_pic);//获取扩展名
$target_im = imagecreatetruecolor($src_w,$src_h);//新图


if($file_ext == 'jpg') //转换JPG 开始
{
    $src_im = ImageCreateFromJPEG($o_pic);
    echo $src_w;
    imagecopymerge($target_im,$src_im,0,0,0,0,$src_w,$src_h,100);

    for($x = 0; $x      {
        for($y = 0; $y          {
            $rgb = imagecolorat($src_im, $x, $y);

            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            //将开始设定的色阶值改为白色
            if($r > $begin_r && $g > $begin_g && $b > $begin_b ){   
                imagecolortransparent($target_im, imagecolorallocate($target_im,$r, $g, $b));                
            }
        }
    }

}
header("Content: image/jpeg"); 
imagejpeg($target_im,'c.jpg'); 
imagedestroy($target_im); 

这个方法会不会太慢?我看到是一个像素一个像素循环的。

http://stackoverflow.com/questions/16660729/php-change-background-color-to-transparent

This maybe help. 

Well, it is more or less the same code as you posted. 

有效果,但效果还是不行,毛边太多,图片周围还是有些颜色没有去掉

有些颜色看来是相近的,不是绝对一样,只是一般肉眼无法区别,这样类似的颜色那就不好控制了

有些颜色看来是相近的,不是绝对一样,只是一般肉眼无法区别,这样类似的颜色那就不好控制了

那恐怕是没有办法了,除非你人工修。

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 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...

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...

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