Home > php教程 > php手册 > php 生成缩略图函数代码

php 生成缩略图函数代码

WBOY
Release: 2016-06-13 10:39:24
Original
872 people have browsed it

  1.  /**
  2.  * 生成图片缩略图
  3.  *
  4.  * @param   string  $src    原图地址
  5.  * @param   string  $savePath 缩略图保存地址
  6.  * @param   integer $width  缩略图宽
  7.  * @param   integer $height 缩略图高
  8.  * @return  string  缩略图地址
  9.  */
  10.  function buildThumb($src, $savePath, $width = 220, $height = 180)
  11.  {
  12.      $arr = getimagesize($src);
  13.      if (!is_array($arr)) {
  14.          return false;
  15.      }
  16.      //1,2,3 分别为gif,jpg,png
  17.      if ($arr[2] > 4) {
  18.          return false;
  19.      }
  20.      $func = imagecreatefrom;
  21.      switch ($arr[2]) {
  22.          case 1  : $func .= gif; break;
  23.          case 2  : $func .= jpeg; break;
  24.          case 3  : $func .= png; break;
  25.          default :  $func .= jpeg;
  26.      }
  27.      $srcIm = $func($src);
  28.      $im    = imagecreatetruecolor($width, $height);
  29.       imagecopyresized($im, $srcIm, 0, 0, 0, 0, $width, $height, $arr[0], $arr[1]);
  30.      imagejpeg($im, $savePath);
  31.      imagedestroy($srcIm);
  32.      imagedestroy($im);
  33.      return true;
  34.  }
  35. ?>


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template