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
Creating a File Progress Bar in PHP
Upload File Using Guzzle 6 to API Endpoint
Setting PHP Enviromental Variable While Running Command Line Script
Check and Return Duplicates Array PHP
Calling a Stored Procedure from Codeigniter's Active Record Class
Zend Framework - Multiplate Navigation Blocks
Call to Undefined Function Curl_Init() - with Wamp
Count How Often a Particular Value Appears in an Array
"Call to Undefined Function" Error When Calling Class Method
Should a Function Use: Return Null;
Changes to Upload_Max_Filesize in Ubuntu PHP.Ini Will Not Take Effect
How to Get the Execution Time of a MySQL Query from PHP
How to Deal with "Method Not Found in Class" Warning for Magically Implemented Methods
PHP Code Inside a Laravel 5 Blade Template
What Is the Best Method for Getting a Database Connection/Object into a Function in PHP
How to Disable the Back Browser Button After User Press Logout and Destroy Session