您的位置:首页 > 编程语言 > PHP开发

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

2013-10-11 01:12 267 查看
由于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;
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: