搜索
首页php教程PHP源码PHP缩略图 等比例无损压缩,可填充空白区域补充色

php代码

<?php
error_reporting( E_ALL );

// 测试
imagezoom(&#39;1.jpg&#39;, &#39;2.jpg&#39;, 400, 300, &#39;#FFFFFF&#39;);

/*
    php缩略图函数:
        等比例无损压缩,可填充补充色 author: 华仔
    主持格式:
        bmp 、jpg 、gif、png
    param:
        @srcimage : 要缩小的图片
        @dstimage : 要保存的图片
        @dst_width: 缩小宽
        @dst_height: 缩小高
        @backgroundcolor: 补充色  如:#FFFFFF  支持 6位  不支持3位
*/
function imagezoom( $srcimage, $dstimage,  $dst_width, $dst_height, $backgroundcolor ) {
        
        // 中文件名乱码
        if ( PHP_OS == &#39;WINNT&#39; ) {
                $srcimage = iconv(&#39;UTF-8&#39;, &#39;GBK&#39;, $srcimage);
                $dstimage = iconv(&#39;UTF-8&#39;, &#39;GBK&#39;, $dstimage);
        }
    
    $dstimg = imagecreatetruecolor( $dst_width, $dst_height );
    $color = imagecolorallocate($dstimg
        , hexdec(substr($backgroundcolor, 1, 2))
        , hexdec(substr($backgroundcolor, 3, 2))
        , hexdec(substr($backgroundcolor, 5, 2))
    );
    imagefill($dstimg, 0, 0, $color);
    
    if ( !$arr=getimagesize($srcimage) ) {
                echo "要生成缩略图的文件不存在";
                exit;
        }
        
    $src_width = $arr[0];
    $src_height = $arr[1];
    $srcimg = null;
    $method = getcreatemethod( $srcimage );
    if ( $method ) {
        eval( &#39;$srcimg = &#39; . $method . &#39;;&#39; );
    }
    
    $dst_x = 0;
    $dst_y = 0;
    $dst_w = $dst_width;
    $dst_h = $dst_height;
    if ( ($dst_width / $dst_height - $src_width / $src_height) > 0 ) {
        $dst_w = $src_width * ( $dst_height / $src_height );
        $dst_x = ( $dst_width - $dst_w ) / 2;
    } elseif ( ($dst_width / $dst_height - $src_width / $src_height) < 0 ) {
        $dst_h = $src_height * ( $dst_width / $src_width );
        $dst_y = ( $dst_height - $dst_h ) / 2;
    }

    imagecopyresampled($dstimg, $srcimg, $dst_x
        , $dst_y, 0, 0, $dst_w, $dst_h, $src_width, $src_height);
    
    // 保存格式
    $arr = array(
        &#39;jpg&#39; => &#39;imagejpeg&#39;
        , &#39;jpeg&#39; => &#39;imagejpeg&#39;
        , &#39;png&#39; => &#39;imagepng&#39;
        , &#39;gif&#39; => &#39;imagegif&#39;
        , &#39;bmp&#39; => &#39;imagebmp&#39;
    );
    $suffix = strtolower( array_pop(explode(&#39;.&#39;, $dstimage ) ) );
    if (!in_array($suffix, array_keys($arr)) ) {
        echo "保存的文件名错误";
        exit;
    } else {
        eval( $arr[$suffix] . &#39;($dstimg, "&#39;.$dstimage.&#39;");&#39; );
    }
    
    imagejpeg($dstimg, $dstimage);
    
    imagedestroy($dstimg);
    imagedestroy($srcimg);
    
}


function getcreatemethod( $file ) {
        $arr = array(
                &#39;474946&#39; => "imagecreatefromgif(&#39;$file&#39;)"
                , &#39;FFD8FF&#39; => "imagecreatefromjpeg(&#39;$file&#39;)"
                , &#39;424D&#39; => "imagecreatefrombmp(&#39;$file&#39;)"
                , &#39;89504E&#39; => "imagecreatefrompng(&#39;$file&#39;)"
        );
        $fd = fopen( $file, "rb" );
        $data = fread( $fd, 3 );
        
        $data = str2hex( $data );
        
        if ( array_key_exists( $data, $arr ) ) {
                return $arr[$data];
        } elseif ( array_key_exists( substr($data, 0, 4), $arr ) ) {
                return $arr[substr($data, 0, 4)];
        } else {
                return false;
        }
}

function str2hex( $str ) {
        $ret = "";
        
        for( $i = 0; $i < strlen( $str ) ; $i++ ) {
                $ret .= ord($str[$i]) >= 16 ? strval( dechex( ord($str[$i]) ) )
                        : &#39;0&#39;. strval( dechex( ord($str[$i]) ) );
        }
        
        return strtoupper( $ret );
}

// BMP 创建函数  php本身无
function imagecreatefrombmp($filename)
{
   if (! $f1 = fopen($filename,"rb")) return FALSE;

   $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
   if ($FILE[&#39;file_type&#39;] != 19778) return FALSE;

   $BMP = unpack(&#39;Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel&#39;.
                 &#39;/Vcompression/Vsize_bitmap/Vhoriz_resolution&#39;.
                 &#39;/Vvert_resolution/Vcolors_used/Vcolors_important&#39;, fread($f1,40));
   $BMP[&#39;colors&#39;] = pow(2,$BMP[&#39;bits_per_pixel&#39;]);
   if ($BMP[&#39;size_bitmap&#39;] == 0) $BMP[&#39;size_bitmap&#39;] = $FILE[&#39;file_size&#39;] - $FILE[&#39;bitmap_offset&#39;];
   $BMP[&#39;bytes_per_pixel&#39;] = $BMP[&#39;bits_per_pixel&#39;]/8;
   $BMP[&#39;bytes_per_pixel2&#39;] = ceil($BMP[&#39;bytes_per_pixel&#39;]);
   $BMP[&#39;decal&#39;] = ($BMP[&#39;width&#39;]*$BMP[&#39;bytes_per_pixel&#39;]/4);
   $BMP[&#39;decal&#39;] -= floor($BMP[&#39;width&#39;]*$BMP[&#39;bytes_per_pixel&#39;]/4);
   $BMP[&#39;decal&#39;] = 4-(4*$BMP[&#39;decal&#39;]);
   if ($BMP[&#39;decal&#39;] == 4) $BMP[&#39;decal&#39;] = 0;
   
   $PALETTE = array();
   if ($BMP[&#39;colors&#39;] < 16777216)
   {
    $PALETTE = unpack(&#39;V&#39;.$BMP[&#39;colors&#39;], fread($f1,$BMP[&#39;colors&#39;]*4));
   }
   
   $IMG = fread($f1,$BMP[&#39;size_bitmap&#39;]);
   $VIDE = chr(0);

   $res = imagecreatetruecolor($BMP[&#39;width&#39;],$BMP[&#39;height&#39;]);
   $P = 0;
   $Y = $BMP[&#39;height&#39;]-1;
   while ($Y >= 0)
   {
        $X=0;
        while ($X < $BMP[&#39;width&#39;])
        {
         if ($BMP[&#39;bits_per_pixel&#39;] == 24)
            $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
         elseif ($BMP[&#39;bits_per_pixel&#39;] == 16)
         {  
            $COLOR = unpack("n",substr($IMG,$P,2));
            $COLOR[1] = $PALETTE[$COLOR[1]+1];
         }
         elseif ($BMP[&#39;bits_per_pixel&#39;] == 8)
         {  
            $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
            $COLOR[1] = $PALETTE[$COLOR[1]+1];
         }
         elseif ($BMP[&#39;bits_per_pixel&#39;] == 4)
         {
            $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
            if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
            $COLOR[1] = $PALETTE[$COLOR[1]+1];
         }
         elseif ($BMP[&#39;bits_per_pixel&#39;] == 1)
         {
            $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
            if     (($P*8)%8 == 0) $COLOR[1] =  $COLOR[1]        >>7;
            elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
            elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
            elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
            elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
            elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
            elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
            elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
            $COLOR[1] = $PALETTE[$COLOR[1]+1];
         }
         else
            return FALSE;
         imagesetpixel($res,$X,$Y,$COLOR[1]);
         $X++;
         $P += $BMP[&#39;bytes_per_pixel&#39;];
        }
        $Y--;
        $P+=$BMP[&#39;decal&#39;];
   }
   fclose($f1);

return $res;
}
// BMP 保存函数,php本身无
function imagebmp ($im, $fn = false)
{
    if (!$im) return false;
            
    if ($fn === false) $fn = &#39;php://output&#39;;
    $f = fopen ($fn, "w");
    if (!$f) return false;
            
    $biWidth = imagesx ($im);
    $biHeight = imagesy ($im);
    $biBPLine = $biWidth * 3;
    $biStride = ($biBPLine + 3) & ~3;
    $biSizeImage = $biStride * $biHeight;
    $bfOffBits = 54;
    $bfSize = $bfOffBits + $biSizeImage;
            
    fwrite ($f, &#39;BM&#39;, 2);
    fwrite ($f, pack (&#39;VvvV&#39;, $bfSize, 0, 0, $bfOffBits));
            
    fwrite ($f, pack (&#39;VVVvvVVVVVV&#39;, 40, $biWidth, $biHeight, 1, 24, 0, $biSizeImage, 0, 0, 0, 0));
            
    $numpad = $biStride - $biBPLine;
    for ($y = $biHeight - 1; $y >= 0; --$y)
    {
        for ($x = 0; $x < $biWidth; ++$x)
        {
            $col = imagecolorat ($im, $x, $y);
            fwrite ($f, pack (&#39;V&#39;, $col), 3);
        }
        for ($i = 0; $i < $numpad; ++$i)
            fwrite ($f, pack (&#39;C&#39;, 0));
    }
    fclose ($f);
    return true;
}

?>
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能