-
/**
- * Upload images to generate thumbnails
- *
- * Requires GD2 library support
- *
- * Parameters new thumbnails('original address of the image to be thumbnailed', 'width of thumbnail', 'height of thumbnail' are required during initialization ','(optional parameter) Thumbnail saving path');
- * If the last parameter is not specified, the thumbnail will be saved in the small folder in the directory of the original image by default,
- * If small does not exist folder, 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 sum of the image after scaling it down. Array of height values
- * $size = array('width'=>'','height'=>'');
- * Get the width and height of the image after scaling it
- * $size['width ']//The width of the proportional thumbnail
- * $size['height']//The height of the proportional thumbnail
- *
- */
- class thumbnails{
- private $imgSrc; //Picture path
- private $saveSrc; //Picture saving path, default is empty
- private $ canvasWidth; //The width of the canvas
- private $canvasHeight; //The height of the canvas
- private $im; //Canvas resources
- private $dm; //Resources returned by copying 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 The height of the thumbnail
- */
- 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 the information of the loaded image
- *
- * Contains the length, width, and image type
- *
- * @return array An array containing the image length, width, and 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 being reduced
- *
- * 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 under the current directory of the large image
- *
- * If the small directory does not exist, it will be automatically created
- */
- 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);
- }
- }
复制代码
|