下面就是生成指定尺寸的缩略图的函数- //生成指定尺寸的缩略图
- function make_m_thumb($srcfile,$objfile,$w,$h) {
- global $_SGLOBAL;
- //获取图片信息
- $im = '';
- if($data = getimagesize($srcfile)) {
- if($data[2] == 1) {
- $make_max = 0;//gif不处理
- if(function_exists("imagecreatefromgif")) {
- $im = imagecreatefromgif($srcfile);
- }
- } elseif($data[2] == 2) {
- if(function_exists("imagecreatefromjpeg")) {
- $im = imagecreatefromjpeg($srcfile);
- }
- } elseif($data[2] == 3) {
- if(function_exists("imagecreatefrompng")) {
- $im = imagecreatefrompng($srcfile);
- }
- }
- }
-
- if(!$im) return '';
-
- $srcw = imagesx($im);
- $srch = imagesy($im);
-
- $p = $srcw/$srch;
- $sw = $sh = $tw = $th = 0;//初始化切割图片坐标
- if($srcw>$w || $srch>$h){
- if($p>1){
- $sw = ceil(($srcw - $srch)/2);
- $tw = $srch;
- $sh = 0;
- $th = $srch;
- }elseif($p<1){
- $sw = 0;
- $sh = ceil(($srch - $srcw)/2);
- $tw = $srcw;
- $th = $srcw;
- }else{
- $sh = $sw = 0;
- $th = $srch; $tw = $srcw;
- }
- }
- if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($w, $h)) {
- imagecopyresampled($ni, $im, 0, 0, $sw, $sh, $w, $h, $tw, $th);
- } elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($w, $h)) {
- imagecopyresampled($ni, $im, 0, 0, $sw, $sh, $w, $h, $tw, $th);
- } else {
- return '';
- }
- if(function_exists('imagejpeg')) {
- imagejpeg($ni, $objfile);
- } elseif(function_exists('imagepng')) {
- imagepng($ni, $objfile);
- }
- imagedestroy($ni);
- if(!file_exists($objfile)) {
- return '';
- } else {
- return $objfile;
- }
- }
复制代码 |