/*
- * Desc:resize Image (PNG, JPG, GIF)
- */
- Class Resizeimage {
- Type of picture
- Private $type;
- Actual width
- Private $width;
- Actual height
- Private $height;
- The width after the change
- Private $resize _width;
- The height after the change
- Private $resize _height;
- Whether or not to cut the map
- Private $cut;
- Source image
- Private $srcimg;
- Destination image Address
- Private $dstimg;
- Temporarily created images
- Private $im;
function __construct ($imgPath, $width, $height, $isCut, $savePath) {
- $this->srcimg = $imgPath;
- $this->resize_width = $width;
- $this->resize_height = $height;
- $this->cut = $isCut;
- Type of picture
$this->type = Strtolower (substr (STRRCHR ($this->srcimg, "."), 1);
Initializing an image
- $this->initi_img ();
- Destination image Address
- $this-Dst_img ($savePath);
- //--
- $this->width = imagesx ($this->im);
- $this->height = Imagesy ($this->im);
- Creating images
- $this->newimg ();
- Imagedestroy ($this->im);
- }
Private Function newimg () {
- The proportions of the altered image
- $resize _ratio = ($this->resize_width)/($this->resize_height);
- Ratio of actual images
- $ratio = ($this->width)/($this->height);
- if ($this->cut) {
- Crop chart
- $newimg = Imagecreatetruecolor ($this->resize_width, $this->resize_height);
- if ($this->type== "png") {
- Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127));
- }
- if ($ratio >= $resize _ratio) {
- High priority
- Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, (($this Height) * $resize _ratio), $this->height);
- } else {
- Width first
- Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width , (($this->width)/$resize _ratio));
- }
- } else {
- Do not cut the map
- if ($ratio >= $resize _ratio) {
- $newimg = Imagecreatetruecolor ($this->resize_width, ($this->resize_width)/$ratio);
- if ($this->type== "png") {
- Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127));
- }
- Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this- >width, $this->height);
- } else {
- $newimg = Imagecreatetruecolor (($this->resize_height) * $ratio, $this->resize_height);
- if ($this->type== "png") {
- Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127));
- }
- Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height) * $ratio, $this->resize_height, $ This->width, $this->height);
- }
- }
- if ($this->type== "png") {
- Imagesavealpha ($newimg, true);
- Imagepng ($newimg, $this->dstimg);
- } else {
- Imagejpeg ($newimg, $this->dstimg);
- }
- }
Initializing an image
- Private 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
- Private 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;
- }
- }
- ?>
Copy CodeThe constructor of the class is called directly using the constructor: $resizeimage = new Resizeimage ($imgPath, $width, $height, $isCut, $savePath); Parameter $imgpath: Original image address $width: Thumbnail width $height: Thumbnail high $iscut: Whether clipping, bool value $savepath: Thumbnail address (can be the same as the original image address) Example:
Include "resizeimage.php";
Jpg
- $jpgResize = new Resizeimage ("Img/test_1920_1200.jpg", "img/test_320_240.jpg");
Png
- $pngResize = new Resizeimage ("Img/test_1024_746.png", "img/test_320_240.png");
- ?>
Copy CodeEffect: |