Thumbnails generated by php

Source: Internet
Author: User
Tags imagejpeg

Generating thumbnails is a common practice when we want to upload images. If the images are large, we need to generate a small image of the specified size to display it on the list page. This saves resources, next I will share a php thumbnail generation class that supports custom height and width. It also supports high and wide

The Code is as follows: Copy code

Class resizeimage
{
// Image type
Var $ type;
// Actual width
Var $ width;
// Actual height
Var $ height;
// Change the width
Var $ resize_width;
// The height after the change
Var $ resize_height;
// Whether to cut 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;
// Image type

$ 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 an image
$ This-> newimg ();
ImageDestroy ($ this-> im );
}
Function newimg ()
{
// Ratio of the changed image
$ Resize_ratio = ($ this-> resize_width)/($ this-> resize_height );
// Ratio of the actual image
$ Ratio = ($ this-> width)/($ this-> height );
If ($ this-> cut) = "1 ")
// Cut the 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 <$ resize_ratio)
// The width is preferred.
{
$ 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 cut chart
{
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 <$ 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 the 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 );
}
}
// Target image 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;
}
}

$ Resizeimage = new resizeimage ("11.jpg"," 200 "," 150 "," 1 "," 17.jpg ");
?>

Instance 2

The Code is as follows: Copy code

<? Php

/**
* Generating thumbnails
* @ Author yangzhiguo0903@163.com
* @ Param string the absolute complete source image address {with a file name and suffix}
* @ Param string the absolute complete address of the Target Image {with the file name and suffix}
* @ Param int thumbnail width {0: The target height cannot be 0, and the target width is the source Image Width * (Target Height/source Image Height )}
* @ Param int thumbnail height {0: The target width cannot be 0, and the target height is the source Image Height * (target width/source image width )}
* @ Param int whether to crop {width, height must be not 0}
* @ Param int/float scale {0: No scale, 0 <this <1: scale to the corresponding proportion (in this case, the width and height limit and cropping are invalid )}
* @ Return boolean
*/
Function img2thumb ($ src_img, $ dst_img, $ width = 75, $ height = 75, $ cut = 0, $ proportion = 0)
{
If (! Is_file ($ src_img ))
{
Return false;
}
$ Ot = fileext ($ dst_img );
$ Otfunc = 'image'. ($ ot = 'jpg '? 'Jpeg ': $ ot );
$ Srcinfo = getimagesize ($ src_img );
$ Src_w = $ srcinfo [0];
$ Src_h = $ srcinfo [1];
$ Type = strtolower (substr (image_type_to_extension ($ srcinfo [2]), 1 ));
$ Createfun = 'imagecreatefrom '. ($ type = 'jpg '? 'Jpeg ': $ type );

$ Dst_h = $ height;
$ Dst_w = $ width;
$ X = $ y = 0;

/**
* The thumbnail size cannot exceed the source image size (provided that there is only one width or height)
*/
If ($ width> $ src_w & $ height> $ src_h) | ($ height> $ src_h & $ width = 0) | ($ width> $ src_w & $ height = 0 ))
{
$ Proportion = 1;
}
If ($ width> $ src_w)
{
$ Dst_w = $ width = $ src_w;
}
If ($ height> $ src_h)
{
$ Dst_h = $ height = $ src_h;
}

If (! $ Width &&! $ Height &&! $ Proportion)
{
Return false;
}
If (! $ Proportion)
{
If ($ cut = 0)
{
If ($ dst_w & $ dst_h)
{
If ($ dst_w/$ src_w> $ dst_h/$ src_h)
{
$ Dst_w = $ src_w * ($ dst_h/$ src_h );
$ X = 0-($ dst_w-$ width)/2;
}
Else
{
$ Dst_h = $ src_h * ($ dst_w/$ src_w );
$ Y = 0-($ dst_h-$ height)/2;
}
}
Else if ($ dst_w xor $ dst_h)
{
If ($ dst_w &&! $ Dst_h) // with width and height
{
$ Propor = $ dst_w/$ src_w;
$ Height = $ dst_h = $ src_h * $ propor;
}
Else if (! $ Dst_w & $ dst_h) // you can specify the dst_h parameter.
{
$ Propor = $ dst_h/$ src_h;
$ Width = $ dst_w = $ src_w * $ propor;
}
}
}
Else
{
If (! $ Dst_h) // No height during Cropping
{
$ Height = $ dst_h = $ dst_w;
}
If (! $ Dst_w) // No width during Cropping
{
$ Width = $ dst_w = $ dst_h;
}
$ Propor = min (max ($ dst_w/$ src_w, $ dst_h/$ src_h), 1 );
$ Dst_w = (int) round ($ src_w * $ propor );
$ Dst_h = (int) round ($ src_h * $ propor );
$ X = ($ width-$ dst_w)/2;
$ Y = ($ height-$ dst_h)/2;
}
}
Else
{
$ Proportion = min ($ proportion, 1 );
$ Height = $ dst_h = $ src_h * $ proportion;
$ Width = $ dst_w = $ src_w * $ proportion;
}

$ Src = $ createfun ($ src_img );
$ Dst = imagecreatetruecolor ($ width? $ Width: $ dst_w, $ height? $ Height: $ dst_h );
$ White = imagecolorallocate ($ dst, 255,255,255 );
Imagefill ($ dst, 0, 0, $ white );

If (function_exists ('imagecopyresampled '))
{
Imagecopyresampled ($ dst, $ src, $ x, $ y, 0, 0, $ dst_w, $ dst_h, $ src_w, $ src_h );
}
Else
{
Imagecopyresized ($ dst, $ src, $ x, $ y, 0, 0, $ dst_w, $ dst_h, $ src_w, $ src_h );
}
$ Otfunc ($ dst, $ dst_img );
Imagedestroy ($ dst );
Imagedestroy ($ src );
Return true;
}

Usage

The Code is as follows: Copy code

$ Src_img = "./ROSI_050_002.JPG ";
$ Dst_img = "./ROSI_050_002_thumb.jpg ";
$ Stat = img2thumb ($ src_img, $ dst_img, $ width = 200, $ height = 300, $ cut = 0, $ proportion = 0 );
If ($ stat ){
Echo 'resize Image Success! <Br/> ';
Echo ' ';
} Else {
Echo 'resize Image Fail! ';
}

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.