Home >Backend Development >PHP Tutorial >Generate image thumbnails with PHP_PHP tutorial

Generate image thumbnails with PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:27:44963browse

PHP generates image thumbnails:

To use PHP to generate image thumbnails, make sure your PHP server has the GD2 graphics library installed

Use a class to generate thumbnails of images. See the source code of the class below

Call the method of this class:

resizeimage = new resizeimage("image source file address", "200", "100", "0", "thumbnail address");

//Just use the above sentence to generate a thumbnail. The source file and thumbnail addresses can be the same, 200 and 100 represent the width and height respectively

//Use the following class to generate image thumbnails,

class resizeimage

 {

//Image type

 var type;

 //Actual width

 var width;

//Actual height

 var height;

//Changed width

var resize_width;

//Height after change

var resize_height;

//Whether to crop the image

 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;

//Type of image

 this->type = strtolower(substr(strrchr(this->srcimg,"."),1));

//Initialize 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()

 {

//The proportion of the changed image

resize_ratio = (this->resize_width)/(this->resize_height);

//Proportion of actual image

ratio = (this->width)/(this->height);

 if((this->cut)=="1")

 //Cut image

 {

 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

//Breadth priority

 {

newimg = imagecreatetruecolor(this->resize_width,this->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

//No cropping

 {

 if(ratio>=resize_ratio)

 {

newimg = imagecreatetruecolor(this->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

 {

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(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;

 }

 }

 ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/815031.htmlTechArticleGenerate image thumbnails with PHP: To use PHP to generate image thumbnails, make sure your PHP server has GD2 graphics installed. The library uses a class to generate thumbnails of images. The source code of the class is shown below. Call this...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn