请选择时期:
怀孕准备 怀孕 分娩 宝宝0-1岁 宝宝1-3岁 宝宝3-6岁

PHP生成图片缩略图类示例

来源: 最后更新:22-04-03 05:49:28

导读:PHP生成图片缩略图类示例,

  篇文章介绍了PHP生成图片缩略图类示例,有兴趣的同学可以参考一下

  本文实例讲述了PHP生成图片缩略图类。分享给大家供大家参考,具体如下:


  classApp_image_helper {


  protected$imgFileName;


  protected$imgWidth;


  protected$imgHeight;


  protected$imgMime;


  protected$imgResource;


  static $imgMineList


  =array(


  'jpeg'=>'image/jpeg',


  'gif'=>'image/gif',


  'png'=>'image/png',


  'wbmp'=>'image/wbmp',


  );


  /**


  * 根据文件名,初始化图片,


  * 计算出给定图片的宽、高、图片类型,并获取图片的资源保存到内存,便于下次使用


  * App_image_helper constructor.


  *


  * @param $fileName


  */


  publicfunction__construct($fileName) {


  $this->imgFileName =$fileName;


  list($this->imgWidth,$this->imgHeight,$this->imgMime) =$this->getImageInfo($this->imgFileName);


  $this->imgResource =$this->getImageResource($this->imgFileName);


  }


  /**


  * 根据图片路径获取相关宽、高、MIME类型信息


  *


  * @param $fileName


  *


  * @return array|null


  */


  protectedfunctiongetImageInfo($fileName) {


  $result= null;


  if(is_file($fileName) ) {


  $tmpImageInfo=getimagesize($fileName);


  if($tmpImageInfo) {


  $result=array($tmpImageInfo[0],$tmpImageInfo[1],$tmpImageInfo['mime']);


  }


  }


  return$result;


  }


  /**


  * 将图片文件转为资源类类型


  *


  * @param $fileName


  *


  * @return null|resource


  */


  protectedfunctiongetImageResource($fileName) {


  $image= null;


  if(is_file($fileName) ) {


  switch($this->imgMime) {


  caseself::$imgMineList['jpeg']:


  $image= imagecreatefromjpeg($fileName);


  break;


  caseself::$imgMineList['gif']:


  $image= imagecreatefromgif($fileName);


  break;


  caseself::$imgMineList['png']:


  $image= imagecreatefrompng($fileName);


  break;


  caseself::$imgMineList['wbmp']:


  $image= imagecreatefromwbmp($fileName);


  break;


  default:


  break;


  }


  }


  return$image;


  }


  /**


  * 可根据固定宽,等比缩放图片;或根据百分比,等比缩放图片


  *


  * @param int $width


  * @param int $percent


  *


  * @return array|null


  */


  protectedfunctiongetSizeByScale($width= 360,$percent= 1) {


  $result= null;


  if($this->imgWidth &&$this->imgHeight ) {


  if($width) {


  $result=array($width,intval($width*$this->imgHeight /$this->imgWidth));


  }elseif($percent) {


  $result=array(intval($this->imgWidth *$percent),intval($this->imgHeight *$percent));


  }


  }


  return$result;


  }


  /**


  * 外调


  *


  * @param int $percentOrWidth int整数表示图片缩放为固定宽度,0.0~0.99999表示缩放百分比


  * @param null $fileName


  * @param int $quality


  * @param bool $reSample 重新采样图片,默认是


  *


  * @return bool


  */


  publicfunctioncreateImage($percentOrWidth= 1,$fileName= null,$quality= 75,$reSample= true) {


  $result= false;


  $fileName? header('Content-Type: '.$this->imgMime) : false;


  $size=$this->getSizeByScale(($percentOrWidth<= 1) ? null :$percentOrWidth,$percentOrWidth);


  if($size) {


  $thumb= imagecreatetruecolor($size[0],$size[1]);


  if($reSample) {


  imagecopyresampled($thumb,$this->imgResource, 0, 0, 0, 0,$size[0],$size[1],$this->imgWidth,$this->imgHeight);


  }else{


  imagecopyresized($thumb,$this->imgResource, 0, 0, 0, 0,$size[0],$size[1],$this->imgWidth,$this->imgHeight);


  }


  $result= imagejpeg($thumb,$fileName,$quality);


  }


  return$result;


  }


  }


标签: 软件  示例  缩略图  百科  

免责声明:本文系转载,版权归原作者所有;旨在传递信息,其原创性以及文中陈述文字和内容未经本站证实。

本文地址:http://www.zuomama.com/qiaomen/youxi/226091.html

  • 1学校组织学生徒步自费看《满江红》,这种“自愿”未免太牵强

    学校组织学生徒步自费看《满江红》,这种“自愿”未免太牵强

  • 2怎么样鸽子 怎么养鸽子不怕它飞走

    怎么样鸽子 怎么养鸽子不怕它飞走

  • 3拱辰享怎么样 粉色后拱辰享怎么样

    拱辰享怎么样 粉色后拱辰享怎么样

  • 4汉能怎么样 汉能 北京

    汉能怎么样 汉能 北京

  • 5清华怎么样 蒲城朗悦清华怎么样

    清华怎么样 蒲城朗悦清华怎么样

  • 6叶酸片怎么样 斯利安叶酸片怎么样

    叶酸片怎么样 斯利安叶酸片怎么样

  • 7湘雅怎么样(湘雅各医院的优势)

    湘雅怎么样(湘雅各医院的优势)

  • 8民生保险怎么样 中国人民保险怎么样

    民生保险怎么样 中国人民保险怎么样

  • 9鸽子怎么样(鸽子怎么样做才好吃)

    鸽子怎么样(鸽子怎么样做才好吃)

  • 10新君威怎么样 全新君威怎么样

    新君威怎么样 全新君威怎么样

关于我们 | 广告服务 | 网站合作 | 免责声明 | 联系我们| 网站地图

© 2006-2022 做妈妈育儿网 all rights reserved. 浙ICP备2022035435号-3

声明: 本站文章均来自互联网,不代表本站观点 如有异议 请与本站联系 联系邮箱:kf#zuomama.com (请把#替换成@)