php读取zip文件中的图片,并动态更换div的背景图片

由于zip包中的图片为bmp格式,php的gd库没有相应的函数,网上找了N多的php类,奈何都是扯淡。

于是另行一套方法,现表露如下,思路:

1、确定zip包中的图片文件名。

2、zip_entry_read 读出文件内容后用文件操作函数写到临时文件,这里图片文件后缀要一致。

3、看情况命名临时图片文件名,因为前台可能会有缓存,不能实时切换。最好随机生成。

我是用的ajax来实现这一过程的。

前台发送ajax:

<span style="white-space:pre">	</span>$.ajax({
		 type: "GET",
		 url: "http://XXX/readImgFromZip.class.php",
		 data: "w="+$(this).attr('id'),
		 success: function(msg){
		 $("#Pdiv").css("background",'url(http://XXX/'+msg+')');
		 }

	}); //end ajax

后台接收处理:

<?php
	
	$img_str=isset($_GET['w'])?$_GET['w']:'first';
		
	/* filename */
	
	switch($img_str)
	{
		case 'first':
			$img_file='init.bmp';
			$temp_img='temp1.bmp';
		break;
		case 'second':
			$img_file='post_offline.bmp';
			$temp_img='temp2.bmp';
		break;
		case 'three':
			$img_file='error_printing.bmp';
			$temp_img='temp3.bmp';
		break;
	}
	
	/* zip file */
	$zip_file=str_replace('\\','/',$_SERVER['DOCUMENT_ROOT'].'/img/ui/aiq.zip');
	
	$zip=zip_open($zip_file);
	
	if ($zip) {
		while ($zip_entry = zip_read($zip)) {

			$file_name=zip_entry_name($zip_entry);
			
			if($file_name==$img_file)
			
				if (zip_entry_open($zip, $zip_entry, "r")) {
				
					/* read binary stream */
					$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));					
					
					/* create temporary bmpImg */
					if($fp = fopen($temp_img,'w'))
					{  
						if(fwrite($fp,$buf))
						{  
							 fclose($fp);      
						}  
					}  

					zip_entry_close($zip_entry);
				}
		}

	zip_close($zip);

	}
	echo $temp_img;
?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值