首页 > 编程技术 > php

PHP批量生成图片缩略图(1/5)

发布时间:2016-11-25 16:28

这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。

这款教程会用到php文件
view.php
config.php
funs.php
index.php


功能:
-----------------------
1.为文件夹里的图片生成指定大小缩略图
2.列出子文件夹, 可以指定生生某文件夹生成
3.为方便不同大小的缩图生成,图片保存到新的目录,而目录结构不变

实现:
-----------------------
1.图片原文件夹   $image
2.处理后保存在   $newimage
 保持目录结构和文件名不变(方便调用)
3.如果在新的文件夹里图片已经存在,直接跳过

操作界面:
-----------------------
1.$image 输入框
2.$newimage 输入框(若没填写,刚为当前目录平级的以缩略大小为文件名的目录)
3.按扭"搜索",列出所有文件夹,生成缓存,写到文件.
4.生成.     生成子目录

*/
?>
//index.php代码

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=gb2312">
  <title>生成缩略图</title>
</head>
 <body>
<?php
/* +-------------------------------------------------------------+
 * | copyright (c) 2008-2009 yese.in all rights reserved.       
 * +-------------------------------------------------------------+
 * | author: wbsifan <wbsifan@163.com>   <qq:29500196>          
 * +-------------------------------------------------------------+ 
 * | filename : index.php v0.1
 * | update   : 2008-07-01
 * | content  : 生成缩略图 
 * +-------------------------------------------------------------+
 */

现在用php采集远程图片是很简单的事情,但是我们要把采集过来的数据进行过滤,这恐怕得用到正则表达式了,下面的程序是采集远程图片图片并且替换图片地址代码。

