PHP generates thumbnail images

Source: Internet
Author: User
Tags imagejpeg php server strlen

PHP to generate thumbnail images:

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

Use a class to generate a thumbnail of the picture, the source of the class is shown below

Calling methods of this class:

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

You can generate thumbnail images using the following classes,

Class Resizeimage

{

Picture type

var type;

Actual width

var width;

Actual height

var height;

The width after the change

var resize_width;

Change the height of the post

var resize_height;

Whether or not to cut the map

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 picture

This->type = Strtolower (substr (STRRCHR (This->srcimg, "."), 1));

Initializing images

This->initi_img ();

Target Image Address

This-> dst_img (Dstpath);

//--

This->width = Imagesx (This->im);

This->height = Imagesy (This->im);

Build 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->cut) = = "1")

Drawing

{

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

Width first

{

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

Do not cut the figure

{

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

}

}

}

Initializing images

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

}

}

?>

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.