How to Resize Pngs With Transparency in PHP

How do I resize pngs with transparency in PHP?

From what I can tell, you need to set the blending mode to false, and the save alpha channel flag to true before you do the imagecolorallocatealpha()

<?php
/**
* https://stackoverflow.com/a/279310/470749
*
* @param resource $image
* @param int $newWidth
* @param int $newHeight
* @return resource
*/
public function getImageResized($image, int $newWidth, int $newHeight) {
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagealphablending($newImg, false);
imagesavealpha($newImg, true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $newWidth, $newHeight, $transparent);
$src_w = imagesx($image);
$src_h = imagesy($image);
imagecopyresampled($newImg, $image, 0, 0, 0, 0, $newWidth, $newHeight, $src_w, $src_h);
return $newImg;
}
?>

UPDATE : This code is working only on background transparent with opacity = 0. If your image have 0 < opacity < 100 it'll be black background.

PHP Resize image and save Transparent PNG

Sorry I have no experience in PHP but after I read this page, I think you may need add the transparent colour after you set the save alpha is true

imagesavealpha($image, true);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $color);

if still cannot try add this after imagecolorallocatealpha(),

imagecolortransparent($image, $color); 

How to keep transparent background on PNG when resizing with PHP?

I found another post with same question, according to that post following code can be used to preserve transparancy.

imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );

Reference: Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?

Please vote-up if it helps you. :)

Resize PNG without losing quality and transparency

For people with the same problem in the future. I solved it by stopped using imagegd and switched to ImageMagick. All images are still transparent and u can easily convert cmyk images to sRGB with the correct ICC profile

How to minimize and resize PNG images with transparency in php

You can use ImageMagick :

Simple example:

$inFile = "big_img.png";
$outFile = "small_img.png";
$image = new Imagick($inFile);
$image->thumbnailImage(256, 256);
$image->writeImage($outFile);

More info

PNG Transparency Resize with SimpleImage.php Class

Now have the transparency working for PNG but not gif. Here are the edits to the specific functions in case it will help someone else:

Save Function:

function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {

// do this or they'll all go to jpeg
$image_type=$this->image_type;

if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
// need this for transparent png to work
imagealphablending($this->image, false);
imagesavealpha($this->image,true);
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}

}

Resize Function

 function resize($width,$height,$forcesize='n') {

/* optional. if file is smaller, do not resize. */
if ($forcesize == 'n') {
if ($width > $this->getWidth() && $height > $this->getHeight()){
$width = $this->getWidth();
$height = $this->getHeight();
}
}

$new_image = imagecreatetruecolor($width, $height);
/* Check if this image is PNG or GIF, then set if Transparent*/
if(($this->image_type == IMAGETYPE_GIF) || ($this->image_type==IMAGETYPE_PNG)){
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
}
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());

$this->image = $new_image;

}



Related Topics



Leave a reply



Submit