PHP-php bmp格式的图片生成缩略图

发布于 2016-10-30 23:49:36 字数 940 浏览 1390 评论 4

bmp格式的图片怎么生成缩略图?

//生成缩略图函数

function img_create_small($big_img,$width,$height,$phname_type){//大图文件地址,缩略宽,缩略高,小图地址
$imgage=getimagesize($big_img);//获取大图信息
switch ($imgage[2]){//判断图像类型
case 1:
$im=imagecreatefromgif($big_img);
break;
case 2:
$im=imagecreatefromjpeg($big_img);
break;
case 3:
$im=imagecreatefrompng($big_img);
break;
}
//echo '<pre>';print_r($im);die;
$src_W=imagesx($im);//获取大图宽
$src_H=imagesy($im);//获取大图高
$sizexy = $this->getScaleImage($big_img,$width,$height);

$width = $sizexy[0];
$height = $sizexy[1];

$tn=imagecreatetruecolor($width,$height);//创建小图
imagecopyresized($tn,$im,0,0,0,0,$width,$height,$src_W,$src_H);//复制图像并改变大小
//$type = strrchr($phname,".");
$arr = explode($phname_type,$big_img);
$small_img = $arr[0].'_small'.$phname_type;
imagejpeg($tn,$small_img);//输出图像

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晚风撩人 2017-07-06 10:02:09

参考php手册 imagecreatefrombmp 函数的实现,

 function imagecreatefrombmp($p_sFile)
{
// Load the image into a string
$file = fopen($p_sFile,"rb");
$read = fread($file,10);
while(!feof($file)&&($read<>""))
$read .= fread($file,1024);

$temp = unpack("H*",$read);
$hex = $temp[1];
$header = substr($hex,0,108);

// Process the header
// Structure: http://www.fastgraph.com/help/bmp_header_format.html
if (substr($header,0,4)=="424d")
{
// Cut it in parts of 2 bytes
$header_parts = str_split($header,2);

// Get the width 4 bytes
$width = hexdec($header_parts[19].$header_parts[18]);

// Get the height 4 bytes
$height = hexdec($header_parts[23].$header_parts[22]);

// Unset the header params
unset($header_parts);
}

// Define starting X and Y
$x = 0;
$y = 1;

// Create newimage
$image = imagecreatetruecolor($width,$height);

// Grab the body from the image
$body = substr($hex,108);

// Calculate if padding at the end-line is needed
// Divided by two to keep overview.
// 1 byte = 2 HEX-chars
$body_size = (strlen($body)/2);
$header_size = ($width*$height);

// Use end-line padding? Only when needed
$usePadding = ($body_size>($header_size*3)+4);

// Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
// Calculate the next DWORD-position in the body
for ($i=0;$i<$body_size;$i+=3)
{
// Calculate line-ending and padding
if ($x>=$width)
{
// If padding needed, ignore image-padding
// Shift i to the ending of the current 32-bit-block
if ($usePadding)
$i += $width%4;

// Reset horizontal position
$x = 0;

// Raise the height-position (bottom-up)
$y++;

// Reached the image-height? Break the for-loop
if ($y>$height)
break;
}

// Calculation of the RGB-pixel (defined as BGR in image-data)
// Define $i_pos as absolute position in the body
$i_pos = $i*2;
$r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
$g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
$b = hexdec($body[$i_pos].$body[$i_pos+1]);

// Calculate and draw the pixel
$color = imagecolorallocate($image,$r,$g,$b);
imagesetpixel($image,$x,$height-$y,$color);

// Raise the horizontal position
$x++;
}

// Unset the body / free the memory
unset($body);

// Return image-object
return $image;
}

瑾兮 2017-02-24 01:51:17

查了很多资料,网上的imagecreatefrombmp函数和手册评论里的imagecreatefrombmp函数都不靠谱,而且效率是极低的,它的实现过程也是解析上传的BMP图片,然后再新绘制到一张新的图片,所以建议不要要BMP图片,如果一定要从BMP生成缩略图,建议在系统上装一个bmp2png,然后把网页上传过来的bmp图片通过shell_exec()调用命令,通过bmp2png转换成png图片,然后再用转换后的PNG图片生成缩略图。

泛泛之交 2017-02-23 15:18:54

 <?php
function ConvertBMP2GD($src, $dest = false) {
if(!($src_f = fopen($src, "rb"))) {
return false;
}
if(!($dest_f = fopen($dest, "wb"))) {
return false;
}
$header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f, 14));
$info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread($src_f, 40));
extract($info);
extract($header);
if($type != 0x4D42) { // signature "BM"
return false;
}
$palette_size = $offset - 54;
$ncolor = $palette_size / 4;
$gd_header = "";
// true-color vs. palette
$gd_header .= ($palette_size == 0) ? "xFFxFE" : "xFFxFF";
$gd_header .= pack("n2", $width, $height);
$gd_header .= ($palette_size == 0) ? "x01" : "x00";
if($palette_size) {
$gd_header .= pack("n", $ncolor);
}
// no transparency
$gd_header .= "xFFxFFxFFxFF";
fwrite($dest_f, $gd_header);
if($palette_size) {
$palette = fread($src_f, $palette_size);
$gd_palette = "";
$j = 0;
while($j < $palette_size) {
$b = $palette{$j++};
$g = $palette{$j++};
$r = $palette{$j++};
$a = $palette{$j++};
$gd_palette .= "$r$g$b$a";
}
$gd_palette .= str_repeat("x00x00x00x00", 256 - $ncolor);
fwrite($dest_f, $gd_palette);
}
$scan_line_size = (($bits * $width) + 7) >> 3;
$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0;
for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
// BMP stores scan lines starting from bottom
fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
$scan_line = fread($src_f, $scan_line_size);
if($bits == 24) {
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size) {
$b = $scan_line{$j++};
$g = $scan_line{$j++};
$r = $scan_line{$j++};
$gd_scan_line .= "x00$r$g$b";
}
}
else if($bits == 8) {
$gd_scan_line = $scan_line;
}
else if($bits == 4) {
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = chr($byte >> 4);
$p2 = chr($byte & 0x0F);
$gd_scan_line .= "$p1$p2";
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
else if($bits == 1) {
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = chr((int) (($byte & 0x80) != 0));
$p2 = chr((int) (($byte & 0x40) != 0));
$p3 = chr((int) (($byte & 0x20) != 0));
$p4 = chr((int) (($byte & 0x10) != 0));
$p5 = chr((int) (($byte & 0x08) != 0));
$p6 = chr((int) (($byte & 0x04) != 0));
$p7 = chr((int) (($byte & 0x02) != 0));
$p8 = chr((int) (($byte & 0x01) != 0));
$gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
fwrite($dest_f, $gd_scan_line);
}
fclose($src_f);
fclose($dest_f);
return true;
}
function imagecreatefrombmp($filename) {
$tmp_name = tempnam("tmp", "GD");
if(ConvertBMP2GD($filename, $tmp_name)) {
$img = imagecreatefromgd($tmp_name);
unlink($tmp_name);
return $img;
}
return false;
}
function mylog($msg)
{
$fp = fopen("mylog.txt", "a+");
echo $msg."n";
fwrite($fp, $msg."n");
fclose($fp);
}
?>

 $bmptoimg = imagecreatefrombmp($filename);
if($bmptoimg)
{
imagepng($bmptoimg, $thum_filename); //原图文是转成jpeg
}

清晨说ぺ晚安 2016-11-15 14:17:51

<?
class resizeimage
{
//图片类型
var $type;
//实际宽度
var $width;
//实际高度
var $height;
//改变后的宽度
var $resize_width;
//改变后的高度
var $resize_height;
//是否裁图
var $cut;
//源图象
var $srcimg;
//目标图象地址
var $dstimg;
//临时建的图象
var $im;
//生成的文件名后缀
var $extstr;

function resizeimage($img, $wid, $hei,$extstr,$c=0)
{
$this->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
$this->extstr = $extstr;
//图片的类型
$this->type = substr(strrchr($this->srcimg,"."),1);
//初始化图象
$this->initi_img();
//目标图象地址
$this -> dst_img();
$this->width = @imagesx($this->im);
$this->height = @imagesy($this->im);
//生成图象
$this->newimg();
@ImageDestroy ($this->im);
}
function newimg()
{
//改变后的图象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);
//实际图象的比例
if($this->height>0)
$ratio = ($this->width)/($this->height);
if(($this->cut)=="1")
//裁图
{
if($ratio>=$resize_ratio)
//高度优先
{
$newimg = @imagecreatetruecolor($this->resize_width,$this->resize_height);
@imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
@ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
//宽度优先
{
$newimg = @imagecreatetruecolor($this->resize_width,$this->resize_height);
@imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
@ImageJpeg ($newimg,$this->dstimg);
}
}
else
//不裁图
{
if($ratio>=$resize_ratio)
{
$newimg = @imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
@imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
{
$newimg = @imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
@imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
@ImageJpeg ($newimg,$this->dstimg);
}
}
}
//初始化图象
function initi_img()
{
$type=strtolower($this->type);//转换成小写,否则不写扩展名生成不了。

if($type=="jpg" || $type=="jpeg" || $type=="jpe")
{
$this->im = @imagecreatefromjpeg($this->srcimg);
}
if($type=="gif")
{
$this->im = @imagecreatefromgif($this->srcimg);
}
if($type=="png")
{
$this->im = @imagecreatefrompng($this->srcimg);
}
if($type=="bmp")
{
$this->im = $this->imagecreatefrombmp($this->srcimg);
}
}

function imagecreatefrombmp($p_sFile){
$file = fopen($p_sFile,"rb");
$read = fread($file,10);
while(!feof($file)&&($read<>""))
$read .= fread($file,1024);
$temp = unpack("H*",$read);
$hex = $temp[1];
$header = substr($hex,0,108);
if (substr($header,0,4)=="424d"){
$header_parts = str_split($header,2);
$width = hexdec($header_parts[19].$header_parts[18]);
$height = hexdec($header_parts[23].$header_parts[22]);
unset($header_parts);
}
$x = 0;
$y = 1;
$image = imagecreatetruecolor($width,$height);
$body = substr($hex,108);
$body_size = (strlen($body)/2);
$header_size = ($width*$height);
$usePadding = ($body_size>($header_size*3)+4);
for ($i=0;$i<$body_size;$i+=3){
if ($x>=$width){
if ($usePadding)
$i += $width%4;
$x = 0;
$y++;
if ($y>$height)
break;
}
$i_pos = $i*2;
$r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
$g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
$b = hexdec($body[$i_pos].$body[$i_pos+1]);
$color = imagecolorallocate($image,$r,$g,$b);
imagesetpixel($image,$x,$height-$y,$color);
$x++;
}
unset($body);
return $image;
}

//图象目标地址
function dst_img()
{
$full_length = strlen($this->srcimg);
$type_length = strlen($this->type);
$name_length = $full_length-$type_length;
$name = substr($this->srcimg,0,$name_length-1);
$this->dstimg = $name.$this->extstr.'.'.$this->type;
}

static function get_url($img,$extstr){
$imgs = explode('.',$img);
$ext = end($imgs);
$full_length = strlen($img);
$type_length = strlen($ext);
$name_length = $full_length-$type_length;
$name = substr($img,0,$name_length-1);
return $name.$extstr.'.'.$ext;
}
}
?>

来自:http://zhujllove.blog.163.com/blog/static/113901825201122113930917/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文