![]() I created a PHP function to extract the alpha channel from an image, and then to apply that alpha channel to another image. Imagetruecolortopalette($resizedImg, true, 250) Imagecopyresampled($resizedImg, $src, 0, 0, 0, 0, $wNew, $hNew, $w, $h) $resizedImg = imagecreatetruecolor($wNew, $hNew) $alpha = imagescale($alpha,$wNew,$hNew,IMG_NEAREST_NEIGHBOUR) Resize alpha to match resized source image Extract the alpha and save as greyscale for inspection printf("DEBUG: Replacing transparency at %d,%d\n",$x,$y) Printf("DEBUG: Target image is truecolour\n") ![]() If output image is truecolour, we can set alpha 0.127 Ensure all the lovely new alpha we create will be saved when written to PNG iterate over pixels replacing transparent ones with allocated transparent colour allocate transparent colour in palette // find a transparent colour in the palette iterate over pixels getting current color and just replacing alpha component I had a a bit more time to work out the full code to answer you - I have simplified what you had quite considerably and it seems to do what I think you want now! #!/usr/bin/php -f Also some pixel at the edge of the duck are converted in black pixel or in full-transparent pixel. ![]() It makes completely transparent all pixels with an alpha, also if this alpha is very low, see the shadow below the penguin. Only if the image has only pixels already completely transparent does it work very well (e.g. There are visible color banding in the penguin but the duck its ok (although sometimes the color tone is darker). Note: I put an underlying grid to better show the alpha. Let me do some tests with your responses, maybe there is a solution with only PHP. In phpinfo() results that the imagemagick module isn't installed. Unfortunately, I can't use ImageMagick because my code is in a shared hosting without it installed. In GIMP v.2.8.22 I can save an image for Web with these properties: PNG-8ĭither: Floyd-Steinberg / Floyd-Steinberg 2 / positionedĪnd it produce a reduced image almost identical of the original.Īlso pngquant, tinypng, and many others do the same work, but I need to do it with PHP. How can I make it work and to understand the reason for this strange behaviour? Imagecolortransparent($resizedImg, imagecolorat($resizedImg,0,0)) Īlso: others SO posts and some on the WebĪlso without resizing the image (removing imagecopyresampled and adapting the variables name) the result is the same. If(!imagepng($resizedImg, ($newPath) ?: null, 8)) return false Imagetruecolortopalette($resizedImg, true, 255) If(!imagecopyresampled($resizedImg, $originalImg, 0, 0, 0, 0, $xImgNew, $yImgNew, $xImg, $yImg)) return false copy content from originalImg to resizedImg If(!$resizedImg = imagecreatetruecolor($xImgNew, $yImgNew)) return false If(!$originalImg = imagecreatefrompng($originalPath)) return false If(!trim($originalPath) || !$xyOriginalPath = getimagesize("$originalPath")) return false The complete function's code: function resizePng($originalPath, $xImgNew='', $yImgNew='', $newPath='') If I set 256 colors instead of 255: imagetruecolortopalette($resizedImg, true, 256) Ī similar result occurs with this image (note the half transparency for the case with 255 colors): ![]() The resulting image is this, with the transparency all around and a little inside the image: If(!imagefill($resizedImg, 0, 0, $transparent)) return false $transparent = imagecolorallocatealpha($resizedImg, 255, 255, 255, 127) The problem is when I convert the image in 8 bit: imagetruecolortopalette($resizedImg, true, 255) The resize works perfectly, preserving also image transparency: So the size of the resulting file will be smaller but without too much quality loss. One of GIMP's strengths is its free availability from many sources for many operating systems.I'm trying to write a PHP script that resizes a PNG image and then converts it to PNG-8 bit mode. The advanced scripting interface allows everything from the simplest task to the most complex image manipulation procedures to be easily scripted. GIMP is amazingly expandable and extensible – it is designed to be augmented with plug-ins and extensions to do just about anything. It can be used as a simple paint program, an expert quality photo retouching program, an online batch processing system, a mass production image renderer, an image format converter, and so much more. Like Photoshop, GIMP is suitable for a variety of image manipulation tasks, including photo retouching, image composition, image construction, and has many other capabilities. GIMP is a digital photo manipulation tool for Windows (and many other platforms) that’s considered to be the open source (free) answer to Adobe Photoshop. ![]()
0 Comments
Leave a Reply. |