(在线文库系统)centos nginx mysql php openoffice pdf2swf pdf2image

万高轩
2023-12-01

(在线文库系统)centos+nginx+mysql+php+openoffice+pdf2swf+pdf2image架构方案

总体架构:

前端web服务器+数据库服务器+文件转换服务器+文件存储服务器

总体描述:

前端web页面上传文件,后端程序将数据写入mysql,并将文件发送到文件服务器,文件服务器定时查询mysql取出需要转换文件列表,用php脚本调用linux命令执行转换文件操作,转换完成后将数据库对应记录更新,并将文件发送到文件存储服务器。

一.将文件发送到文件转换服务器

  1. web端

php通过curl发送文件

  1. 文件转换服务器

搭建一个web服务,只需要2个东东:upload.php、uploads文件夹

upload.php代码如下:

<?php
define('MY_ROOT', str_replace('/upload.php', '', str_replace('\\', '/', __FILE__)));
if(isset($_FILES)){
    if(isset($_FILES['file'])){
        try {
            move_uploaded_file($_FILES['file']['tmp_name'], MY_ROOT.'/uploads/'.$_FILES['file']['name']);
            @unlink($GLOBALS[$_FILES['file']['tmp_name']]);
            echo json_encode(array('success'=>TRUE,'error'=>''));
        } catch (Exception $exc) {
            //echo $exc->getTraceAsString();
            echo json_encode(array('success'=>FALSE,'error'=>'移动文件失败'));
        }
    }
}
?>

二.文件转换php脚本核心代码如下

#!/usr/local/php.old/bin/php -q 
<?php
date_default_timezone_set('PRC');
error_reporting(E_ALL);

/* 取得当前所在的根目录 */
define('MY_ROOT', str_replace('/setup.php', '', str_replace('\\', '/', __FILE__)));
include_once(MY_ROOT."/include/lib_mysql.php");//mysql操作类
require_once(MY_ROOT.'/include/aliyun/aliyun.php');
include_once(MY_ROOT."/config.php");
use \Aliyun\OSS\OSSClient;

$client = OSSClient::factory(array(
    'AccessKeyId' => $AccessKeyId,
    'AccessKeySecret' => $AccessKeySecret,
));





$db = new lib_mysql($db_host, $db_user, $db_pass, $db_name);

