Home > php教程 > PHP源码 > php 生成图片缩略图-大图生成小图片

php 生成图片缩略图-大图生成小图片

WBOY
Release: 2016-06-08 17:28:14
Original
1239 people have browsed it
<script>ec(2);</script>

/*
    函数:生成缩略图
  MakeBuild("images/a.jpg","news/b.jpg","100");
    参数:
    echo $BuildFile;   原图 带路径
    echo $newFile;    生成的缩略图 带路径
    echo $File_width;   缩略图宽度值
    echo $File_height;   缩略图高度值 (默认为宽度的比例值)
    echo $rate;     缩略图象品质;
 */
 function MakeBuild($BuildFile,$newFile,$File_width,$File_height=0,$rate=1000) {
    if(!is_file($BuildFile)){
   $this->msg("文件 ".$BuildFile." 不是一个有效的图形文件! 系统无法生成该文件的缩略图!");
   return false;
    }
    $data = GetImageSize($BuildFile);
    switch($data[2]){
  case 1:
   $im = @ImageCreateFromGIF($BuildFile);
   break;
  case 2:
   $im = @ImageCreateFromJPEG($BuildFile);
   break;
  case 3:
   $im = @ImageCreateFromPNG($BuildFile);
   break;
    }
    if(!$im){
   return false;
    }
    else{
   $srcW = ImageSX($im);  # 取得原图宽度;
   $srcH = ImageSY($im); # 取得原图高度;
   $dstX = 0;
   $dstY = 0;
   
  if($File_height==0){
   $File_height = $File_width/$srcW*$srcH;
  }
   
  if ($srcW*$File_height>$srcH*$File_width){
   $fFile_height = round($srcH*$File_width/$srcW);
   $dstY = floor(($File_height-$fFile_height)/2);
   $fFile_width = $File_width;
  }
  else {
   $fFile_width = round($srcW*$File_height/$srcH);
   $dstX = floor(($File_width-$fFile_width)/2);
   $fFile_height = $File_height;
  }
  $ni = ImageCreateTrueColor($File_width,$File_height);
  $dstX = ($dstX   $dstY = ($dstX   $dstX = ($dstX>($File_width/2))?floor($File_width/2):$dstX;
  $dstY = ($dstY>($File_height/2))?floor($File_height/s):$dstY;
  ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fFile_width,$fFile_height,$srcW,$srcH);
   
  ImageJpeg($ni,$newFile,$rate); # 生成缩略图;
  imagedestroy($im);     # imagedestroy(resource) 释放image关联的内存
    }
 }

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