GD2 Graphics library based php generation picture thumbnail class code sharing _php Tips

Source: Internet
Author: User
Tags imagejpeg php class php server

To use PHP to generate thumbnail images, make sure your PHP server has a GD2 graphics library that uses a class to generate thumbnail images of the pictures

1. How to use

$resizeimage = new Resizeimage ("Picture source file Address", "200", "100", "0", "thumbnail address");
Just use the words above, you can generate thumbnails, where the source file and thumbnail address can be the same, 200,100 representing width and height

2. Thumbnail Class Code

Use the following class to generate picture thumbnails, <?php class Resizeimage {//Picture type var $type;
  Actual width var $width;
  The actual height of Var $height;
  Change the width of var $resize _width;
  After changing the height of var $resize _height;
  Whether to map Var $cut;
  source image var $srcimg;
  Target image address Var $dstimg;
 
  A temporarily created image Var $im;
    function Resizeimage ($img, $wid, $hei, $c, $dstpath) {$this->srcimg = $img;
    $this->resize_width = $wid;
    $this->resize_height = $hei;
    $this->cut = $c;
 
    Type of picture $this->type = Strtolower (substr (STRRCHR ($this->srcimg, "."), 1));
    Initialization of Image $this->initi_img ();
    The target image address $this-> dst_img ($dstpath);
    --$this->width = Imagesx ($this->im);
    $this->height = Imagesy ($this->im);
    Generating Image $this->newimg ();
  Imagedestroy ($this->im);
    function newimg () {//The proportions of the altered image $resize _ratio = ($this->resize_width)/($this->resize_height);
    The proportions of the actual image $ratio = ($this->width)/($this->height); if ($this-&GT;cut = = "1")//Map {if ($ratio >= $resize _ratio)//high priority {$newimg = Imagecreatetruecolor
        ($this->resize_width, $this->resize_height); Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, ($this->
        Height) * $resize _ratio), $this->height);
      Imagejpeg ($newimg, $this->dstimg); } if ($ratio < $resize _ratio)//width First {$newimg = Imagecreatetruecolor ($this->resize_width, $th
        Is->resize_height); Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width
        , (($this->width)/$resize _ratio));
      Imagejpeg ($newimg, $this->dstimg); } else//do not map {if ($ratio >= $resize _ratio) {$newimg = Imagecreatetruecolor ($this-&
        Gt;resize_width, ($this->resize_width)/$ratio); Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
      Imagejpeg ($newimg, $this->dstimg); } if ($ratio < $resize _ratio) {$newimg = Imagecreatetruecolor ($this->resize_height) * $ratio, $this-
        >resize_height); Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height) * $ratio, $this->resize_height, $
        This->width, $this->height);
      Imagejpeg ($newimg, $this->dstimg); }}///Initialize image function Initi_img () {if ($this->type== "jpg") {$this->im = imagecreatefromj
    Peg ($this->srcimg);
    } if ($this->type== "gif") {$this->im = imagecreatefromgif ($this->srcimg);
    } if ($this->type== "png") {$this->im = imagecreatefrompng ($this->srcimg);
 
    }//Image target Address 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;
  Echo $this->dstimg;
 }}?>

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.