您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 遂宁分类信息网,免费分类信息发布

有没针对 fopen 打开的图片文件获取信息的函数解决方案

2025/3/26 7:28:25发布9次查看
有没针对 fopen 打开的图片文件获取信息的函数
getimagesize() 的传参是图片文件路劲
如果我只知道 tmpfile() 或 fopen() 返回的文件句柄怎么办呢
难道非要临时保存一下到硬盘上?
------解决方案--------------------
最好保存一下,如果不使用gd函数的话,那就复杂了,要自己解析图片二进制数据来得到他的原数据了。
比如这个解析png图片的。
/************************* png ****************************/
function _parsepng($file) 

//extract info from a png file 
$f=fopen($file,'rb'); 
if(!$f) 
$this->error('can\'t open image file: '.$file); 
//check signature 
if(fread($f,8)!=chr(137).'png'.chr(13).chr(10).chr(26).chr(10)) 
$this->error('not a png file: '.$file); 
//read header chunk 
fread($f,4); 
if(fread($f,4)!='ihdr') 
$this->error('incorrect png file: '.$file); 
$w=$this->_freadint($f); 
$h=$this->_freadint($f); 
$bpc=ord(fread($f,1)); 
if($bpc>8) 
$this->error('16-bit depth not supported: '.$file); 
$ct=ord(fread($f,1)); 
if($ct==0) 
$colspace='devicegray'; 
elseif($ct==2) 
$colspace='devicergb'; 
elseif($ct==3) 
$colspace='indexed'; 
else 
$this->error('alpha channel not supported: '.$file); 
if(ord(fread($f,1))!=0) 
$this->error('unknown compression method: '.$file); 
if(ord(fread($f,1))!=0) 
$this->error('unknown filter method: '.$file); 
if(ord(fread($f,1))!=0) 
$this->error('interlacing not supported: '.$file); 
fread($f,4); 
$parms='/decodeparms >'; 
//scan chunks looking for palette, transparency and image data 
$pal=''; 
$trns=''; 
$data=''; 
do 

$n=$this->_freadint($f); 
$type=fread($f,4); 
if($type=='plte') 

//read palette 
$pal=fread($f,$n); 
fread($f,4); 

elseif($type=='trns') 

//read transparency info 
$t=fread($f,$n); 
if($ct==0) 
$trns=array(ord(substr($t,1,1))); 
elseif($ct==2) 
$trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1))); 
else 

$pos=strpos($t,chr(0)); 
if($pos!==false) 
$trns=array($pos); 

fread($f,4); 

elseif($type=='idat') 

//read image data block 
$data.=fread($f,$n); 
fread($f,4); 

elseif($type=='iend') 
break; 
else 
fread($f,$n+4); 

while($n); 
if($colspace=='indexed' && empty($pal)) 
$this->error('missing palette in '.$file); 
fclose($f); 
return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'flatedecode','parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data); 
}
遂宁分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录