PHP-generated thumbnail classes that support PNG transparent images share _php tips

Source: Internet
Author: User

Note: This feature relies on the GD2 graphics library

Recently to use PHP to generate thumbnails, looked up on the internet, found this article: PHP generated thumbnail images

After a trial, there are several problems:

1, PNG image generated thumbnail is jpg format

2, the PNG image generated by the thumbnail does not have a transparent (translucent) effect (filled with a black background)

3, the code syntax is relatively old

So, on the basis of this version, simple modifications are optimized.

PHP Generate thumbnail class

<?php/* desc:resize Image (PNG, JPG, GIF) * Author: Ten years later, Lu Brother date:2014.11.13 * * Class Resizeimage
    {//Picture type private $type;
    Actual width private $width;
    The actual height of private $height;
    The width of the change after the private $resize _width;
    After changing the height of the private $resize _height;
    Whether the drawing is private $cut;
    source image private $srcimg;
    Target image address private $dstimg;

    A temporary image created by private $im;
      function __construct ($imgPath, $width, $height, $isCut, $savePath) {$this->srcimg = $imgPath;
      $this->resize_width = $width;
      $this->resize_height = $height;
      $this->cut = $isCut;

      Type of picture $this->type = Strtolower (substr (STRRCHR ($this->srcimg, "."), 1));
      Initialization of Image $this->initi_img ();
      The target image address $this-> dst_img ($savePath);
      --$this->width = Imagesx ($this->im);
      $this->height = Imagesy ($this->im);
      Generating Image $this->newimg (); Imagedestroy ($thiS-&GT;IM); Private Function Newimg () {//The proportions of the altered image $resize _ratio = ($this->resize_width)/($this->resize_heig
      HT);
      The proportions of the actual image $ratio = ($this->width)/($this->height);
        if ($this->cut) {//Map $newimg = Imagecreatetruecolor ($this->resize_width, $this->resize_height);
        if ($this->type== "png") {Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127)); } if ($ratio >= $resize _ratio) {//High priority imagecopyresampled ($newimg, $this->im, 0, 0,
        0, 0, $this->resize_width, $this->resize_height, (($this->height) * $resize _ratio), $this->height); else {//Width first imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this-&G
        T;resize_height, $this->width, (($this->width)/$resize _ratio)); } else {//Do not map if ($ratio >= $resize _ratio) {$newimg = ImagecreateTrueColor ($this->resize_width, ($this->resize_width)/$ratio);
          if ($this->type== "png") {Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127)); Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$rat
        Io, $this->width, $this->height);
          else {$newimg = Imagecreatetruecolor ($this->resize_height) * $ratio, $this->resize_height);
          if ($this->type== "png") {Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127)); Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height) * $ratio, $this->resize_he
        ight, $this->width, $this->height);
        } if ($this->type== "png") {Imagesavealpha ($newimg, true);
      Imagepng ($newimg, $this->dstimg);
      else {imagejpeg ($newimg, $this->dstimg);
    }//Initialize imagePrivate Function initi_img () {if ($this->type== "jpg") {$this->im = imagecreatefromjpeg ($this->srci
      MG);
      } if ($this->type== "gif") {$this->im = imagecreatefromgif ($this->srcimg);
      } if ($this->type== "png") {$this->im = imagecreatefrompng ($this->srcimg);

      }//Image Destination Address private function dst_img ($dstpath) {$full _length = strlen ($this->srcimg);
      $type _length = strlen ($this->type);


      $name _length = $full _length-$type _length;
      $name = substr ($this->srcimg,0, $name _length-1);
    $this->dstimg = $dstpath;

 }}?>

Use

When used, the constructor of the class is called directly, and the constructor functions as follows:

$resizeimage = new Resizeimage ($imgPath, $width, $height, $isCut, $savePath);

Parameters
$imgPath: Original picture address

$width: Thumbnail width

$height: Thumbnail height

$isCut: Is trimmed, bool value

$savePath: Thumbnail address (can be the same as the original picture address)

Example

<?php
  include "resizeimage.php";

  JPG
  $jpgResize = new Resizeimage ("Img/test_1920_1200.jpg", "img/test_320_240.jpg");

  PNG
  $pngResize = new Resizeimage ("Img/test_1024_746.png", "img/test_320_240.png");

>

Effect

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.