php bmp中创建图像bmp2gd,让GD支持32位BMP
php GD库可方便的从URL新建一图像, GD中有imagecreatefromjpeg(),imagecreatefromPNG()....等之类的FUNCTION 可有时从URL中读取的切BMP图像而 可恨的是 GD2中切偏偏没有.
<?php
function bmp2gd($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) return false;
$palette_size = $offset-54;
$ncolor = $palette_size/4;
$gd_header = "";
$gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
$gd_header .= pack("n2", $width, $height);
$gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
if($palette_size) $gd_header .= pack("n", $ncolor);
$gd_header .= "\xFF\xFF\xFF\xFF";
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("\x00\x00\x00\x00", 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--) {
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", "BMP");
if(bmp2gd($filename, $tmp_name)) {
$img = imagecreatefromgd($tmp_name);
unlink($tmp_name);
return $img;
}
return false;
}
?>
php bmp中创建图像bmp2gd,让GD支持32位BMP的更多相关文章
- Shell脚本中,如何判断Linux系统是32位还是64位?
一行就能搞定,输出32或者64 可以用“和. 参考代码如下: ldconfig if [ $(getconf WORD_BIT) = '32' ] && [ $(getconf LON ...
- PNG怎么转换成32位的BMP保持透明
32位BMP位图的格式是XRGB,就是X8位 R8位 G8位 B8位,当中的X8可以作为Alpha值用于透明, 只需要搜索一下PNG转32位BMP位图的软件就可以了,另外用PhotoShop下载插件打 ...
- 在64位的ubuntu 14.04 上开展32位Qt 程序开发环境配置(pro文件中增加 QMAKE_CXXFLAGS += -m32 命令)
为了能中一个系统上开发64或32位C++程序,费了些周折,现在终于能够开始干过了.在此记录此时针对Q5.4版本的32位开发环境配置过程. 1. 下载Qt 5.4 的32位版本,进行安装,安装过程中会发 ...
- 发现Xilinx Virtex 5 FPGA中单个DSP乘法器只支持17位无符号乘法
发现Xilinx Virtex 5 FPGA中,单个DSP乘法器只支持17位无符号乘法.如果令18位乘数相乘,结果会与正确的乘积不同.
- gimp怎么移动选取中的图像并创建图层
gimp怎么移动选取中的图像并创建图层 https://jingyan.baidu.com/article/414eccf6bf4d6e6b431f0a3b.html 听语音 原创 | 浏览:1148 ...
- e668. 在一组像素中创建缓冲图像
This example demonstrates how to convert a byte array of pixel values that are indices to a color ta ...
- (转)原始图像数据和PDF中的图像数据
比较原始图像数据和PDF中的图像数据,结果见表1.1.表1.1中各种“解码器”的解释见本文后续的“PDF支持的图像格式”部分,“PDF中的图像数据”各栏中的数据来自开源的PdfView.如果您有兴趣查 ...
- 【转载】VC++中的图像类型转换--使用开源CxImage类库
一.CxImage类库简介 这只是翻译了CxImage开源项目主页上的部分简介及简单使用. CxImage类库是一个优秀的图像操作类库.它可以快捷地存取.显示.转换各种图像.有的读者可能说,有那么多优 ...
- php创建图像具体步骤
php 的图像处理在验证码是最常见的,下面说下使用php创建图像的具体步骤. 简要说明:PHP 并不仅限于创建 HTML 输出, 它也可以创建和处理包括 GIF, PNG(推荐), JPEG, WBM ...
随机推荐
- LYDSY模拟赛day1 Tourist Attractions
/* 假设路径是 a − b − c − d,考虑枚举中间这条边 b − c,计 算有多少可行的 a 和 d. 设 degx 表示点 x 的度数,那么边 b − c 对答案的贡献为 (degb − 1 ...
- php生成随机字符串
<?php echo make_random_str(); function make_random_str() { $arr = ["A","B",&q ...
- BestCoder Round #90
有生以来第一场在COGS以外的地方打的比赛.挂成dog了. 主要是没有经验,加之代码能力过弱.还有最后的瞎hack三次,Too Young Too Simple...... 言归正传. (抄一发题解先 ...
- Python函数讲解
Python函数
- python匹配ip正则
python匹配ip正则 #!/usr/bin/env python # -*- coding:utf-8 -*- import re ip_str = "asdad1.1.1.1sdfwe ...
- jstl core 库 之 out set remove
jstl 核心库 out标签 out:输出的标签 * value :输出的值 * default :默认值 * escapeXml :是否转移 默认为true(转义) 代码: <!-- 输出常量 ...
- JVM内存管理------垃圾搜集器简介
引言 上一章我们已经探讨过GC的各个算法,那么垃圾搜集器是什么呢? 通俗的讲,使用编程语言将算法实现出来,产生的程序就是垃圾搜集器了.既然谈到了编程语言的实现,那么在讨论垃圾搜集器的时候,就已经涉及到 ...
- linux 服务
CentOS 7.x设置自定义开机启动,添加自定义系统服务 http://linux.it.net.cn/CentOS/fast/2015/0507/15184.html
- 数组排序sort()
数组排序sort() sort()方法使数组中的元素按照一定的顺序排列. 语法: arrayObject.sort(方法函数) 参数说明: 1.如果不指定<方法函数>,则按unicode码 ...
- HTML行为元素和块级元素及语义化
块级元素 div - dl - form 交互表单h1 - h6 标题 hr 水平分割线p 段落ul 非排序列表table 表格 行内元素 a 链接br 换行em 强调i 斜体img 图片input ...