Home > Backend Development > PHP Tutorial > PHP implementation of uploading images to generate thumbnails example_PHP tutorial

PHP implementation of uploading images to generate thumbnails example_PHP tutorial

WBOY
Release: 2016-07-13 10:32:49
Original
785 people have browsed it

The function is very simple, there are comments in the code, and I will give you the code directly

Copy code The code is as follows:

/**
* Upload images to generate thumbnails
*
* Requires GD2 library support
*
* Parameter new thumbnails('Original address of the image to be thumbnailed', 'Thumbnails' are required during initialization Thumbnail width', 'Thumbnail height', '(optional parameter) Thumbnail saving path');
* If the last parameter is not specified, the thumbnail will be saved in the directory of the original image by default. In the small folder,
* If the small folder does not exist, the small folder will be automatically created
*
* After initialization, you need to call the method produce to create thumbnails
* $thumbnails = new thumbnails (''....);
* $thumbnails->produce();
*
* You can get the relevant information of the original image, width, height, and image mime
*
* $thumbnails->getImageWidth(); //int image width
* $thumbnails->getImageHeight(); // int image height
* $thumbnails->getImageMime(); / / string mime of the image
*
* $thumbnails->trueSize(); //array This is an array containing the width and height values ​​of the image after the image is scaled down
* $size = array ('width'=>'','height'=>'');
* Get the width and height of the image after scaling it down
* $size['width']//Constant ratio The width of the thumbnail
* $size['height']//The height of the proportional thumbnail
*
*/
class thumbnails{

private $imgSrc; //Path of the image
private $saveSrc; / /The saving path of the image, the default is empty
private $canvasWidth; //Width of canvas
private $canvasHeight; //Height of canvas
private $im; //Canvas resource
private $dm ; //Copy the resources returned by the image

/**
* Initialize the class and load related settings
*
* @param $imgSrc The path of the image that needs to be thumbnailed
* @param $canvasWidth The width of the thumbnail
* @param $canvasHeight thumbnail The height of the sketch
*/
public function __construct($imgSrc,$canvasWidth,$canvasHeight,$saveSrc=null)
{
$this->imgSrc = $imgSrc;
$this->canvasWidth = $canvasWidth;
$this->canvasHeight = $canvasHeight;
$this->saveSrc = $saveSrc;
}

/**
* Generate thumbnails
*/
public function produce()
{
$this->createCanvas();
$this->judgeImage ();
$this->copyImage();
$this->headerImage();
}

/**
* Get information about the loaded image
*
* Contains length, width, image type
*
* @return array Array containing image length, width, mime
*/
private function getImageInfo()
{
return getimagesize($this->imgSrc);
}

/**
* Get the length of the image
*
* @return int The width of the image
*/
public function getImageWidth()
{
$imageInfo = $this->getImageInfo();
return $imageInfo['0'];
}

/**
* Get the height of the image
*
* @return int The height of the image
*/
public function getImageHeight()
{
$imageInfo = $this->getImageInfo();
return $imageInfo['1'];
}

/**
* Get the type of image
*
* @return the mime value of the image
*/
public function getImageMime()
{
$imageInfo = $this->getImageInfo();
return $imageInfo['mime'];
}

/**
* Create canvas
*
* At the same time, put the created canvas resource into the attribute $this->im
*/
private function createCanvas()
{
$size = $this->trueSize();
$this->im = imagecreatetruecolor($ size['width'],$size['height']);
}

/**
* Determine the mime value of the image and determine the function to use
*
* At the same time, put the created image resource into $this->dm
*/
private function judgeImage()
{
$ mime = $this->getImageMime();
switch ($mime)
{
case 'image/png':$dm = imagecreatefrompng($this->imgSrc);
break ;

case 'image/gif':$dm = imagecreatefromgif($this->imgSrc);
break;

case 'image/jpg':$dm = imagecreatefromjpeg( $this->imgSrc);
break;

case 'image/jpeg':$dm = imagecreatefromgjpeg($this->imgSrc);
break;
}
$this->dm = $dm;
}

 /**
* Determine the width and height of the image after thumbnailing
*
* This width and height are also used as the size of the canvas
*
* @return array The size of the image after being reduced in equal proportions
*/
 public function trueSize()
 {
  $proportionW = $this->getImageWidth() / $this->canvasWidth;
  $proportionH = $this->getImageHeight() / $this->canvasHeight;

  if( ($this->getImageWidth() < $this->canvasWidth) && ($this->getImageHeight() < $this->canvasHeight) )
  {
   $trueSize = array('width'=>$this->getImageWidth(),'height'=>$this->getImageHeight());
  }
  elseif($proportionW >= $proportionH)
  {
   $trueSize = array('width'=>$this->canvasWidth,'height'=>$this->getImageHeight() / $proportionW);
  }
  else
  {
   $trueSize = array('width'=>$this->getImageWidth() / $proportionH,'height'=>$this->canvasHeight);
  }
  return $trueSize;
 }

 /**
* Copy the image to a new canvas
*
* The image will be scaled proportionally and will not be deformed
*/
 private function copyImage()
 {
  $size = $this->trueSize();
  imagecopyresized($this->im, $this->dm , 0 , 0 , 0 , 0 , $size['width'] , $size['height'] , $this->getImageWidth() , $this->getImageheight());
 }

 /**
* Export the image
*
* The name of the image is the same as the original image name by default
*
* The path is the small directory in the current directory of the large image
*
* If the small directory does not exist, it will be created automatically
*/
 public function headerImage()
 {
  $position = strrpos($this->imgSrc,'/');
  $imageName = substr($this->imgSrc,($position + 1));
  if($this->saveSrc)
  {
   $imageFlode = $this->saveSrc.'/';
  }
  else
  {
   $imageFlode = substr($this->imgSrc,0,$position).'/small/';
  }
  if(!file_exists($imageFlode))
  {
   mkdir($imageFlode);
  }
  $saveSrc = $imageFlode.$imageName;
  imagejpeg($this->im,$saveSrc);
 } 
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/754037.htmlTechArticle功能很简单,代码中有注释,直接给大家上代码了 复制代码 代码如下: ?php /** * 上传图片生成缩略图 * * 需要GD2库的支持 * * 初始化时需要参...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template