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

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

Jun 13, 2016 pm 12:48 PM
300 thumb White

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

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

?

解决方案有2种:

?

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

?

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

?

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");
Copy after login

?

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

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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What file is thumb What file is thumb Sep 26, 2021 pm 02:02 PM

Thumb refers to the system hidden cache file. Thumb is a cache created by Windows for the image files in the current folder in order to improve the response speed of the folder in thumbnail viewing mode. It can facilitate users to preview images. Deletion and disabling will not affect the system.

Lenovo refreshes Legion Go with new colourful customisation options Lenovo refreshes Legion Go with new colourful customisation options Sep 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 Christmas Brits have been given the chance to secure a small fortune in their advent calendar this Christmas Oct 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,000 Brits urged to 'get checking' at home for unusual but very valuable coin that could be worth more than £1,000 Oct 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 Bought Ethereum Finds Support At $2,300: Over 52 Million ETH Bought Oct 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 Continues Ethereum (ETH) Price Analysis: ETH Failed to Breach $3,000, Targeting $2,300 If the Crash Continues Aug 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.

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

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

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

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

See all articles