$dir_date=date('y/m/d');//日期文件夹
$dir_name="images/$dir_date";//文件夹路径
function mkdirm($path) //取文件夹名
{
if (!file_exists($path)) //判断是否存在
{
mkdirm(dirname($path)); //返回路径中的目录部分
mkdir($path, 0777); //创建文件夹和权限
}
}
mkdirm($dir_name);//创建文件夹
//$files = file_get_contents('http://www.ywsp.com');
$files ='<img" width=100% src="http://img.111cn.net/img/bid_v2/bid_v2_content/p_bid.gif" alt="普通任务" /><img width="272px" height="60px" style="padding-top: 10px;"" width=100% src="http://img.111cn.net/img/index_v3/20100611001.gif">';
// 图片地址转换一下   $p=preg_replace('//image//', 'http://qq.ip138.com/image/', $pg[1]);
 $reg = "/<img[^>]*src="(http://(.+)/(.+).(jpg|gif|bmp|bnp))"/isu";
$img=preg_match_all($reg,$files,$imgs);

for ($i=0;$i<count($imgs[0]);$i++)
{

//echo "".$imgs[1][$i]." <br> ";
//保存图片
$opts = array(
  'http'=>array(
    'header'=>"referer: http://www.111cn.net/ "
  )
);
$context = stream_context_create($opts);
header( 'content-type: image/jpeg' );
$fileresource = @file_get_contents( $imgs[1][$i],false,$context) ;
$image_old.=""".$imgs[1][$i].""".","; //先把所有网址变为一个数如1,2,3,4
$image_new.=""".$dir_name."/".mktime().$i.".jpg".""".","; //先把所有新的图片地址变为一个数如1,2,3,4
file_put_contents($dir_name."/".mktime().$i.".jpg", $fileresource);
//保存图片
}
$image_old=substr($image_old,0,(strlen($image_old)-1));//把网址最后个逗号去掉
$image_new=substr($image_new,0,(strlen($image_new)-1));//把新网址最后个逗号去掉
//print_r($p);

$image_old=explode(",",$image_old);//分割多个
$image_new=explode(",",$image_new);//分割多个

$files=str_replace($image_old,$image_new,$files); //把网址跟新的图片网址调用进去替换
echo $files;

 代码如下 复制代码
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
  2  <html>
  3  <head>
  4     <title>投票结果</title>
  5     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6
  7  <script type="text/网页特效">
  8
  9     function getxmlhttpobject()
 10 {
 11   var xmlhttp=null;
 12   try
 13     {
 14     // firefox, opera 8.0+, safari
 15      xmlhttp=new xmlhttprequest();
 16     }
 17   catch (e)
 18     {
 19     // internet explorer
 20     try
 21       {
 22       xmlhttp=new activexobject("msxml2.xmlhttp");
 23       }
 24     catch (e)
 25       {
 26       xmlhttp=new activexobject("microsoft.xmlhttp");
 27       }
 28     }
 29   return xmlhttp;
 30 }
 31 function checkajax(){
 32    xmlhttp=getxmlhttpobject()
 33  
 34   if (xmlhttp==null)
 35     {
 36     alert ("您的浏览器不支持ajax!");
 37     return ;
 38     }else
 39     {
 40        return xmlhttp;
 41     }
 42 }
 43 function preshow(){
 44     ajaxobj = checkajax();
 45     var url = "page.php?page=1";
 46 ajaxobj.onreadystatechange=statechanged;
 47 ajaxobj.open("get",url,true);
 48 ajaxobj.send(null);
 49 }
 50 function showhint1(str)
 51 {
 52     ajaxobj = checkajax();
 53     document.getelementbyid("msg").innerhtml="正在读取数据……";
 54
 55 var url = str;
 56 ajaxobj.onreadystatechange=statechanged;
 57 ajaxobj.open("get",url,true);
 58 ajaxobj.send(null);
 59
 60 }

 

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net1999/xhtml">

<head>
<meta content="text/html; charset=gb2312" http-equiv="content-type" />
<title>多个文件上传(php+网页特效可动态增加文件上传框)</title>
<script language="网页特效" type="text/网页特效">
function addinput()//增加input节点
{
var input=document.createelement('input');//创建一个input节点
var br=document.createelement('br');//创建一个br节点
input.setattribute('type','file');// 设置input节点type属性为file
input.setattribute('name','pic[]');//设置input节点 name属性为files[],以 数组的方式传递给服务器端
document.form1.appendchild(br);//把节点添加到 form1表单中
document.form1.appendchild(input);
}
</script>
</head>
<?php
if($_post['sub'])
{
$ftype=array('image/jpg','image /jpeg','imgage/png','image/pjpeg','image/gif');//允许上传的文件类型
$files=$_files['files'];
$fnum=count($files['name']); //取得上传文件个数
for($i=0;$i<$fnum;$i++)
{
   if($files['name'][$i]!=''&&is_uploaded_file($files['tmp_name'][$i]))
   {
    if(in_array($files['type'][$i],$ftype))//判断文件是否是允许的类型
    {
     $fname='upfile/'.rand(0,10000).time().substr($files['name'] [$i],strrpos($files['name'][$i],'.'));//自动命名
     move_uploaded_file($files['tmp_name'][$i],$fname);
     echo '<br/>文件上传成功!';
    }
    else
    {
     echo '<br/>不允许的文件类型!';
    }
   }
   else
   {
    echo '<br/>该文件不存在!';
   }
}

}
?>
<body>
<form name="form1" method="post" action="" enctype="multipart/form-data" >
    <input type="file" name="pic[]" />
<input type="submit" name="sub" value="上传"/>
</form>
<a href="#" onclick="addinput()">再上传一张</a>
</body>

</html>

直接用php创建word文档代码(系统无需安装word软件) 使用方法: 首先用$word->start()表示要生成word文件了。 然后你可以输出任何的HTML代码,不论是从文件读过来再写到这里, 还是直接在这里输出HTML,都没有关系。

等你输出完毕后,用$word->save($path)方法,其中$path是你想 生成的word文件的名称(可以给出完整的路径).当你使用了$word->save() 方法后,这后面的任何输出都和word文件没有关系了,也就是说word的生成 工作就完成了。之后就和你平常使用php的方式一样拉。随便你输出什么东西, 都直接在浏览器里输出,而不会写到word里面去。

*/

 代码如下 复制代码

class word

function start()
{
ob_start();
print'<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/tr/rec-html40">';

}

function save($path)
{

print "</html>";
$data = ob_get_contents();

ob_end_clean();

$this->wirtefile ($path,$data);
}

function wirtefile ($fn,$data)
{

$fp=fopen($fn,"wb");
fwrite($fp,$data);
fclose($fp);
}

}
//生成word调用方法
include("word.php");
$word=new word;
$word->start();

标签:[!--infotagslink--]

您可能感兴趣的文章: