search
HomeBackend DevelopmentPHP Tutorial 解决PHP剪切缩略图生成png,gif透明图时,黑色背景有关问题

解决PHP剪切缩略图生成png,gif透明图时,黑色背景问题

最近做头像上传用到剪切,只要GIF或者PNG是透明的话,剪切后都会变成黑色的背景图。

?

解决方案有2种:

?

1.背景图填充白色的背景。

?

$white = imagecolorallocate($dstim,255,255,255);
imagefilledrectangle($dstim,0,0,$width,$height,$white);
imagecolortransparent($dstim,$white);

?

2.设置图片走透明通道。

?

$img = imagecreatefrompng($src);
imagesavealpha($img,true);//这里很重要;
$thumb = imagecreatetruecolor(300,300);
imagealphablending($thumb,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
imagesavealpha($thumb,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
imagecopyresampled($thumb,$img,0,0,0,0,300,300,300,300);
imagepng($thumb,"temp.png");

?

以上2种方式均测试成功。

Statement
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
thumb是什么文件thumb是什么文件Sep 26, 2021 pm 02:02 PM

thumb是指系统隐藏缓存文件。thumb是Windows为了提高文件夹在缩略图查看方式下的响应速度而对当前文件夹下的图像文件建立的缓存,它可以方便用户对图片进行预览,删除与禁用不会对系统造成影响。

Lenovo refreshes Legion Go with new colourful customisation optionsLenovo refreshes Legion Go with new colourful customisation optionsSep 07, 2024 am 09:33 AM

Lenovo has been spotted pulling the Legion Go (curr. $589 on Amazon) from many of its European stores. Incidentally, the company recently introduced various accessories for its first gaming handheld, full details of which we have covered separately.

Brits have been given the chance to secure a small fortune in their advent calendar this ChristmasBrits have been given the chance to secure a small fortune in their advent calendar this ChristmasOct 15, 2024 pm 10:26 PM

Hidden inside one particular countdown to the big day will be a one of a kind hidden coin, worth a whopping £1,355.

Brits urged to 'get checking' at home for unusual but very valuable coin that could be worth more than £1,000Brits urged to 'get checking' at home for unusual but very valuable coin that could be worth more than £1,000Oct 11, 2024 pm 04:38 PM

The 20p coin in question could be extremely distinctive, due to the fact it has been printed on the wrong type of material

Ethereum Finds Support At $2,300: Over 52 Million ETH BoughtEthereum Finds Support At $2,300: Over 52 Million ETH BoughtOct 12, 2024 pm 12:02 PM

The second world's most valuable coin is bearish, dumping by over 50% from July highs and unable to break the local resistance at $3,500.

Ethereum (ETH) Price Analysis: ETH Failed to Breach $3,000, Targeting $2,300 If the Crash ContinuesEthereum (ETH) Price Analysis: ETH Failed to Breach $3,000, Targeting $2,300 If the Crash ContinuesAug 29, 2024 am 03:28 AM

Ethereum's recent attempts to break through the $3,000 mark have been unsuccessful, keeping it tied closely to Bitcoin's price movements.

Brits urged to rummage through their change after TikTok coin expert reveals rare 20p piece could be worth a staggering £1,300Brits urged to rummage through their change after TikTok coin expert reveals rare 20p piece could be worth a staggering £1,300Oct 11, 2024 pm 10:02 PM

The unusual coin is made of copper and was mistakenly struck on the wrong material.

Ethereum (ETH) Gas Fees Hit Five-Year Lows, Here's What It Means for the PriceEthereum (ETH) Gas Fees Hit Five-Year Lows, Here's What It Means for the PriceAug 20, 2024 am 03:22 AM

Amid this ongoing market downturn, recently Ethereum (ETH) is gaining significant attention from the crypto community.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

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),

Safe Exam Browser

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.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version