最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP縮略圖等比例無(wú)損壓縮,可填充空白區(qū)域補(bǔ)充色

 更新時(shí)間:2011年06月10日 00:27:29   作者:  
PHP縮略圖 等比例無(wú)損壓縮,可填充空白區(qū)域補(bǔ)充色的實(shí)現(xiàn)代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:

<?php
error_reporting( E_ALL );
// 測(cè)試
imagezoom('1.jpg', '2.jpg', 400, 300, '#FFFFFF');
/*
php縮略圖函數(shù):
等比例無(wú)損壓縮,可填充補(bǔ)充色 author: 華仔
主持格式:
bmp 、jpg 、gif、png
param:
@srcimage : 要縮小的圖片
@dstimage : 要保存的圖片
@dst_width: 縮小寬
@dst_height: 縮小高
@backgroundcolor: 補(bǔ)充色 如:#FFFFFF 支持 6位 不支持3位
*/
function imagezoom( $srcimage, $dstimage, $dst_width, $dst_height, $backgroundcolor ) {
// 中文件名亂碼
if ( PHP_OS == 'WINNT' ) {
$srcimage = iconv('UTF-8', 'GBK', $srcimage);
$dstimage = iconv('UTF-8', 'GBK', $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( '$srcimg = ' . $method . ';' );
}
$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(
'jpg' => 'imagejpeg'
, 'jpeg' => 'imagejpeg'
, 'png' => 'imagepng'
, 'gif' => 'imagegif'
, 'bmp' => 'imagebmp'
);
$suffix = strtolower( array_pop(explode('.', $dstimage ) ) );
if (!in_array($suffix, array_keys($arr)) ) {
echo "保存的文件名錯(cuò)誤";
exit;
} else {
eval( $arr[$suffix] . '($dstimg, "'.$dstimage.'");' );
}
imagejpeg($dstimg, $dstimage);
imagedestroy($dstimg);
imagedestroy($srcimg);
}
function getcreatemethod( $file ) {
$arr = array(
'474946' => "imagecreatefromgif('$file')"
, 'FFD8FF' => "imagecreatefromjpeg('$file')"
, '424D' => "imagecreatefrombmp('$file')"
, '89504E' => "imagecreatefrompng('$file')"
);
$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]) ) )
: '0'. strval( dechex( ord($str[$i]) ) );
}
return strtoupper( $ret );
}
// BMP 創(chuàng)建函數(shù) php本身無(wú)
function imagecreatefrombmp($filename)
{
if (! $f1 = fopen($filename,"rb")) return FALSE;
$FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
if ($FILE['file_type'] != 19778) return FALSE;
$BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
'/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
'/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
$BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
$BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
$BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
$BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
$BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
$BMP['decal'] = 4-(4*$BMP['decal']);
if ($BMP['decal'] == 4) $BMP['decal'] = 0;
$PALETTE = array();
if ($BMP['colors'] < 16777216)
{
$PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
}
$IMG = fread($f1,$BMP['size_bitmap']);
$VIDE = chr(0);
$res = imagecreatetruecolor($BMP['width'],$BMP['height']);
$P = 0;
$Y = $BMP['height']-1;
while ($Y >= 0)
{
$X=0;
while ($X < $BMP['width'])
{
if ($BMP['bits_per_pixel'] == 24)
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
elseif ($BMP['bits_per_pixel'] == 16)
{
$COLOR = unpack("n",substr($IMG,$P,2));
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 8)
{
$COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 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['bits_per_pixel'] == 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['bytes_per_pixel'];
}
$Y--;
$P+=$BMP['decal'];
}
fclose($f1);
return $res;
}
// BMP 保存函數(shù),php本身無(wú)
function imagebmp ($im, $fn = false)
{
if (!$im) return false;
if ($fn === false) $fn = 'php://output';
$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, 'BM', 2);
fwrite ($f, pack ('VvvV', $bfSize, 0, 0, $bfOffBits));
fwrite ($f, pack ('VVVvvVVVVVV', 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 ('V', $col), 3);
}
for ($i = 0; $i < $numpad; ++$i)
fwrite ($f, pack ('C', 0));
}
fclose ($f);
return true;
}
?>