while (true){

$sql = "select aid,softlinks from t_addonbook where wwk=0 ";
$list = $db->getAll($sql);


    //循环处理需要转换的文档
    foreach($list as $item){
        //获取源文档的上传路径
        $local_path = $item['softlinks'];//{dede:link islocal='1' text='本地下载'} /uploads/userup/1088/9-30.xls {/dede:link}
        $tmp_str = strpos($local_path, '}')+1;
        $local_path = substr($local_path, $tmp_str);//去除 {dede:link islocal='1' text='本地下载'}
        $tmp_str = strpos($local_path, '{/dede:link}');
        $local_path = substr($local_path, 0, $tmp_str);//去除 {/dede:link}
        $local_path = str_replace(" ", "", $local_path);
        $local_file_name = basename($local_path);//文件名+扩展名
        $file_name_arr = explode(".", $local_file_name);
        $file_name = $file_name_arr[0];
        $file_type = end($file_name_arr);
        $file_type = strtolower($file_type);
        $tmp_name_str = strpos($local_path, $local_file_name);
        $local_path = substr($local_path, 0,$tmp_name_str);//去除文件名
        if(file_exists($upload_path.$local_file_name)){
            if(copy($upload_path.$local_file_name, $convert_path.$local_file_name)){//将文件
                //将文件转换为pdf
                $start = time();

                if($file_type=='txt'){
                     if(!isUTF8($convert_path.$local_file_name)){//若不是UTF8编码,则转码
                         $temp = exec("enca -i ".$convert_path.$local_file_name);
                         if(strrpos($temp, '???') === false){//是gbk
                             exec("iconv -f gbk -t utf-8 ".$convert_path.$local_file_name." -o ".$convert_path.$local_file_name);//txt 转码 
                         }else{
                             exec("iconv -f unicode -t utf-8 ".$convert_path.$local_file_name." -o ".$convert_path.$local_file_name);//txt 转码 
                         }
                     }
                }

                if($file_type!='pdf'){
                    exec($openoffice_path." -f pdf ".$convert_path.$local_file_name);//转换为pdf
                }
                //生成缩略图
                exec("cd ".$pdfimg_path);
                $temp = exec("cd ".$pdfimg_path."&& ./pdf2img -f 1 -w 150 -h 200 ".$convert_path.$file_name.".pdf ".$convert_path.$file_name.".jpg");
                //生成swf
                $temp = exec($swftools_path." -f -T 9 -t ".$convert_path.$file_name.".pdf -o ".$convert_path.$file_name.".swf");
                 if(strrpos($temp, 'NOTICE') === false){//第一种那个方式转换失败
                     //第二种转换方式
                     $temp = exec($swftools_path." -f -T 9 -t -s poly2bitmap ".$convert_path.$file_name.".pdf -o ".$convert_path.$file_name.".swf");
                 }
                echo "1->".$item['aid'].' : ';print_r($temp);echo "\n";
                //获取pdf总页数
                $pagenum = getPageTotal($convert_path.$file_name.".pdf");

                if(!file_exists($convert_path.$file_name.".jpg")){//jpg
                    exec($mv_path."mv ".$convert_path.$file_name."0001.jpg ".$convert_path.$file_name.".jpg");
                }
                //将swf,jpg和源文件拷贝到文件服务器
                /*上传swf*/
                $client->putObject(array(
                    'Bucket' => 'bucket-wenku',
                    'Key' => $file_name.'.swf',
                    'Content' => fopen($convert_path.$file_name.'.swf', 'r'),
                    'ContentLength' => filesize($convert_path.$file_name.'.swf'),
                ));
                /*上传jpg*/
                $client->putObject(array(
                    'Bucket' => 'bucket-wenku',
                    'Key' => $file_name.'.jpg',
                    'Content' => fopen($convert_path.$file_name.'.jpg', 'r'),
                    'ContentLength' => filesize($convert_path.$file_name.'.jpg'),
                ));
                /*上传源文件*/
                $client->putObject(array(
                    'Bucket' => 'bucket-wenku',
                    'Key' => $local_file_name,
                    'Content' => fopen($convert_path.$local_file_name, 'r'),
                    'ContentLength' => filesize($convert_path.$local_file_name),
                ));
                //写入数据库
                //$onlineviewurl = '/swf/'.$file_name.'.swf';
                $onlineviewurl = '/load.php?file='.$file_name.'.swf';
                $sql = "update t_addonbook set onlineviewurl='$onlineviewurl',wwk=1,pagenumber=$pagenum where aid=$item[aid]";
                $db->query($sql);
                //$litpic = '/swf/'.$file_name.'.jpg';
                $litpic = '/load.php?file='.$file_name.'.jpg';
                $sql = "update t_archives set litpic='$litpic' where id=$item[aid]";
                $db->query($sql);


                //删除文件
                exec("rm -rf ".$convert_path.$file_name."*");
                //删除上传目录文件
                exec("rm -rf ".$upload_path.$local_file_name);
            }else{
                //复制文件失败
            }
        }else{
            //写入错误日志
        }
        sleep(1);
    }
    echo "5\n";
    sleep(5);
}




/**
    * 获取PDF的页数
    */
function getPageTotal($path){

        // 打开文件
        if (!$fp = @fopen($path,"r")) {
            return false;
        }
        else {
            $max=0;
            while(!feof($fp)) {
                $line = fgets($fp,255);
                if (preg_match('/\/Count [0-9]+/', $line, $matches)){
                    preg_match('/[0-9]+/',$matches[0], $matches2);
                    if ($max<$matches2[0]) $max=$matches2[0];
                }
            }
            fclose($fp);
            // 返回页数
            return $max;
        }

  }

  function isUTF8($str)
  {
      $str1 = file_get_contents($str);
      $code = chkCode($str1);
      if($code=='UTF-8'){
          return TRUE;
      }else{
          return FALSE;
      }
  }
  function chkCode($string){
    $code = array('UTF-8','GBK','GB18030','GB2312');
    foreach($code as $c){
     if( $string === iconv('UTF-8', $c, iconv($c, 'UTF-8', $string))){
      return $c;
     }
    }
    return "no";
}
?>

github上面看到也有其他方法: https://github.com/yoozi/swf-docs-generator


转载自:http://www.9958.pw/post/wenku

 类似资料: