Png Transparency Resize with Simpleimage.PHP Class

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;

}

Fix png transparency problem

I don't beleive that this class can handle transparent PNGs. The PHP documentation on imagecopyresampled explains this.

You'll probably need to extend the class to use imagecolorallocatealpha.

Read for reference: http://php.net/manual/en/function.imagecreatetruecolor.php

imagecopyresampled png black background

You are transforming a PNG file with transparancy into a JPEG, that has no support for transparancy.

Even though your save function had an option for PNG files, you are not using it.

$image->save($img1name);

Runs the following function.

 function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
}
elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
}
elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}

But you never give the second parameter. Therefor it defaults to a JPEG file. If you would use $image->save($img1name, IMAGETYPE_PNG); it is likely to work.

If you want all images in PNG, you could just change your save function as follows:

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

Change php img resizer class to prevent upscaling/pixelating images (by adding whitespace)

You're on the right track. Put a conditional check into the resizeToWidth to check if you're resizing up.

In the resize method, if you want to pad, then first create a new image of the new, larger dimensions, then figure out the center point ( ( new_image_width - old_image_width ) / 2 ) and to an imagecopy to that new location.



Related Topics



Leave a reply



Submit