您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

缩略图生成

2025/12/24 16:59:09发布16次查看
自动生成缩略图,支持裁切。调用方法简单:thumb.php?url=/yovae/data/userface/000/00/00/41_middle_face.jpg?1247718988&w=20&h=20
showimg($thumbfile);
}
//不存在输出
if(copy($url,$tempfile)){
//判断图片大小 如果图片宽和高都小于要缩放的比例 直接输出
    $info    =    getimagesize($tempfile);
    //判断处理图片大约需要的内存
    $need_memory    =    (($info[0]*$info[1])/100000);
    $memory_limit    =    ini_get('memory_limit');
    if( ($need_memory > $memory_limit) && ($need_memory         ini_set('memory_limit',$need_memory.'m');
    }
if($info[0]        copy($tempfile,$thumbfile);
        $img->showimg($thumbfile,'',$info[0],$info[1]);
        unlink($tempfile);
        unlink($grab_temp_file);
        exit;
    }else{
        //生成缩图
        if($t=='c'){
            $thumb    =    $img->cutthumb($tempfile,$thumbfile,$w,$h);
        }elseif($t=='f'){
            $thumb    =    $img->thumb($tempfile,'',$thumbfile,$w,$h);
        }
        //输出缩图
        $img->showimg($thumb,'',$w,$h);
        unlink($tempfile);
        unlink($grab_temp_file);
        exit;
    }
}
//获取远程图片
function grabimage($url,$thumbdir) {
    if($url==)    return false;
    $filename    =    md5($url).strrchr($url,.);
    $img        =    file_get_contents($url);
    if(!$img)        return false;
$filepath    =    $thumbdir.$filename;
    $result        =    file_put_contents($filepath,$img);
    if($result){
        return $filepath;
    }else{
        return false;
    }
}
//检查并创建多级目录
function checkdir($path){
    $patharray = explode('/',$path);
    $nowpath = '';
    array_pop($patharray);
    foreach ($patharray as $key=>$value){
        if ( ''==$value ){
            unset($patharray[$key]);
        }else{
            if ( $key == 0 )
                $nowpath .= $value;
            else
                $nowpath .= '/'.$value;
            if ( !is_dir($nowpath) ){
                if ( !mkdir($nowpath, 0777) ) return false;
            }
        }
    }
    return true;
}
function imagecreatefrombmp($fname) {
$buf=@file_get_contents($fname);
if(strlen($buf)
    $file_header=unpack(sbftype/lbfsize/sbfreserved1/sbfreserved2/lbfoffbits,substr($buf,0,14));
if($file_header[bftype]!=19778) return false;
    $info_header=unpack(lbisize/lbiwidth/lbiheight/sbiplanes/sbibitcountlbicompression/lbisizeimage/lbixpelspermeter/lbiypelspermeter/lbiclrused/lbiclrimportant,substr($buf,14,40));
    if($info_header[bibitcountlbicompression]==2) return false;
    $line_len=round($info_header[biwidth]*$info_header[bibitcountlbicompression]/8);
    $x=$line_len%4;
    if($x>0) $line_len+=4-$x;
$img=imagecreatetruecolor($info_header[biwidth],$info_header[biheight]);
    switch($info_header[bibitcountlbicompression]){
    case 4:
    $colorset=unpack(l*,substr($buf,54,64));
    for($y=0;$y    $colors=array();
    $y_pos=$y*$line_len+$file_header[bfoffbits];
    for($x=0;$x    if($x%2)
    $colors[]=$colorset[(ord($buf[$y_pos+($x+1)/2])&0xf)+1];
    else
    $colors[]=$colorset[((ord($buf[$y_pos+$x/2+1])>>4)&0xf)+1];
    }
    imagesetstyle($img,$colors);
    imageline($img,0,$info_header[biheight]-$y-1,$info_header[biwidth],$info_header[biheight]-$y-1,img_color_styled);
    }
    break;
    case 8:
    $colorset=unpack(l*,substr($buf,54,1024));
    for($y=0;$y    $colors=array();
    $y_pos=$y*$line_len+$file_header[bfoffbits];
    for($x=0;$x    $colors[]=$colorset[ord($buf[$y_pos+$x])+1];
    }
    imagesetstyle($img,$colors);
    imageline($img,0,$info_header[biheight]-$y-1,$info_header[biwidth],$info_header[biheight]-$y-1,img_color_styled);
    }
    break;
    case 16:
    for($y=0;$y    $colors=array();
    $y_pos=$y*$line_len+$file_header[bfoffbits];
    for($x=0;$x    $i=$x*2;
    $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);
    }
    imagesetstyle($img,$colors);
    imageline($img,0,$info_header[biheight]-$y-1,$info_header[biwidth],$info_header[biheight]-$y-1,img_color_styled);
    }
    break;
    case 24:
    for($y=0;$y    $colors=array();
    $y_pos=$y*$line_len+$file_header[bfoffbits];
    for($x=0;$x    $i=$x*3;
    $colors[]=imagecolorallocate($img,ord($buf[$y_pos+$i+2]),ord($buf[$y_pos+$i+1]),ord($buf[$y_pos+$i]));
    }
    imagesetstyle($img,$colors);
    imageline($img,0,$info_header[biheight]-$y-1,$info_header[biwidth],$info_header[biheight]-$y-1,img_color_styled);
    }
    break;
    default:
    return false;
    break;
    }
    return $img;
}
function imagebmp(&$im, $filename = '', $bit = 8, $compression = 0)
{
    if (!in_array($bit, array(1, 4, 8, 16, 24, 32)))
    {
        $bit = 8;
}
    else if ($bit == 32) // todo:32 bit
    {
        $bit = 24;
    }
$bits = pow(2, $bit);
// 调整调色板
    imagetruecolortopalette($im, true, $bits);
    $width = imagesx($im);
    $height = imagesy($im);
    $colors_num = imagecolorstotal($im);
if ($bit     {
        // 颜色索引
        $rgb_quad = '';
        for ($i = 0; $i         {
            $colors = imagecolorsforindex($im, $i);
            $rgb_quad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . \0;         }
// 位图数据
        $bmp_data = '';
// 非压缩
        if ($compression == 0 || $bit         {
            if (!in_array($bit, array(1, 4, 8)))
            {
                $bit = 8;
            }
$compression = 0;
// 每行字节数必须为4的倍数,补齐。
$extra = '';
            $padding = 4 - ceil($width / (8 / $bit)) % 4;
            if ($padding % 4 != 0)
            {
                $extra = str_repeat(\0, $padding);
            }
for ($j = $height - 1; $j >= 0; $j --)
            {
                $i = 0;
                while ($i                 {
                    $bin = 0;
                    $limit = $width - $i 
                    for ($k = 8 - $bit; $k >= $limit; $k -= $bit)
                    {
                        $index = imagecolorat($im, $i, $j);
                        $bin |= $index                         $i ++;
                    }
$bmp_data .= chr($bin);
                }
$bmp_data .= $extra;
            }
        }
        // rle8 压缩
        else if ($compression == 1 && $bit == 8)
        {
            for ($j = $height - 1; $j >= 0; $j --)
            {
                $last_index = \0;
                $same_num   = 0;
                for ($i = 0; $i                 {
                    $index = imagecolorat($im, $i, $j);
                    if ($index !== $last_index || $same_num > 255)
                    {
                        if ($same_num != 0)
                        {
                            $bmp_data .= chr($same_num) . chr($last_index);
                        }
$last_index = $index;
                        $same_num = 1;
                    }
                    else
                    {
                        $same_num ++;
                    }
                }
$bmp_data .= \0\0;
            }
$bmp_data .= \0\1;
        }
$size_quad = strlen($rgb_quad);
        $size_data = strlen($bmp_data);
    }
    else
    {
        // 每行字节数必须为4的倍数,补齐。
        $extra = '';
        $padding = 4 - ($width * ($bit / 8)) % 4;
        if ($padding % 4 != 0)
        {
            $extra = str_repeat(\0, $padding);
        }
// 位图数据
        $bmp_data = '';
for ($j = $height - 1; $j >= 0; $j --)
        {
            for ($i = 0; $i             {
                $index = imagecolorat($im, $i, $j);
                $colors = imagecolorsforindex($im, $index);
if ($bit == 16)
                {
                    $bin = 0 
                    $bin |= ($colors['red'] >> 3)                     $bin |= ($colors['green'] >> 3)                     $bin |= $colors['blue'] >> 3;
$bmp_data .= pack(v, $bin);
                }
                else
                {
                    $bmp_data .= pack(c*, $colors['blue'], $colors['green'], $colors['red']);
                }
// todo: 32bit;
            }
$bmp_data .= $extra;
        }
$size_quad = 0;
        $size_data = strlen($bmp_data);
        $colors_num = 0;
    }
// 位图文件头
    $file_header = bm . pack(v3, 54 + $size_quad + $size_data, 0, 54 + $size_quad);
// 位图信息头
    $info_header = pack(v3v2v*, 0x28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0);
    // 写入文件
    if ($filename != '')
    {
        $fp = fopen(test.bmp, wb);
fwrite($fp, $file_header);
        fwrite($fp, $info_header);
        fwrite($fp, $rgb_quad);
        fwrite($fp, $bmp_data);
        fclose($fp);
return 1;
    }
// 浏览器输出
    header(content-type: image/bmp);
    echo $file_header . $info_header;
    echo $rgb_quad;
    echo $bmp_data;
return 1;
}
class image
{//类定义开始
/**
     +----------------------------------------------------------
     * 架构函数
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    function __construct()
    {
}
/**
     +----------------------------------------------------------
     * 取得图像信息
     *
     +----------------------------------------------------------
     * @static
     * @access public
     +----------------------------------------------------------
     * @param string $image 图像文件名
     +----------------------------------------------------------
     * @return mixed
     +----------------------------------------------------------
     */
    function getimageinfo($img) {
        $imageinfo = getimagesize($img);
        if( $imageinfo!== false) {
if(function_exists(image_type_to_extension)){
                $imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1));
            }else{
                $imagetype = strtolower(substr($img,strrpos($img,'.')+1));
            }
$imagesize = filesize($img);
            $info = array(
                width=>$imageinfo[0],
                height=>$imageinfo[1],
                type=>$imagetype,
                size=>$imagesize,
                mime=>$imageinfo['mime']
            );
            return $info;
        }else {
            return false;
        }
    }
/**
     +----------------------------------------------------------
     * 显示服务器图像文件
     * 支持url方式
     +----------------------------------------------------------
     * @static
     * @access public
     +----------------------------------------------------------
     * @param string $imgfile 图像文件名
     * @param string $text 文字字符串
     * @param string $width 图像宽度
     * @param string $height 图像高度
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
    function showimg($imgfile,$text='',$width=80,$height=30) {
        //获取图像文件信息
        //2007/6/26 增加图片水印输出,$text为图片的完整路径即可
        $info = image::getimageinfo($imgfile);
        if($info !== false) {
            $createfun  =   str_replace('/','createfrom',$info['mime']);
            $im = $createfun($imgfile);
            if($im) {
                $imagefun= str_replace('/','',$info['mime']);
                //水印开始
                if(!empty($text)) {
                    $tc  = imagecolorallocate($im, 0, 0, 0);
                    if(is_file($text)&&file_exists($text)){
                        // 取得水印信息
                        $textinfo = image::getimageinfo($text);
                        $createfun2= str_replace('/','createfrom',$textinfo['mime']);
                        $watermark = $createfun2($text);
                        $imgw    =    $info[width];
                        $imgh    =    $info[width]*$textinfo[height]/$textinfo[width];
                        $y    =    ($info[height]-$textinfo[height])/2;
                        if(function_exists(imagecopyresampled))
                            imagecopyresampled($im,$watermark,0,$y,0,0, $imgw,$imgh, $textinfo[width],$textinfo[height]);
                        else
                            imagecopyresized($im,$watermark,0,$y,0,0,$imgw,$imgh,  $textinfo[width],$textinfo[height]);
                    }else{
                        imagestring($im, 3, 5, 5, $text, $tc);
                    }
                    //imagedestroy($tc);
                }
                //水印结束
                if($info['type']=='png' || $info['type']=='gif') {
                imagealphablending($im, false);//取消默认的混色模式
                imagesavealpha($im,true);//设定保存完整的 alpha 通道信息
                }
                header(content-type: .$info['mime']);
                $imagefun($im);
                @imagedestroy($im);
                return ;
            }
        }
        //获取或者创建图像文件失败则生成空白png图片
        $im  = imagecreatetruecolor($width, $height);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
        imagestring($im, 4, 5, 5, no pic, $tc);
        image::output($im);
        return ;
    }
// 切割缩图cutthumb
    // 2007/6/15
    function cutthumb($image,$filename='',$maxwidth='200',$maxheight='50',$wartermark='',$type='',$interlace=true,$suffix='_thumb')
    {
        // 获取原图信息
        $info  = image::getimageinfo($image);
         if($info !== false) {
            $srcwidth  = $info['width'];
            $srcheight = $info['height'];
            $pathinfo = pathinfo($image);
            $type =  $pathinfo['extension'];
            $type = empty($type)?$info['type']:$type;
            $type    =    strtolower($type);
            $interlace  =  $interlace? 1:0;
            unset($info);
            // 载入原图
            $createfun = 'imagecreatefrom'.($type=='jpg'?'jpeg':$type);
            $srcimg     = $createfun($image);
//创建缩略图
            if($type!='gif' && function_exists('imagecreatetruecolor'))
                $thumbimg = imagecreatetruecolor($maxwidth, $maxheight);
            else
                $thumbimg = imagecreate($maxwidth, $maxheight);
// 新建png缩略图通道透明处理
            if('png'==$type) {
                imagealphablending($thumbimg, false);//取消默认的混色模式
                imagesavealpha($thumbimg,true);//设定保存完整的 alpha 通道信息
            }elseif('gif'==$type) {
            // 新建gif缩略图预处理,保证透明效果不失效
                $background_color  =  imagecolorallocate($thumbimg,  0,255,0);  //  指派一个绿色
                imagecolortransparent($thumbimg,$background_color);  //  设置为透明色,若注释掉该行则输出绿色的图
            }
// 计算缩放比例
            if(($maxwidth/$maxheight)>=($srcwidth/$srcheight)){
                //宽不变,截高,从中间截取 y=
                $width    =    $srcwidth;
                $height    =    $srcwidth*($maxheight/$maxwidth);
                $x        =    0;
                $y        =    ($srcheight-$height)*0.5;
            }else{
                //高不变,截宽,从中间截取,x=
                $width    =    $srcheight*($maxwidth/$maxheight);
                $height    =    $srcheight;
                $x        =    ($srcwidth-$width)*0.5;
                $y        =    0;
            }
            // 复制图片
            if(function_exists(imagecopyresampled)){
                imagecopyresampled($thumbimg, $srcimg, 0, 0, $x, $y, $maxwidth, $maxheight, $width,$height);
            }else{
                imagecopyresized($thumbimg, $srcimg, 0, 0, $x, $y, $maxwidth, $maxheight,  $width,$height);
            }
            imagedestroy($srcimg);
            /*水印开始* /
            if($wartermark){
                //计算水印的位置,默认居中
                $textinfo = image::getimageinfo($wartermark);
                $textw    =    $textinfo[width];
                $texth    =    $textinfo[height];
                unset($textinfo);
                $mark = imagecreatefrompng($wartermark);
                $imgw    =    $width;
                $imgh    =    $width*$texth/$textw;
                $y        =    ($height-$texth)/2;
                if(function_exists(imagecopyresampled)){
                    imagecopyresampled($thumbimg,$mark,0,$y,0,0, $imgw,$imgh, $textw,$texth);
                }else{
                    imagecopyresized($thumbimg,$mark,0,$y,0,0,$imgw,$imgh,  $textw,$texth);
                }
                imagedestroy($mark);
            }
            /*水印结束*/
            /*if('gif'==$type || 'png'==$type) {
                //imagealphablending($thumbimg, false);//取消默认的混色模式
                //imagesavealpha($thumbimg,true);//设定保存完整的 alpha 通道信息
                $background_color  =  imagecolorallocate($thumbimg,  0,255,0);
                //  指派一个绿色
                imagecolortransparent($thumbimg,$background_color);
                //  设置为透明色,若注释掉该行则输出绿色的图
            }*/
// 对jpeg图形设置隔行扫描
            if('jpg'==$type || 'jpeg'==$type)     imageinterlace($thumbimg,$interlace);
// 生成图片
            //$imagefun = 'image'.($type=='jpg'?'jpeg':$type);
            $imagefun    =    'imagepng';
            $filename  = empty($filename)? substr($image,0,strrpos($image, '.')).$suffix.'.'.$type : $filename;
$imagefun($thumbimg,$filename);
            imagedestroy($thumbimg);
            return $filename;
         }
         return false;
}
    /**
     +----------------------------------------------------------
     * 生成缩略图
     *
     +----------------------------------------------------------
     * @static
     * @access public
     +----------------------------------------------------------
     * @param string $image  原图
     * @param string $type 图像格式
     * @param string $filename 缩略图文件名
     * @param string $maxwidth  宽度
     * @param string $maxheight  高度
     * @param string $position 缩略图保存目录
     * @param boolean $interlace 启用隔行扫描
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws thinkexecption
     +----------------------------------------------------------
     */
     //2007/7/18 添加水印缩略图
    function thumb($image,$type='',$filename='',$maxwidth=200,$maxheight=50,$wartermark='',$interlace=true,$suffix='_thumb')
    {
        // 获取原图信息
        $info  = image::getimageinfo($image);
        if($info !== false) {
            $srcwidth  = $info['width'];
            $srcheight = $info['height'];
            $pathinfo = pathinfo($image);
            $type =  $pathinfo['extension'];
            $type = empty($type)?$info['type']:$type;
            $type    =    strtolower($type);
            $interlace  =  $interlace? 1:0;
            unset($info);
            $scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); // 计算缩放比例
            // 缩略图尺寸
            $width  = (int)($srcwidth*$scale);
            $height = (int)($srcheight*$scale);
            // 载入原图
            $createfun = 'imagecreatefrom'.($type=='jpg'?'jpeg':$type);
            $srcimg     = $createfun($image);
            //创建缩略图
            if($type!='gif' && function_exists('imagecreatetruecolor'))
                $thumbimg = imagecreatetruecolor($width, $height);
            else
                $thumbimg = imagecreate($width, $height);
// 新建png缩略图通道透明处理
            if('png'==$type) {
                imagealphablending($thumbimg, false);//取消默认的混色模式
                imagesavealpha($thumbimg,true);//设定保存完整的 alpha 通道信息
            }elseif('gif'==$type) {
            // 新建gif缩略图预处理,保证透明效果不失效
                $background_color  =  imagecolorallocate($thumbimg,  0,255,0);  //  指派一个绿色
                imagecolortransparent($thumbimg,$background_color);  //  设置为透明色,若注释掉该行则输出绿色的图
            }
// 复制图片
            if(function_exists(imagecopyresampled))
                imagecopyresampled($thumbimg, $srcimg, 0, 0, 0, 0, $width, $height, $srcwidth,$srcheight);
            else
                imagecopyresized($thumbimg, $srcimg, 0, 0, 0, 0, $width, $height,  $srcwidth,$srcheight);
            imagedestroy($srcimg);
            /*
            //水印开始
            //计算水印的位置,默认居中
            $textinfo = image::getimageinfo($wartermark);
            $textw    =    $textinfo[width];
            $texth    =    $textinfo[height];
            unset($textinfo);
            $mark = imagecreatefrompng($wartermark);
            $imgw    =    $width;
            $imgh    =    $width*$texth/$textw;
            $y        =    ($height-$texth)/2;
            if(function_exists(imagecopyresampled)){
                imagecopyresampled($thumbimg,$mark,0,$y,0,0, $imgw,$imgh, $textw,$texth);
            }else{
                imagecopyresized($thumbimg,$mark,0,$y,0,0,$imgw,$imgh,  $textw,$texth);
            }
            imagedestroy($mark);
            //水印结束
            */
            /*if('gif'==$type || 'png'==$type) {
                imagealphablending($thumbimg, false);//取消默认的混色模式
                imagesavealpha($thumbimg,true);//设定保存完整的 alpha 通道信息
                $background_color  =  imagecolorallocate($thumbimg,  0,255,0);//  指派一个绿色
                imagecolortransparent($thumbimg,$background_color);//  设置为透明色,若注释掉该行则输出绿色的图
            }*/
            if('jpg'==$type || 'jpeg'==$type) {
                imageinterlace($thumbimg,$interlace);// 对jpeg图形设置隔行扫描
            }
            //&nbs
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product