Get the App
SLTechnology News&Howtos  ›  Development  › 

How to reduce png Picture without losing transparent Color by php

Shulou Source: shulou.com Published: 2022-06-03 15:43:12 10月01日 Update

This article mainly explains the "php how to achieve reduction of png pictures without loss of transparent color", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "php how to achieve the reduction of png pictures do not lose transparent color" it!

There are two main ways to use the gd library:

The copy code is as follows:

Imagecolorallocatealpha / / assign colors + alpha

Imagesavealpha / / set to save complete alpha channel information when saving png images

Code example:

The copy code is as follows:

/ / get the source image gd image identifier

$srcImg = imagecreatefrompng ('. / src.png')

$srcWidth = imagesx ($srcImg)

$srcHeight = imagesy ($srcImg)

/ / create a new diagram

$newWidth = round ($srcWidth / 2)

$newHeight = round ($srcHeight / 2)

$newImg = imagecreatetruecolor ($newWidth, $newHeight)

/ / assign color + alpha to fill the new image with color

$alpha = imagecolorallocatealpha ($newImg, 0,0,0127)

Imagefill ($newImg, 0,0, $alpha)

/ / copy the source image to the new image, and set to save the complete alpha channel information when saving the PNG image.

Imagecopyresampled ($newImg, $srcImg, 0,0,0,0, $newWidth, $newHeight, $srcWidth, $srcHeight)

Imagesavealpha ($newImg, true)

Imagepng ($newImg,'. / dst.png')

Thank you for your reading, the above is the "php how to achieve reduction of png pictures without loss of transparent color" of the content, after the study of this article, I believe you on how to achieve php reduction of png pictures without loss of transparent color of this problem has a deeper understanding, the specific use of the situation also need to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Code picture loss image color new picture learning information content channel source map assignment two namely idea situation copy article method more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple Linux macOS Docker