PHP's method of scaling PNG images (supports transparent backgrounds), PNG zoom _php Tutorial

Source: Internet
Author: User
Tags transparent color

PHP implements the method of scaling PNG images (supports transparent backgrounds), PNG zoom


The example in this paper describes how PHP implements scaling of PNG images. Share to everyone for your reference. The implementation method is as follows:

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 _col        or[' 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 color for PNGs that don ' t has one allocated already elseif ($info [2] = = IMA        Getype_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;

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1032795.html www.bkjia.com true http://www.bkjia.com/PHPjc/1032795.html techarticle PHP implements a method for zooming PNG images (supports transparent backgrounds), PNG scaling This article describes how PHP implements scaling of PNG images. Share to everyone for your reference. Concrete Reality ...

  • Related Article

    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.