相關(guān)文章

  • 詳解EventDispatcher事件分發(fā)組件

    詳解EventDispatcher事件分發(fā)組件

    Symfony EventDispatcher以一個(gè)簡(jiǎn)單有效的方式實(shí)現(xiàn)了中介者模式,事件分發(fā)器就是那個(gè)中介,讓系統(tǒng)和插件不會(huì)耦合在一起,這讓上面的插件系統(tǒng)成為可能,而且他會(huì)讓你的項(xiàng)目可擴(kuò)展性更好。本文將對(duì)此進(jìn)行詳細(xì)介紹,需要的朋友一起來(lái)看下吧
    2016-12-12
  • PHP從零開(kāi)始打造自己的MVC框架之類的自動(dòng)加載實(shí)現(xiàn)方法詳解

    PHP從零開(kāi)始打造自己的MVC框架之類的自動(dòng)加載實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了PHP從零開(kāi)始打造自己的MVC框架之類的自動(dòng)加載實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式詳細(xì)分析了MVC框架類的自動(dòng)加載原理、定義、實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下
    2019-06-06
  • PHP的PDO操作簡(jiǎn)單示例

    PHP的PDO操作簡(jiǎn)單示例

    這篇文章主要介紹了PHP的PDO操作,以簡(jiǎn)單示例形式分析了PHP操作PDO的簡(jiǎn)單連接,初始化及查詢,插入等操作技巧,需要的朋友可以參考下
    2016-03-03
  • PHPExcel實(shí)現(xiàn)的讀取多工作表操作示例

    PHPExcel實(shí)現(xiàn)的讀取多工作表操作示例

    這篇文章主要介紹了PHPExcel實(shí)現(xiàn)的讀取多工作表操作,結(jié)合實(shí)例形式分析了PHPExcel針對(duì)Excel多個(gè)sheet工作表的讀取、解析相關(guān)操作技巧,需要的朋友可以參考下
    2020-04-04
  • PHP中限制IP段訪問(wèn)、禁止IP提交表單的代碼

    PHP中限制IP段訪問(wèn)、禁止IP提交表單的代碼

    最近,小編發(fā)現(xiàn)有一個(gè)云南的網(wǎng)友經(jīng)常在網(wǎng)站發(fā)表一些垃圾信息的評(píng)論,由于使用的事DEDECMS構(gòu)架,系統(tǒng)本身并無(wú)禁止IP功能,每天看到這些垃圾評(píng)論,盡管不多,但是讓人感覺(jué)不爽,那么如何來(lái)限制呢?
    2011-04-04
  • 深入PHP內(nèi)存相關(guān)的功能特性詳解

    深入PHP內(nèi)存相關(guān)的功能特性詳解

    本篇文章是對(duì)PHP中內(nèi)存相關(guān)的功能特性進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • php上傳圖片之時(shí)間戳命名(保存路徑)

    php上傳圖片之時(shí)間戳命名(保存路徑)

    這篇文章主要介紹了php中上傳圖片文件,并且以上傳時(shí)的時(shí)間戳命名文件,并將文件的路徑存在session中以便使用,需要的朋友可以參考下
    2014-08-08
  • 在PHP中利用wsdl創(chuàng)建標(biāo)準(zhǔn)webservice的實(shí)現(xiàn)代碼

    在PHP中利用wsdl創(chuàng)建標(biāo)準(zhǔn)webservice的實(shí)現(xiàn)代碼

    網(wǎng)上有現(xiàn)成的nusoap,我沒(méi)使用,如果使用了,我可能就不知道PHP是怎么創(chuàng)建webservice的了
    2011-12-12
  • sae使用smarty模板的方法

    sae使用smarty模板的方法

    這篇文章主要介紹了sae使用smarty模板的方法,大家參考使用吧
    2013-12-12
  • 對(duì)text數(shù)據(jù)類型不支持代碼頁(yè)轉(zhuǎn)換 從: 1252 到: 936

    對(duì)text數(shù)據(jù)類型不支持代碼頁(yè)轉(zhuǎn)換 從: 1252 到: 936

    錯(cuò)誤的提示同樣是不能從text的轉(zhuǎn)換問(wèn)題:這主要是由于數(shù)據(jù)庫(kù)在設(shè)計(jì)的時(shí)候的數(shù)據(jù)類型存在Text——而我們采用的是中文操作系統(tǒng)。檢查數(shù)據(jù)庫(kù)的腳本,修改Text為ntext。支持unicode。
    2011-04-04

最新評(píng)論

轮台县| 柘城县| 永善县| 阳江市| 延寿县| 德州市| 仁怀市| 满洲里市| 溧水县| 镇原县| 海阳市| 通辽市| 潜山县| 武威市| 乡宁县| 抚州市| 汨罗市| 井研县| 杭锦旗| 克什克腾旗| 西盟| 阳曲县| 嵊州市| 建阳市| 永安市| 东宁县| 无棣县| 南宫市| 陆川县| 曲沃县| 神木县| 马龙县| 梅州市| 延吉市| 监利县| 九台市| 四会市| 桓仁| 城口县| 太谷县| 瑞丽市|