首頁 >php教程 >php手册 >缩略图生成

缩略图生成

WBOY
WBOY原創
2016-06-07 11:45:071205瀏覽

自动生成缩略图,支持裁切。调用方法简单:thumb.php?url=/yovae/data/userface/000/00/00/41_middle_face.jpg?1247718988&w=20&h=20
<?php <br /> /*<br>  * 自动缩略图 参数 url|w|h|type="cut/full"|mark="text/image|r"<br>  * thumb.php?url=/yovae/data/userface/000/00/00/41_middle_face.jpg?1247718988&w=20&h=20<br> */<br> error_reporting(0);<br> set_time_limit(30);<br> $biggest_memory_limit    =    256; //单位M,后缀不要加M<br> //全局定义文件<br> //require 'define.inc.php';<br> <br> //临时目录<br> $tempDir    =    "./data/thumb_temp/";<br> checkDir($tempDir);<br> <br> //分析URL<br> $url = urldecode($_GET['url']);<br> $url = preg_replace('/(.*)\?(.*)$/','$1',$url);<br> <br> //XSS脚本攻击探测<br> //include THINK_PATH.'/Vendor/xss.php';<br> //DetectXSS($url);<br> <br> //2009-10-7 修改 将本地图片修改成相对地址,避免file_get_contents不能读取远程文件时出错(可修改php.ini  设置 allow_fopen_url 为 true)<br> //$url =  str_ireplace(SITE_URL,'.',$url);<br> if(file_exists($url)){<br>     $url    =    $url;<br> }elseif($result    =    GrabImage($url,$tempDir)){<br>     $url    =    $result;<br>     $grab_temp_file    =    $result;<br> }else{<br>     $url    =    "./Public/images/nopic.jpg";<br> }<br> <br> //解析参数<br> $w = $_GET['w']?intval($_GET['w']):'100';    //宽度<br> $h = $_GET['h']?intval($_GET['h']):'100';    //高度<br> $t = $_GET['t']=='f'?'f':'c';        //是否切割<br> $r = $_GET['r']?1:0;            //是否覆盖<br> <br> //目录名hash<br> $fileHash    =    md5($url.$w.$h);<br> $hashPath    =    substr($fileHash,0,2).'/'.substr($fileHash,2,2).'/';<br> <br> //缩图目录<br> $thumbDir    =    "./data/thumb/".$hashPath;<br> checkDir($thumbDir);<br> <br> if(!$sourceInfo['type']) <br>     $sourceInfo['type'] == 'jpg';<br> <br> $tempFile    =    $tempDir.$fileHash.'.'.$sourceInfo['type'];<br> $thumbFile    =    $thumbDir.$fileHash."_".$w."_".$h."_".$t.'.'.$sourceInfo['type'];<br> <br> $img        =    new Image();<br> //判断是否替换,存在则跳转,不存在继续进行<br> if(!$r && file_exists($thumbFile)){<br>     //这里有2种方法,第一种多一次跳转,多一个http连接数,第二种,要进行一次php处理,多占用一部分内存。<br>     //header('location:'.$thumbFile);<br>     $img->showImg($thumbFile);<br> }<br> <br> //不存在输出<br> if(copy($url,$tempFile)){<br> <br>     //判断图片大小 如果图片宽和高都小于要缩放的比例 直接输出<br>     $info    =    getimagesize($tempFile);<br>     //判断处理图片大约需要的内存<br>     $need_memory    =    (($info[0]*$info[1])/100000);<br>     $memory_limit    =    ini_get('memory_limit');<br>     if( ($need_memory > $memory_limit) && ($need_memory          ini_set('memory_limit',$need_memory.'M');<br>     }<br> <br>     if($info[0]         copy($tempFile,$thumbFile);<br>         $img->showImg($thumbFile,'',$info[0],$info[1]);<br>         unlink($tempFile);<br>         unlink($grab_temp_file);<br>         exit;<br>     }else{<br>         //生成缩图<br>         if($t=='c'){<br>             $thumb    =    $img->cutThumb($tempFile,$thumbFile,$w,$h);<br>         }elseif($t=='f'){<br>             $thumb    =    $img->thumb($tempFile,'',$thumbFile,$w,$h);<br>         }<br>         //输出缩图<br>         $img->showImg($thumb,'',$w,$h);<br>         unlink($tempFile);<br>         unlink($grab_temp_file);<br>         exit;<br>     }<br> }<br> <br> //获取远程图片<br> function GrabImage($url,$thumbDir) {<br>     if($url=="")    return false;<br>     $filename    =    md5($url).strrchr($url,".");<br>     $img        =    file_get_contents($url);<br>     if(!$img)        return false;<br> <br>     $filepath    =    $thumbDir.$filename;<br>     $result        =    file_put_contents($filepath,$img);<br>     if($result){<br>         return $filepath;<br>     }else{<br>         return false;<br>     }<br> }<br> <br> //检查并创建多级目录<br> function checkDir($path){<br>     $pathArray = explode('/',$path);<br>     $nowPath = '';<br>     array_pop($pathArray);<br>     foreach ($pathArray as $key=>$value){<br>         if ( ''==$value ){<br>             unset($pathArray[$key]);<br>         }else{<br>             if ( $key == 0 )<br>                 $nowPath .= $value;<br>             else<br>                 $nowPath .= '/'.$value;<br>             if ( !is_dir($nowPath) ){<br>                 if ( !mkdir($nowPath, 0777) ) return false;<br>             }<br>         }<br>     }<br>     return true;<br> }<br> <br> function imagecreatefrombmp($fname) {<br> <br>     $buf=@file_get_contents($fname);<br> <br>     if(strlen($buf) <br>     $file_header=unpack("sbfType/LbfSize/sbfReserved1/sbfReserved2/LbfOffBits",substr($buf,0,14));<br> <br>     if($file_header["bfType"]!=19778) return false;<br>     $info_header=unpack("LbiSize/lbiWidth/lbiHeight/sbiPlanes/sbiBitCountLbiCompression/LbiSizeImage/lbiXPelsPerMeter/lbiYPelsPerMeter/LbiClrUsed/LbiClrImportant",substr($buf,14,40));<br>     if($info_header["biBitCountLbiCompression"]==2) return false;<br>     $line_len=round($info_header["biWidth"]*$info_header["biBitCountLbiCompression"]/8);<br>     $x=$line_len%4;<br>     if($x>0) $line_len+=4-$x;<br> <br>     $img=imagecreatetruecolor($info_header["biWidth"],$info_header["biHeight"]);<br>     switch($info_header["biBitCountLbiCompression"]){<br>     case 4:<br>     $colorset=unpack("L*",substr($buf,54,64));<br>     for($y=0;$y     $colors=array();<br>     $y_pos=$y*$line_len+$file_header["bfOffBits"];<br>     for($x=0;$x     if($x%2)<br>     $colors[]=$colorset[(ord($buf[$y_pos+($x+1)/2])&0xf)+1];<br>     else<br>     $colors[]=$colorset[((ord($buf[$y_pos+$x/2+1])>>4)&0xf)+1];<br>     }<br>     imagesetstyle($img,$colors);<br>     imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);<br>     }<br>     break;<br>     case 8:<br>     $colorset=unpack("L*",substr($buf,54,1024));<br>     for($y=0;$y     $colors=array();<br>     $y_pos=$y*$line_len+$file_header["bfOffBits"];<br>     for($x=0;$x     $colors[]=$colorset[ord($buf[$y_pos+$x])+1];<br>     }<br>     imagesetstyle($img,$colors);<br>     imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);<br>     }<br>     break;<br>     case 16:<br>     for($y=0;$y     $colors=array();<br>     $y_pos=$y*$line_len+$file_header["bfOffBits"];<br>     for($x=0;$x     $i=$x*2;<br>     $color=ord($buf[$y_pos+$i])|(ord($buf[$y_pos+$i+1])     $colors[]=imagecolorallocate($img,(($color>>10)&0x1f)*0xff/0x1f,(($color>>5)&0x1f)*0xff/0x1f,($color&0x1f)*0xff/0x1f);<br>     }<br>     imagesetstyle($img,$colors);<br>     imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);<br>     }<br>     break;<br>     case 24:<br>     for($y=0;$y     $colors=array();<br>     $y_pos=$y*$line_len+$file_header["bfOffBits"];<br>     for($x=0;$x     $i=$x*3;<br>     $colors[]=imagecolorallocate($img,ord($buf[$y_pos+$i+2]),ord($buf[$y_pos+$i+1]),ord($buf[$y_pos+$i]));<br>     }<br>     imagesetstyle($img,$colors);<br>     imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);<br>     }<br>     break;<br>     default:<br>     return false;<br>     break;<br>     }<br>     return $img;<br> }<br> function imagebmp(&$im, $filename = '', $bit = 8, $compression = 0)<br> {<br>     if (!in_array($bit, array(1, 4, 8, 16, 24, 32)))<br>     {<br>         $bit = 8;<br> <br>     }<br>     else if ($bit == 32) // todo:32 bit<br>     {<br>         $bit = 24;<br>     }<br> <br>     $bits = pow(2, $bit);<br> <br>     // 调整调色板<br>     imagetruecolortopalette($im, true, $bits);<br>     $width = imagesx($im);<br>     $height = imagesy($im);<br>     $colors_num = imagecolorstotal($im);<br> <br>     if ($bit      {<br>         // 颜色索引<br>         $rgb_quad = '';<br>         for ($i = 0; $i          {<br>             $colors = imagecolorsforindex($im, $i);<br>             $rgb_quad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0";         }<br> <br>         // 位图数据<br>         $bmp_data = '';<br> <br>         // 非压缩<br>         if ($compression == 0 || $bit          {<br>             if (!in_array($bit, array(1, 4, 8)))<br>             {<br>                 $bit = 8;<br>             }<br> <br>             $compression = 0;<br> <br>             // 每行字节数必须为4的倍数,补齐。<br> <br> <br>             $extra = '';<br>             $padding = 4 - ceil($width / (8 / $bit)) % 4;<br>             if ($padding % 4 != 0)<br>             {<br>                 $extra = str_repeat("\0", $padding);<br>             }<br> <br>             for ($j = $height - 1; $j >= 0; $j --)<br>             {<br>                 $i = 0;<br>                 while ($i                  {<br>                     $bin = 0;<br>                     $limit = $width - $i  <br>                     for ($k = 8 - $bit; $k >= $limit; $k -= $bit)<br>                     {<br>                         $index = imagecolorat($im, $i, $j);<br>                         $bin |= $index                          $i ++;<br>                     }<br> <br>                     $bmp_data .= chr($bin);<br>                 }<br> <br>                 $bmp_data .= $extra;<br>             }<br>         }<br>         // RLE8 压缩<br>         else if ($compression == 1 && $bit == 8)<br>         {<br>             for ($j = $height - 1; $j >= 0; $j --)<br>             {<br>                 $last_index = "\0";<br>                 $same_num   = 0;<br>                 for ($i = 0; $i                  {<br>                     $index = imagecolorat($im, $i, $j);<br>                     if ($index !== $last_index || $same_num > 255)<br>                     {<br>                         if ($same_num != 0)<br>                         {<br>                             $bmp_data .= chr($same_num) . chr($last_index);<br>                         }<br> <br>                         $last_index = $index;<br>                         $same_num = 1;<br>                     }<br>                     else<br>                     {<br>                         $same_num ++;<br>                     }<br>                 }<br> <br>                 $bmp_data .= "\0\0";<br>             }<br> <br>             $bmp_data .= "\0\1";<br>         }<br> <br>         $size_quad = strlen($rgb_quad);<br>         $size_data = strlen($bmp_data);<br>     }<br>     else<br>     {<br>         // 每行字节数必须为4的倍数,补齐。<br>         $extra = '';<br>         $padding = 4 - ($width * ($bit / 8)) % 4;<br>         if ($padding % 4 != 0)<br>         {<br>             $extra = str_repeat("\0", $padding);<br>         }<br> <br>         // 位图数据<br>         $bmp_data = '';<br> <br>         for ($j = $height - 1; $j >= 0; $j --)<br>         {<br>             for ($i = 0; $i              {<br>                 $index = imagecolorat($im, $i, $j);<br>                 $colors = imagecolorsforindex($im, $index);<br> <br>                 if ($bit == 16)<br>                 {<br>                     $bin = 0  <br>                     $bin |= ($colors['red'] >> 3)                      $bin |= ($colors['green'] >> 3)                      $bin |= $colors['blue'] >> 3;<br> <br>                     $bmp_data .= pack("v", $bin);<br>                 }<br>                 else<br>                 {<br>                     $bmp_data .= pack("c*", $colors['blue'], $colors['green'], $colors['red']);<br>                 }<br> <br>                 // todo: 32bit;<br>             }<br> <br>             $bmp_data .= $extra;<br>         }<br> <br>         $size_quad = 0;<br>         $size_data = strlen($bmp_data);<br>         $colors_num = 0;<br>     }<br> <br>     // 位图文件头<br>     $file_header = "BM" . pack("V3", 54 + $size_quad + $size_data, 0, 54 + $size_quad);<br> <br>     // 位图信息头<br>     $info_header = pack("V3v2V*", 0x28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0);<br>     // 写入文件<br>     if ($filename != '')<br>     {<br>         $fp = fopen("test.bmp", "wb");<br> <br>         fwrite($fp, $file_header);<br>         fwrite($fp, $info_header);<br>         fwrite($fp, $rgb_quad);<br>         fwrite($fp, $bmp_data);<br>         fclose($fp);<br> <br>         return 1;<br>     }<br> <br>     // 浏览器输出<br>     header("Content-Type: image/bmp");<br>     echo $file_header . $info_header;<br>     echo $rgb_quad;<br>     echo $bmp_data;<br> <br>     return 1;<br> }<br> class Image<br> {//类定义开始<br> <br>     /**<br>      +----------------------------------------------------------<br>      * 架构函数<br>      *<br>      +----------------------------------------------------------<br>      * @access public<br>      +----------------------------------------------------------<br>      */<br>     function __construct()<br>     {<br> <br>     }<br> <br>     /**<br>      +----------------------------------------------------------<br>      * 取得图像信息<br>      *<br>      +----------------------------------------------------------<br>      * @static<br>      * @access public<br>      +----------------------------------------------------------<br>      * @param string $image 图像文件名<br>      +----------------------------------------------------------<br>      * @return mixed<br>      +----------------------------------------------------------<br>      */<br>     function getImageInfo($img) {<br>         $imageInfo = getimagesize($img);<br>         if( $imageInfo!== false) {<br> <br>             if(function_exists(image_type_to_extension)){<br>                 $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));<br>             }else{<br>                 $imageType = strtolower(substr($img,strrpos($img,'.')+1));<br>             }<br> <br>             $imageSize = filesize($img);<br>             $info = array(<br>                 "width"=>$imageInfo[0],<br>                 "height"=>$imageInfo[1],<br>                 "type"=>$imageType,<br>                 "size"=>$imageSize,<br>                 "mime"=>$imageInfo['mime']<br>             );<br>             return $info;<br>         }else {<br>             return false;<br>         }<br>     }<br> <br>     /**<br>      +----------------------------------------------------------<br>      * 显示服务器图像文件<br>      * 支持URL方式<br>      +----------------------------------------------------------<br>      * @static<br>      * @access public<br>      +----------------------------------------------------------<br>      * @param string $imgFile 图像文件名<br>      * @param string $text 文字字符串<br>      * @param string $width 图像宽度<br>      * @param string $height 图像高度<br>      +----------------------------------------------------------<br>      * @return void<br>      +----------------------------------------------------------<br>      */<br>     function showImg($imgFile,$text='',$width=80,$height=30) {<br>         //获取图像文件信息<br>         //2007/6/26 增加图片水印输出,$text为图片的完整路径即可<br>         $info = Image::getImageInfo($imgFile);<br>         if($info !== false) {<br>             $createFun  =   str_replace('/','createfrom',$info['mime']);<br>             $im = $createFun($imgFile);<br>             if($im) {<br>                 $ImageFun= str_replace('/','',$info['mime']);<br>                 //水印开始<br>                 if(!empty($text)) {<br>                     $tc  = imagecolorallocate($im, 0, 0, 0);<br>                     if(is_file($text)&&file_exists($text)){<br>                         // 取得水印信息<br>                         $textInfo = Image::getImageInfo($text);<br>                         $createFun2= str_replace('/','createfrom',$textInfo['mime']);<br>                         $waterMark = $createFun2($text);<br>                         $imgW    =    $info["width"];<br>                         $imgH    =    $info["width"]*$textInfo["height"]/$textInfo["width"];<br>                         $y    =    ($info["height"]-$textInfo["height"])/2;<br>                         if(function_exists("ImageCopyResampled"))<br>                             ImageCopyResampled($im,$waterMark,0,$y,0,0, $imgW,$imgH, $textInfo["width"],$textInfo["height"]);<br>                         else<br>                             ImageCopyResized($im,$waterMark,0,$y,0,0,$imgW,$imgH,  $textInfo["width"],$textInfo["height"]);<br>                     }else{<br>                         imagestring($im, 3, 5, 5, $text, $tc);<br>                     }<br>                     //ImageDestroy($tc);<br>                 }<br>                 //水印结束<br>                 if($info['type']=='png' || $info['type']=='gif') {<br>                 imagealphablending($im, FALSE);//取消默认的混色模式<br>                 imagesavealpha($im,TRUE);//设定保存完整的 alpha 通道信息<br>                 }<br>                 Header("Content-type: ".$info['mime']);<br>                 $ImageFun($im);<br>                 @ImageDestroy($im);<br>                 return ;<br>             }<br>         }<br>         //获取或者创建图像文件失败则生成空白PNG图片<br>         $im  = imagecreatetruecolor($width, $height);<br>         $bgc = imagecolorallocate($im, 255, 255, 255);<br>         $tc  = imagecolorallocate($im, 0, 0, 0);<br>         imagefilledrectangle($im, 0, 0, 150, 30, $bgc);<br>         imagestring($im, 4, 5, 5, "no pic", $tc);<br>         Image::output($im);<br>         return ;<br>     }<br> <br>     // 切割缩图cutThumb<br>     // 2007/6/15<br>     function cutThumb($image,$filename='',$maxWidth='200',$maxHeight='50',$warterMark='',$type='',$interlace=true,$suffix='_thumb')<br>     {<br>         // 获取原图信息<br>         $info  = Image::getImageInfo($image);<br>          if($info !== false) {<br>             $srcWidth  = $info['width'];<br>             $srcHeight = $info['height'];<br>             $pathinfo = pathinfo($image);<br>             $type =  $pathinfo['extension'];<br>             $type = empty($type)?$info['type']:$type;<br>             $type    =    strtolower($type);<br>             $interlace  =  $interlace? 1:0;<br>             unset($info);<br>             // 载入原图<br>             $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);<br>             $srcImg     = $createFun($image);<br> <br>             //创建缩略图<br>             if($type!='gif' && function_exists('imagecreatetruecolor'))<br>                 $thumbImg = imagecreatetruecolor($maxWidth, $maxHeight);<br>             else<br>                 $thumbImg = imagecreate($maxWidth, $maxHeight);<br> <br>             // 新建PNG缩略图通道透明处理<br>             if('png'==$type) {<br>                 imagealphablending($thumbImg, false);//取消默认的混色模式<br>                 imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息<br>             }elseif('gif'==$type) {<br>             // 新建GIF缩略图预处理,保证透明效果不失效<br>                 $background_color  =  imagecolorallocate($thumbImg,  0,255,0);  //  指派一个绿色<br>                 imagecolortransparent($thumbImg,$background_color);  //  设置为透明色,若注释掉该行则输出绿色的图<br>             }<br> <br>             // 计算缩放比例<br>             if(($maxWidth/$maxHeight)>=($srcWidth/$srcHeight)){<br>                 //宽不变,截高,从中间截取 y=<br>                 $width    =    $srcWidth;<br>                 $height    =    $srcWidth*($maxHeight/$maxWidth);<br>                 $x        =    0;<br>                 $y        =    ($srcHeight-$height)*0.5;<br>             }else{<br>                 //高不变,截宽,从中间截取,x=<br>                 $width    =    $srcHeight*($maxWidth/$maxHeight);<br>                 $height    =    $srcHeight;<br>                 $x        =    ($srcWidth-$width)*0.5;<br>                 $y        =    0;<br>             }<br>             // 复制图片<br>             if(function_exists("ImageCopyResampled")){<br>                 ImageCopyResampled($thumbImg, $srcImg, 0, 0, $x, $y, $maxWidth, $maxHeight, $width,$height);<br>             }else{<br>                 ImageCopyResized($thumbImg, $srcImg, 0, 0, $x, $y, $maxWidth, $maxHeight,  $width,$height);<br>             }<br>             ImageDestroy($srcImg);<br>             /*水印开始* /<br>             if($warterMark){<br>                 //计算水印的位置,默认居中<br>                 $textInfo = Image::getImageInfo($warterMark);<br>                 $textW    =    $textInfo["width"];<br>                 $textH    =    $textInfo["height"];<br>                 unset($textInfo);<br>                 $mark = imagecreatefrompng($warterMark);<br>                 $imgW    =    $width;<br>                 $imgH    =    $width*$textH/$textW;<br>                 $y        =    ($height-$textH)/2;<br>                 if(function_exists("ImageCopyResampled")){<br>                     ImageCopyResampled($thumbImg,$mark,0,$y,0,0, $imgW,$imgH, $textW,$textH);<br>                 }else{<br>                     ImageCopyResized($thumbImg,$mark,0,$y,0,0,$imgW,$imgH,  $textW,$textH);<br>                 }<br>                 ImageDestroy($mark);<br>             }<br>             /*水印结束*/<br>             /*if('gif'==$type || 'png'==$type) {<br>                 //imagealphablending($thumbImg, FALSE);//取消默认的混色模式<br>                 //imagesavealpha($thumbImg,TRUE);//设定保存完整的 alpha 通道信息<br>                 $background_color  =  ImageColorAllocate($thumbImg,  0,255,0);<br>                 //  指派一个绿色<br>                 imagecolortransparent($thumbImg,$background_color);<br>                 //  设置为透明色,若注释掉该行则输出绿色的图<br>             }*/<br> <br>             // 对jpeg图形设置隔行扫描<br>             if('jpg'==$type || 'jpeg'==$type)     imageinterlace($thumbImg,$interlace);<br> <br>             // 生成图片<br>             //$imageFun = 'image'.($type=='jpg'?'jpeg':$type);<br>             $imageFun    =    'imagepng';<br>             $filename  = empty($filename)? substr($image,0,strrpos($image, '.')).$suffix.'.'.$type : $filename;<br> <br>             $imageFun($thumbImg,$filename);<br>             ImageDestroy($thumbImg);<br>             return $filename;<br>          }<br>          return false;<br> <br>     }<br>     /**<br>      +----------------------------------------------------------<br>      * 生成缩略图<br>      *<br>      +----------------------------------------------------------<br>      * @static<br>      * @access public<br>      +----------------------------------------------------------<br>      * @param string $image  原图<br>      * @param string $type 图像格式<br>      * @param string $filename 缩略图文件名<br>      * @param string $maxWidth  宽度<br>      * @param string $maxHeight  高度<br>      * @param string $position 缩略图保存目录<br>      * @param boolean $interlace 启用隔行扫描<br>      +----------------------------------------------------------<br>      * @return void<br>      +----------------------------------------------------------<br>      * @throws ThinkExecption<br>      +----------------------------------------------------------<br>      */<br>      //2007/7/18 添加水印缩略图<br>     function thumb($image,$type='',$filename='',$maxWidth=200,$maxHeight=50,$warterMark='',$interlace=true,$suffix='_thumb')<br>     {<br>         // 获取原图信息<br>         $info  = Image::getImageInfo($image);<br>         if($info !== false) {<br>             $srcWidth  = $info['width'];<br>             $srcHeight = $info['height'];<br>             $pathinfo = pathinfo($image);<br>             $type =  $pathinfo['extension'];<br>             $type = empty($type)?$info['type']:$type;<br>             $type    =    strtolower($type);<br>             $interlace  =  $interlace? 1:0;<br>             unset($info);<br>             $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // 计算缩放比例<br>             // 缩略图尺寸<br>             $width  = (int)($srcWidth*$scale);<br>             $height = (int)($srcHeight*$scale);<br>             // 载入原图<br>             $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);<br>             $srcImg     = $createFun($image);<br>             //创建缩略图<br>             if($type!='gif' && function_exists('imagecreatetruecolor'))<br>                 $thumbImg = imagecreatetruecolor($width, $height);<br>             else<br>                 $thumbImg = imagecreate($width, $height);<br> <br>             // 新建PNG缩略图通道透明处理<br>             if('png'==$type) {<br>                 imagealphablending($thumbImg, false);//取消默认的混色模式<br>                 imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息<br>             }elseif('gif'==$type) {<br>             // 新建GIF缩略图预处理,保证透明效果不失效<br>                 $background_color  =  imagecolorallocate($thumbImg,  0,255,0);  //  指派一个绿色<br>                 imagecolortransparent($thumbImg,$background_color);  //  设置为透明色,若注释掉该行则输出绿色的图<br>             }<br> <br>             // 复制图片<br>             if(function_exists("ImageCopyResampled"))<br>                 ImageCopyResampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);<br>             else<br>                 ImageCopyResized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height,  $srcWidth,$srcHeight);<br>             ImageDestroy($srcImg);<br>             /*<br>             //水印开始<br>             //计算水印的位置,默认居中<br>             $textInfo = Image::getImageInfo($warterMark);<br>             $textW    =    $textInfo["width"];<br>             $textH    =    $textInfo["height"];<br>             unset($textInfo);<br>             $mark = imagecreatefrompng($warterMark);<br>             $imgW    =    $width;<br>             $imgH    =    $width*$textH/$textW;<br>             $y        =    ($height-$textH)/2;<br>             if(function_exists("ImageCopyResampled")){<br>                 ImageCopyResampled($thumbImg,$mark,0,$y,0,0, $imgW,$imgH, $textW,$textH);<br>             }else{<br>                 ImageCopyResized($thumbImg,$mark,0,$y,0,0,$imgW,$imgH,  $textW,$textH);<br>             }<br>             ImageDestroy($mark);<br>             //水印结束<br>             */<br>             /*if('gif'==$type || 'png'==$type) {<br>                 imagealphablending($thumbImg, FALSE);//取消默认的混色模式<br>                 imagesavealpha($thumbImg,TRUE);//设定保存完整的 alpha 通道信息<br>                 $background_color  =  ImageColorAllocate($thumbImg,  0,255,0);//  指派一个绿色<br>                 imagecolortransparent($thumbImg,$background_color);//  设置为透明色,若注释掉该行则输出绿色的图<br>             }*/<br>             if('jpg'==$type || 'jpeg'==$type) {<br>                 imageinterlace($thumbImg,$interlace);// 对jpeg图形设置隔行扫描<br>             }<br>             //&nbs

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn