PHP generated image thumbnail class code sharing based on GD2 graphics library,
To use PHP to generate image thumbnails, make sure your PHP server has a GD2 graphics library that uses a class to generate thumbnails of pictures
1. How to use
$resizeimage = new Resizeimage ("Picture source file Address", "200", "100", "0", "thumbnail address"), or//just use the words above to generate thumbnails, where the source file and thumbnail address can be the same, 200,100 represent width and height respectively
2. Thumbnail Class Code
Use the following class to generate picture thumbnails, <?phpclass resizeimage{//Picture type var $type; Actual width var $width; Actual height var $height; Change the width of var $resize _width; After changing the height of var $resize _height; Whether to crop the Var $cut; source image var $srcimg; Target image address Var $dstimg; 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; The type of picture $this->type = Strtolower (substr (STRRCHR ($this->srcimg, "."), 1); Initialize the image $this->initi_img (); Target image Address $this, dst_img ($dstpath); --$this->width = Imagesx ($this->im); $this->height = Imagesy ($this->im); Generate Image $this->newimg (); Imagedestroy ($this->im); } function Newimg () {//change the proportions of the image $resize _ratio = ($this->resize_width)/($this->resize_height); The ratio of actual images $ratio = ($this->width)/($this->height); if ($this->cut) = = "1")//crop {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, $this-& Gt;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->resi Ze_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 = imagecreatefromjpeg ($thi S->SRCIMG); } if ($this->type== "gif") {$this->im = imagecreatefromgif ($this->srcimg); } if ($this->type== "png") {$this->im = imagecreatefrompng ($this->srcimg); }}//Image Destination 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; }}?>
http://www.bkjia.com/PHPjc/955405.html www.bkjia.com true http://www.bkjia.com/PHPjc/955405.html techarticle based on the GD2 graphics library of PHP generated image thumbnail class code sharing, to use PHP to generate image thumbnails, to ensure that your PHP server installed GD2 graphics library using a class to generate thumbnails of the image ...