PHP Generate thumbnail class

Source: Internet
Author: User
Tags imagejpeg strlen trim
The code is as follows Copy Code

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 < $resize _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 < $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);
}
}
}
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;
}
}

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

Example Two

The code is as follows Copy Code

<?php

/**
* Generate thumbnails
* @author yangzhiguo0903@163.com
* @param string source diagram absolute full address {with filename and suffix name}
* @param string Destination map absolute full address {with filename and suffix name}
* @param int thumbnail width {0: At this point the target height cannot be 0, the target width is source plot width * (target height/source graph height)}
* @param int thumbnail height {0: At this point the target width cannot be 0, the target height is source graph high * (target width/source wide chart)}
* @param int Trim {Wide, height must not 0}
* @param int/float Scaling {0: No scaling, 0<this<1: scaling to the appropriate proportions (at this time wide-height limit and trim all fail)}
* @return Boolean
*/
function Img2thumb ($src _img, $dst _img, $width = $height =, $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;

/**
* Thumbnail does not exceed source diagram size (if only one is wide or high)
*/
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)//wide without height
{
$propor = $dst _w/$src _w;
$height = $dst _h = $src _h * $PROPOR;
}
else if (! $DST _w && $dst _h)//High without width
{
$propor = $dst _h/$src _h;
$width = $dst _w = $src _w * $PROPOR;
}
}
}
Else
{
if (! $DST _h)//cropped without high
{
$height = $dst _h = $dst _w;
}
if (! $DST _w)//trimmed without width
{
$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;
}

How to use

  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 = =, $height = 0, $cut = 0);
if ($stat) {
 echo ' Resize Image success!<br/> ';
 echo ' }else{
 echo ' Resize Image fail! '; &NBSP
}

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.