考虑到页面加载速度和页面布局的需要,很多时候要将用户上传的图片进行缩略,下面这个函数分享出来,希望对大家有用。
它支持gif、jpg、png和bmp几种图片格式,不过bmp图像需要ImageCreateFromBMP函数支持。
/** * $srcFile - 图形文件 * $pixel - 尺寸大小,如:400*300 * $_quality - 图片质量,默认75 * $cut - 是否裁剪,默认1,当$cut=0的时候,将不进行裁剪 * $cache - 如果有缓存,是否直接用缓存,默认true * 示例:"< img src=\"".MiniImg('images/image.jpg','300*180',72,0)."\">" * */ function MiniImg($srcFile, $pixel, $_quality = 75, $cut=1, $cache=true){ $_type = strtolower(substr(strrchr($srcFile,"."),1)); $pixelInfo = explode('*', $pixel); $pathInfo = pathinfo($srcFile); $_cut = intval($cut); $searchFileName = preg_replace("/\.([A-Za-z0-9]+)$/isU", "_".$pixelInfo[0]."x".$pixelInfo[1]."_".$cut.".\", $pathInfo['basename']); $miniFile = $pathInfo['dirname'].'/'.$searchFileName; if($cache and file_exists($miniFile)) return $miniFile; $data = GetImageSize($srcFile); $FuncExists = 1; switch ($data[2]) { case 1: //gif if(function_exists('ImageCreateFromGIF')) $_im = ImageCreateFromGIF($srcFile); break; case 2: //jpg if(function_exists('imagecreatefromjpeg')) $_im = imagecreatefromjpeg($srcFile); break; case 3: //png if(function_exists('ImageCreateFromPNG')) $_im = ImageCreateFromPNG($srcFile); break; case 6: //bmp,这里需要用到ImageCreateFromBMP $_im = ImageCreateFromBMP($srcFile); break; } if(!$_im) return $srcFile; $sizeInfo['width'] = @imagesx($_im); $sizeInfo['height'] = @imagesy($_im); if(!$sizeInfo['width'] or !$sizeInfo['height']) return $srcFile; if($sizeInfo['width'] == $pixelInfo[0] && $sizeInfo['height'] == $pixelInfo[1] ) { return $srcFile; }elseif($sizeInfo['width'] < $pixelInfo[0] && $sizeInfo['height'] < $pixelInfo[1] && $miniMode=='2'){ return $srcFile; }else{ $resize_ratio = ($pixelInfo[0])/($pixelInfo[1]); $ratio = ($sizeInfo['width'])/($sizeInfo['height']); if($cut==1){ $newimg = imagecreatetruecolor($pixelInfo[0],$pixelInfo[1]); if($ratio>=$resize_ratio){ //高度优先 imagecopyresampled($newimg, $_im, 0, 0, 0, 0, $pixelInfo[0],$pixelInfo[1], (($sizeInfo['height'])*$resize_ratio), $sizeInfo['height']); $_result = ImageJpeg ($newimg,$miniFile, $_quality); }else{ //宽度优先 imagecopyresampled($newimg, $_im, 0, 0, 0, 0, $pixelInfo[0], $pixelInfo[1], $sizeInfo['width'], (($sizeInfo['width'])/$resize_ratio)); $_result = ImageJpeg ($newimg,$miniFile, $_quality); } }else{ //不裁图 if($ratio>=$resize_ratio){ $newimg = imagecreatetruecolor($pixelInfo[0],($pixelInfo[0])/$ratio); imagecopyresampled($newimg, $_im, 0, 0, 0, 0, $pixelInfo[0], ($pixelInfo[0])/$ratio, $sizeInfo['width'], $sizeInfo['height']); $_result = ImageJpeg ($newimg,$miniFile, $_quality); }else{ $newimg = imagecreatetruecolor(($pixelInfo[1])*$ratio,$pixelInfo[1]); imagecopyresampled($newimg, $_im, 0, 0, 0, 0, ($pixelInfo[1])*$ratio, $pixelInfo[1], $sizeInfo['width'], $sizeInfo['height']); $_result = ImageJpeg ($newimg,$miniFile, $_quality); } } ImageDestroy($_im); ImageDestroy($newimg); if($_result) return $miniFile; return $srcFile; } }
以下是ImageCreateFromBMP函数
ImageCreateFromBMP() – 支持BMP图片函数
/** * ImageCreateFromBMP() - 支持BMP图片函数 * $filename - BMP图形文件 * */ function ImageCreateFromBMP( $filename ) { // Ouverture du fichier en mode binaire if ( ! $f1 = @fopen ($filename, "rb")) return FALSE ; // 1 : Chargement des ent?tes FICHIER $FILE = unpack ( "vfile_type/Vfile_size/Vreserved/Vbitmap_offset" , fread($f1 ,14)); if ( $FILE ['file_type'] != 19778 ) return FALSE ; // 2 : Chargement des ent?tes BMP $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 ; // 3 : Chargement des couleurs de la palette $PALETTE = array (); if ( $BMP ['colors'] < 16777216 ){ $PALETTE = unpack ( 'V' . $BMP ['colors'] , fread ( $f1 , $BMP ['colors'] * 4 )); } // 4 : Cr?ation de l'image $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']; } // Fermeture du fichier fclose ( $f1 ); return $res ; }