栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php实现上传图片生成缩略图示例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php实现上传图片生成缩略图示例

功能很简单,代码中有注释,直接给大家上代码了

复制代码 代码如下:

class thumbnails{

 private $imgSrc; //图片的路径
 private $saveSrc; //图片的保存路径,默认为空
 private $canvasWidth; //画布的宽度
 private $canvasHeight; //画布的高度
 private $im; //画布资源
 private $dm; //复制图片返回的资源

 
 public function __construct($imgSrc,$canvasWidth,$canvasHeight,$saveSrc=null)
 {
  $this->imgSrc = $imgSrc;
  $this->canvasWidth = $canvasWidth;
  $this->canvasHeight = $canvasHeight;
  $this->saveSrc = $saveSrc;
 }

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

 
 private function getImageInfo()
 {
  return getimagesize($this->imgSrc);
 }

 
 public function getImageWidth()
 {
  $imageInfo = $this->getImageInfo();
  return $imageInfo['0'];
 }

 
 public function getImageHeight()
 {
  $imageInfo = $this->getImageInfo();
  return $imageInfo['1'];
 }

 
 public function getImageMime()
 {
  $imageInfo = $this->getImageInfo();
  return $imageInfo['mime'];
 }

 
 private function createCanvas()
 {
  $size = $this->trueSize();
  $this->im = imagecreatetruecolor($size['width'],$size['height']);
 }

 
 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;
 }

 
 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;
 }

 
 private function copyImage()
 {
  $size = $this->trueSize();
  imagecopyresized($this->im, $this->dm , 0 , 0 , 0 , 0 , $size['width'] , $size['height'] , $this->getImageWidth() , $this->getImageheight());
 }

 
 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.mshxw.com
本文地址:https://www.mshxw.com/it/48373.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号