PHP实用小例子:批量压缩图片
进度显示页代码<?php
ini_set("memory_limit","120M");
include("functions.php");
$dir="image/";
if ($dh = @opendir($dir)) {
set_time_limit(0);
$width = 800; //显示的进度条长度,单位 px
$total = count(scandir($dir)); //总共需要操作的记录数
$pix = $width / $total; //每条记录的操作所占的进度条单位长度
$progress = 0;
//echo $totel; //当前进度条长度
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>降低品质</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style>
body, div input { font-family: Tahoma; font-size: 14px; }
</style>
<script language="JavaScript">
<!--
function updateProgress(sMsg, iWidth)
{
document.getElementById("status").innerHTML = sMsg;
document.getElementById("progress").style.width = (iWidth-8) + "px";
document.getElementById("percent").innerHTML = parseInt(iWidth / <?php echo $width; ?> * 100) + "%";
}
//-->
</script>
</head>
<body>
<div style="padding: 8px; border:none;left:expression((body.offsetWidth-this.offsetWidth)/2);position:absolute; width: <?php echo $width+8; ?>px" >
<div><font color="gray">→</font></div>
<div style="padding:0; margin:3px; background-color: white; border: 1px solid #d6d6d6; width: <?php echo $width; ?>px">
<div id="progress" style="padding:1px; margin:3px; background: url(bg.gif) center left repeat-x;border:0;width:0px;text-align:center;height:16px"></div>
</div>
<div id="status"> </div>
<div id="percent" style="position: relative; top: -44px; text-align: center; font-weight: bold; font-size:12px; color:#0099ff">0%</div>
</div>
<?php
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != "..") {
if(@filetype($dir."/".$file)=="file"){
$ff=$dir."/".$file;
quanty($ff);
}
flush(); //将输出发送给客户端浏览器
//foreach ($ff as $user) {
// 在此处使用空循环模拟较为耗时的操作,实际应用中需将其替换;
// 如果你的操作不耗时,我想你就没必要使用这个脚本了 :)
?>
<script language="JavaScript">
updateProgress("… <?php echo $file; ?> ", <?php echo min($width, intval($progress)); ?>);
</script>
<?php
//flush(); //将输出发送给客户端浏览器,使其可以立即执行服务器端输出的 JavaScript 程序。
$progress += $pix;
}
}
//}//end foreach
// 最后将进度条设置成最大值 $width,同时显示操作完成
?>
<script language="JavaScript">
updateProgress("over! ", <?php echo $width; ?>);
//window.location.href='index.php';
</script>
</body>
</html>
<?php
}
?>
压缩图片函数
function quanty($filename){
if(is_file($filename)) {
$size = getimagesize($filename);
$fileInfo = pathinfo($filename);
$ext = strtolower($fileInfo["extension"]);
$dst_filename = $fileInfo["dirname"]."/".$fileInfo["basename"];
$max_width = $size[0];
$max_height = $size[1];
if($ext == 'jpg' || $ext == 'jpeg'||$ext == 'png'||$ext == 'gif'){
if ($ext == 'jpg' || $ext == 'jpeg') {
$src_img = imagecreatefromjpeg($filename) or die( 'Cannot load input JPEG image' );
} else if ($ext == 'png') {
$src_img = imagecreatefrompng($filename) or die( 'Cannot load input PNG image' );
} else if ($ext == 'gif') {
$src_img = imagecreatefromgif($filename) or die( 'Cannot load input GIF image' );
}
$dst_img = imagecreatetruecolor($max_width, $max_height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $max_width, $max_height, $size[0], $size[1]);
imagejpeg($dst_img, $dst_filename, 85);
imagedestroy($src_img);
imagedestroy($dst_img);
}
}
} 用 java script 是否有相同效果 ? 来这个区 我就是文盲了!!! 很實用的語法
頁:
[1]