PHP Scales the PNG image to support a transparent background

Source: Internet
Author: User
Tags transparent color

PHP Scales the PNG image to support a transparent background

function Smart_resize_image ($file, $width = 0, $height = 0, $proportional = false, $output = ' file ', $delete _original = t Rue, $use _linux_commands = False)

{

if ($height <= 0 && $width <= 0) {

return false;

}

$info = getimagesize ($file);

$image = ';

$final _width = 0;

$final _height = 0;

List ($width _old, $height _old) = $info;

if ($proportional) {

if ($width = = 0) $factor = $height/$height _old;

ElseIf ($height = = 0) $factor = $width/$width _old;

else $factor = min ($width/$width _old, $height/$height _old);

$final _width = Round ($width _old * $factor);

$final _height = Round ($height _old * $factor);

}

else {

$final _width = ($width <= 0)? $width _old: $width;

$final _height = ($height <= 0)? $height _old: $height;

}

Switch ($info [2]) {

Case Imagetype_gif:

$image = Imagecreatefromgif ($file);

Break

Case IMAGETYPE_JPEG:

$image = Imagecreatefromjpeg ($file);

Break

Case Imagetype_png:

$image = Imagecreatefrompng ($file);

Break

Default

return false;

}

$image _resized = Imagecreatetruecolor ($final _width, $final _height);

if ($info [2] = = Imagetype_gif) | | ($info [2] = = Imagetype_png)) {

$trnprt _indx = imagecolortransparent ($image);

If we have a specific transparent color

if ($trnprt _indx >= 0) {

Get the original image ' s transparent color ' s RGB values

$trnprt _color = Imagecolorsforindex ($image, $trnprt _indx);

Allocate the same color in the new image resource

$trnprt _indx = imagecolorallocate ($image _resized, $trnprt _color[' red '), $trnprt _color[' green ', $trnprt _color[' Blue ']);

Completely fill the background of the new image with allocated color.

Imagefill ($image _resized, 0, 0, $trnprt _indx);

Set the background color for new image to Transparent

Imagecolortransparent ($image _resized, $trnprt _indx);

}

Always make a transparent background the color for PNGs that don ' t have one allocated already

ElseIf ($info [2] = = Imagetype_png) {

Turn off transparency Blending (temporarily)

Imagealphablending ($image _resized, false);

Create a new transparent color for image

$color = Imagecolorallocatealpha ($image _resized, 0, 0, 0, 127);

Completely fill the background of the new image with allocated color.

Imagefill ($image _resized, 0, 0, $color);

Restore Transparency Blending

Imagesavealpha ($image _resized, true);

}

}

Imagecopyresampled ($image _resized, $image, 0, 0, 0, 0, $final _width, $final _height, $width _old, $height _old);

if ($delete _original) {

if ($use _linux_commands)

EXEC (' rm '. $file);

Else

@unlink ($file);

}

Switch (Strtolower ($output)) {

Case ' Browser ':

$mime = Image_type_to_mime_type ($info [2]);

Header ("Content-type: $mime");

$output = NULL;

Break

Case ' file ':

$output = $file;

Break

Case ' return ':

return $image _resized;

Break

Default

Break

}

Switch ($info [2]) {

Case Imagetype_gif:

Imagegif ($image _resized, $output);

Break

Case IMAGETYPE_JPEG:

Imagejpeg ($image _resized, $output);

Break

Case Imagetype_png:

Imagepng ($image _resized, $output);

Break

Default

return false;

}

return true;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.