一、getimgarrfrommysql<?phpecho ' select mysql get picurl to array start<br>';$link = mysql_connect('localhost','root',''); //('hostname','dbuser','dbpassword')if (!$link) {die('Could not connect to MySQL: ' . mysql_error());}echo 'Connection OK<br>';mysql_select_db("test", $link);$result = mysql_query("SELECT * FROM to8to_goodcasepic");$caseArr = array();$i = 0;while($row = mysql_fetch_array($result)){echo $i . " --> " . $row['picpath'];echo "<br />";$caseArr[$i] = $row['picpath'];$i ++;}echo '<br> close mysql <br>';mysql_close($link);echo '<br> mysql the end <br>';echo '<br> writeFile start <br>';$imgDataArr = '<?php$picdata =';$imgDataArr .= var_export($caseArr,true).'; ';//###测试临时注释//file_put_contents('ab3.php',$textdata2);$fileName = "./caseimgarrdata.php";writeFile($fileName,$imgDataArr,'a');echo '<br> writeFile the end <br>';echo '<br> all the end <br>';/**** 写文件* @param string $file 文件路径* @param string $str 写入内容* @param char $mode 写入模式*/function writeFile($file,$str,$mode='w'){$oldmask = @umask(0);$fp = @fopen($file,$mode);@flock($fp, 3);if(!$fp){Return false;} else {@fwrite($fp,$str);@fclose($fp);@umask($oldmask);Return true;}}///the end ///二、getimgarrfromdb<?php/*** 从数据库批量获取图库(效果图)地址,按规则生成webp 图片** @author bfire.lai* @version 1.0* @copyright bfire.lai, 2015-01-16* @package default*///实时刷新浏览器缓存ob_end_clean();ob_implicit_flush(true);//网站根目录if (!defined('ROOT')) {define('ROOT', dirname(dirname(__FILE__)));}//定义图片路径define('PIC_DOMAIN','http://pic.to8to.com/');//导入基本配置文件require_once("../include/start_init.inc.php");$debug2 = (int)$_GET['_debug'];if ($debug2) {ini_set('display_errors',true);error_reporting(E_ALL);$db->SetPrintSql(true);}//设置响应时间不限制@set_time_limit(0);//设置最大缓存块是512M@ini_set('memory_limit','512M');echo '<br> all start <br>'.date("Y-m-d H:i:s");if(1){//效果图 获取数据写入文件数组$tables = 'xiaoguotu';//数遍表$tablesArr = array(0 => 'xiaoguotu', //效果图(图库) filename1 => 'owner', //业主头像 id2 => 'fcom', //装修公司头像 id3 => 'art', //装修公司设计方案 filename4 => 'company_zhengshu', //装修公司证书 filename5 => 'gongdi_pic', //远程工地 filename6 => 'xiaoguotu_topic', //专题 img_for_mobile/img1/img2/img3/img4/img5//7 => 'knowzxcs', //知识 content//8 => 'company_zhengshu', //装修公司证书 filename//9 => 'c');foreach ($tablesArr as $kk => $vv) {//switch($tables){$tables = $vv;switch($tables){case 'xiaoguotu':$filename = 'filename as filename';$where = 'visible=1';$orderby = 'edittime desc';$in_rule = 'smallcase';imgDbToImgWebp($tables, $filename, $where, $orderby, $in_rule);break;case 'owner':$filename = 'id as filename';$in_rule = 'user';imgDbToImgWebp($tables, $filename, '', '', $in_rule);break;case 'fcom':$filename = 'id as filename';$in_rule = 'user';imgDbToImgWebp($tables, $filename, '', '', $in_rule);break;case 'art':$filename = 'filename as filename';$orderby = 'edittime desc';$in_rule = 'case';imgDbToImgWebp($tables, $filename, '', $orderby, $in_rule);break;case 'company_zhengshu':$filename = 'filename as filename';$orderby = 'puttime desc';$in_rule = 'c_zhengshu';imgDbToImgWebp($tables, $filename, '', $orderby, $in_rule);break;case 'gongdi_pic':$filename = 'filename as filename';$orderby = 'add_time desc';$in_rule = 'gongdi_pic';imgDbToImgWebp($tables, $filename, '', $orderby, $in_rule);break;case 'xiaoguotu_topic':$filename = 'img_for_mobile as filename';$orderby = 'puttime desc';$in_rule = 'xiaoguotu_topic';imgDbToImgWebp($tables, $filename, '', $orderby, $in_rule);break;/*case 'xiaoguotu7':break;case 'xiaoguotu8':break;case 'xiaoguotu9':break;case 'xiaoguotu10':break;case 'xiaoguotu11':break;case 'xiaoguotu12':break;*/default :echo '脚本执行完毕...';break;}}}echo '<br> all end time <br>'.date("Y-m-d H:i:s");/*** @功能:先查数据库 转换成数组 再 转换成 WEBP 格式* @参数:$tables => 数据表, $filename => 要查的字段, $where=> 条件, $orderby => 排序* @返回:若转换成功返回true,否则返回false或直接跳出*/function imgDbToImgWebp($tables, $filename='', $where='', $orderby='',$in_rule=''){global $db;if($tables == ''){return false;}$page = 1;if(!@$perpage) $perpage=3;while(true){$limit=($page-1)*$perpage;$limit="$limit,$perpage";echo '<br> select xiaoguotu start time : <br>'.date("Y-m-d H:i:s");//$tables = 'xiaoguotu';$result=$db->GetRow($tables,$filename,$where,'',$orderby,$limit);echo '<br> select xiaoguotu end time : <br>'.date("Y-m-d H:i:s");echo '<br> foreach result start time : <br>'.date("Y-m-d H:i:s");echo '<pre>';//print_r($result);exit;if($result){//可以操作条件//echo '<pre>';//临时图片$imgDataArr = array();foreach ($result as $k => $v) {//echo $k . " --> " . $v['filename'];//echo "<br />";switch($in_rule){case 'case':$imgDataArr[$k] = $in_rule.'/'.$v['filename'];break;case 'smallcase':$imgDataArr[$k] = $in_rule.'/'.$v['filename'];break;case 'user'://$file='/pic/user/'.($uid%100)."/headphoto_$uid$FLAG.jpg";$uid = intval($v['filename']);$imgDataArr[$k] = $in_rule.'/'.($uid%100)."/headphoto_$uid.jpg";break;case 'c_zhengshu':$imgDataArr[$k] = $in_rule.'/'.$v['filename'];break;case 'gongdi_pic':$imgDataArr[$k] = $in_rule.'/'.$v['filename'];break;case 'xiaoguotu_topic':$imgDataArr[$k] = $in_rule.'/'.$v['filename'];break;/*case 'xiaoguotu5':break;case 'xiaoguotu6':break;case 'xiaoguotu7':break;case 'xiaoguotu8':break;case 'xiaoguotu9':break;*/default :$imgDataArr[$k] = $in_rule.'/'.$v['filename'];break;}}//图片转换echo '<br> imgDataArrToWebp start time <br>'.date("Y-m-d H:i:s");imgDataArrToWebp($imgDataArr);echo '<br> imgDataArrToWebp end time <br>'.date("Y-m-d H:i:s");}else{//结束条件echo '<br> 没有结果呢... <br>'.date("Y-m-d H:i:s");break;}if($page == 3){//结束条件echo '<br> 有数据哟 不过已经结束了... <br>'.date("Y-m-d H:i:s");break;}$page ++;}echo '<br> foreach result time <br>'.date("Y-m-d H:i:s");return true;}/*** @功能:数组中的图片按规则转换成 WEBP 格式* @参数:$picdata => 源图片数组,* @返回:若转换成功返回true,否则返回false或直接跳出*/function imgDataArrToWebp($picdata){//图片规则$images_rule = array('smallcase'=>array('r','s','t',254,508,284,480),'case'=>array('s','c','831',480,'592x344M','140x140M'),'home'=>array('s','r','280x280M'),'huodong'=>array('c',730),'attch'=>array('s','r','140x140M','80x80M','600',480,'180x180M','560x315M','640x360M','640x580M','600x338M'),'user'=>array(120,130,60),'renmai'=>array(600,440,80,'p'),'vr'=>array(223),'gongdi_pic'=>array(80,160,240,'230x230M'),'ask'=>array('s'),'mall'=>array('m',280,180,80,50,480),'photo'=>array('s'),'news_center'=>array('s'),'xiaoguotu_topic'=>array(480,284,588),'live'=>array('600',480,'180x180M','560x315M','640x360M','140x140M','80x80M','640x580M','600x338M'),'c_zhengshu'=>array(180),);echo '<pre>';//print_r($picdata);exit;if(!is_array($picdata)){return false;}$imgiconnum = $imgiconsum = 0;$t = 0;echo '<br> foreach picdata start <br>'.date("Y-m-d H:i:s");foreach ($picdata as $k2 => $v2) {//echo $k2;//echo '<br>';$imgiconsum ++;$to_path = pathinfo($v2);$in_rule = explode('/',$to_path['dirname']);/*[dirname] => case/1501/15[basename] => 20150115_84991cead0e29c29a14dr5a7kn6bu7qn.jpg[extension] => jpg[filename] => 20150115_84991cead0e29c29a14dr5a7kn6bu7qn*///print_r($to_path);if($images_rule[$in_rule[0]]){echo '<br> 正在图片转换.... <br>'.date("Y-m-d H:i:s");//原图转换$srcurl = PIC_DOMAIN.$to_path['dirname'].'/'.$to_path['basename'].'.webp';askimgtohtml($srcurl);//缩略图转换foreach ($images_rule[$in_rule[0]] as $k3 => $v3) {// echo '<br>';$url = PIC_DOMAIN.$to_path['dirname'].'/'.$to_path['filename'].'_'.$v3.'.'.$to_path['extension'].'.webp';// echo $url;askimgtohtml($url);// echo '<br>';}}else{echo '<br> 不做图片转换工作 <br>'.date("Y-m-d H:i:s");}echo '<br> the '.($k2+1).' end <br>'.date("Y-m-d H:i:s");}echo '<br> foreach picdata end <br>';echo '<br>---sur- result : $imgiconnum '.$imgiconnum.'+++++ $imgiconsum '.$imgiconsum.'-----<br>';return true;}/**** 访问URL 生成WEBP* @param string $url 图片URL*/function askimgtohtml($url){$cmd=$retext=$args=$str = '';//判断所在系统$isSystem = stripos(PHP_OS,"WIN")>-1;if ($isSystem) {$cmd = "wget -O c:/null $url ";}else{$cmd = "wget -O /dev/null $url";//$retext = implode(",",$return);//$linenum = __LINE__;//操作日志//rewritetolog('#html+'.$linenum.'#', $cmd, $retext, $args);}//$str = exec($cmd,$return,$args);//echo '<br> 输出 cmd '.date("Y-m-d H:i:s").'--->';echo $cmd;//控制访问时间sleep(0.2);}/**** 写文件* @param string $file 文件路径* @param string $str 写入内容* @param char $mode 写入模式*/function writeFile($file,$str,$mode='w'){$oldmask = @umask(0);$fp = @fopen($file,$mode);@flock($fp, 3);if(!$fp){Return false;} else {@fwrite($fp,$str);@fclose($fp);@umask($oldmask);Return true;}}/*** @功能:日志记录* @参数:$intype => 要记录日志的类型,* $cmd => 执行的LINUX命令,* $return => 返回结果集,* $args => 返回结果状态,0表示成功,非0表示失败* $logger => 是否要记录日志* @返回:若转换成功返回true,否则返回false或直接跳出* 显示行号 __LINE__*/function rewritetolog($intype='', $cmd='', $return='', $args='', $logger =true) {if($logger){$log = '#Suc#';if($args){$log = '#Err#';}$log = $log.$intype.':'.date("Y-m-d H:i:s").":#cmd#".$cmd."#return#".$return."#args#".$args."<br>\n";$fileName = "../syslog/imglogs/".date('Y-m-d').".log";writeFile($fileName,$log,'a');}}三、getimgdatafromhtml<?phpecho '<br> getimgdatafromhtml start <br>';echo date('h:i:s') . "<br />";//暂停 10 秒sleep(2);//重新开始echo date('h:i:s');echo '<pre>';echo '<br> require imgfile start <br>';//#读出的方法require("caseimgarrdata.php");//print_r($picdata);echo '<br> require imgfile end <br>';$imgiconnum = $imgiconsum = 0;$t = 0;echo '<br> foreach picdata start <br>';foreach ($picdata as $k2 => $v2) {echo $k2;$imgiconsum ++;$to_path = pathinfo($v2);//print_r($to_path);//makeDir( $to_path['dirname'] );$url = 'http://pic.to8to.com/'.$v2;//$url = 'http://img.to8to.com/'.$v2;//echo $url;//echo '<br>';sleep(2);$res = getImage($url,$to_path['dirname'],$to_path['basename']);//print_r($res);if($res['error']=='0'){$imgiconnum ++;}}echo '<br> foreach picdata end <br>';echo '<br>---sur- result : $imgiconnum '.$imgiconnum.'+++++ $imgiconsum '.$imgiconsum.'-----<br>';echo '<br> all end <br>';exit;/*** @功能:创建目录* @参数:$path => 要创建的目录, $mod => 指定权限* @返回:若创建成功返回true,否则返回false或直接跳出*/function makeDir($path, $mod = 0777) {if (stripos(PHP_OS, 'win') === false) {$cmd = 'mkdir -p '.$path;} else {$path = str_replace('/', '\\', $path);$cmd = 'mkdir '.$path;}exec($cmd);return true;}/*02*功能:php完美实现下载远程图片保存到本地03*参数:文件url,保存文件目录,保存文件名称,使用的下载方式04*当保存文件名称为空时则使用远程文件原来的名称05*/function getImage($url,$save_dir='',$filename='',$type=0){if(trim($url)==''){return array('file_name'=>'','save_path'=>'','error'=>1);}if(trim($save_dir)==''){$save_dir='./';}if(trim($filename)==''){//保存文件名$ext=strrchr($url,'.');if($ext!='.gif'&&$ext!='.jpg'&&$ext!='.png'){return array('file_name'=>'','save_path'=>'','error'=>3);}$filename=time().$ext;}if(0!==strrpos($save_dir,'/')){$save_dir.='/';}//创建保存目录if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){return array('file_name'=>'','save_path'=>'','error'=>5);}//获取远程文件所采用的方法if($type){$ch=curl_init();$timeout=5;curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);$img=curl_exec($ch);curl_close($ch);}else{ob_start();readfile($url);$img=ob_get_contents();ob_end_clean();}//$size=strlen($img);//文件大小$fp2=@fopen($save_dir.$filename,'a');fwrite($fp2,$img);fclose($fp2);unset($img,$url);return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);}