PHP Customer Function

本文介绍了一种批量处理字体文件的方法,包括去除特殊字符、替换空格等,并提供了PDF转doc、html及png的实现方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 修改字体文件名中的特殊字符 start
$path = "/usr/share/nginx/font/wp-content/data-deal/font-file-extract";
if($dh = opendir($path)){
    $i = 0;
    $path_arr = array();
    $font_type = array('.ttf','.TTF','.OTF','.otf');
    while (($file = readdir($dh)) !== false){
        if($file=="." || $file=="..") continue;
        $tmp_dir = $path."/".$file;
        $path_arr[] = $tmp_dir;
    }
    closedir($dh); 
    if(!empty($path_arr)){
        foreach($path_arr as $val){
            $i+=1;
            $cc = getFile($val);
            $aa = '';
            $f_new = $val;
            $val_o = $val;
            foreach($cc as $key){
                $key = str_replace('-----', '', $key);
                if(strpos($key,' ')!==false||strpos($key,',')!==false||strpos($key,'&')!==false||strpos($key,"'")!==false||strpos($key,"(")!==false||strpos($key,")")!==false||strpos($key,"!")!==false){

                    $type = strrchr($key,'.');
                    if(in_array($type,$font_type)){
                        $f_new .= '/'.sanitize_title(str_replace($type, '', $key)).$type;
                    }else{
                        $f_new .= '/'.sanitize_title($key);
                    }
                }else{
                    $f_new .= '/'.$key;
                }
                $val_o .='/'.$key;
                if($val_o!=$f_new){
                    if(is_dir($val_o)){
                        $flag = rename($val_o,$f_new);
                    }elseif(is_file($val_o)){
                        $flag = rename($val_o,$f_new);
                    }
                    if($flag){
                        $val_o = $f_new;
                        echo 'yes<br>';
                    }else{
                        echo $val_o."<br>";
                    }
                }
            }
        }
        echo $i;
    }   
}
// 修改字体文件名中的特殊字符 end


//获取指定类型文件在文件夹中的位置 start
function getFile( $dir, $fileArray=array(),$dir_o='',$floder_arr =array()){
    if($dir_o==''){
        $dir_o = $dir;
    }
    $font_type = array('.ttf','.TTF','.OTF','.otf');
    $prefix_arr = array('.','_');
    if($dh = opendir($dir)){
        while (($file = readdir($dh)) !== false){
            if($file=="." || $file=="..") continue;
            $tmp_dir = $dir."/".$file;
            $file_type = strrchr($file,'.');
            $tr_floder = basename($dir).'-----';

            if(!(is_dir($tmp_dir))&&in_array($file_type,$font_type)&&!in_array(substr($file, 0,1),$prefix_arr)){
                if( ( !empty($fileArray)||$dir!=$dir_o )){
                    $fileArray[] = $tr_floder;
                }
                $fileArray[] = $file;
                return $fileArray;                         
            }elseif(is_dir($tmp_dir)){
                if( $dir != $dir_o){
                    $fileArray[] = $tr_floder;
                }
                $floder_arr[] = $file; 
            }
        }
        if(!empty($floder_arr)){
            foreach($floder_arr as $floder){
                if(!empty($fileArray)){
                                if(in_array(strrchr(end($fileArray),'.'),$font_type)&&!in_array(substr(end($fileArray), 0,1),$prefix_arr)){
                        break;
                    }
                }
                $tr_path = $dir.'/'.$floder;
                $file_arr = scandir($tr_path); 
                $file_count = count($file_arr)-2;
                if($file_count>0){
                    $fileArray = getFile($tr_path,$fileArray,$dir_o);
                    if(!empty($fileArray)){
                        if(is_file($tr_path.'/'.end($fileArray))){
                            return $fileArray; 
                        }
                    }
                }   
            }   
        }else{
            $fileArray=array();
        }
        closedir($dh); 
    }
    return $fileArray; 
}
//获取指定类型文件在文件夹中的位置 end


//  文件和文件夹中空格替换 start
$source_path = '/usr/share/nginx/';
if($dh = opendir($source_path)){
    $flo_arr = array();
    while (($file = readdir($dh)) !== false){
        if($file=="." || $file=="..") continue;
        $flo_arr[] = $file;
    }
    foreach($flo_arr as $floder){
        $floder_path = $source_path.$floder;
        if($dh = opendir($floder_path)){
            while (($file = readdir($dh)) !== false){
                if($file=="." || $file=="..") continue;
                $file_new = str_replace(' ','-',$file);
                $flag = rename($floder_path.'/'.$file,$floder_path.'/'.$file_new);
            }
        }
        $flag = rename($floder_path,str_replace(' ','-',$floder_path));
    }
}


//  文件和文件夹中空格替换 end
//pdf 转换 doc html png start
$source_path = '/usr/share/nginx/thinkphp_job_application_online_28_13/ja_resume/';
// $out_html_path = 'ja_resume_html/';
$out_png_path = 'ja_resume_png/';
$out_doc_path = '/usr/share/nginx/thinkphp_job_application_online_28_13/ja_resume_doc/';
// $flag = pdf2html($source_path, $out_html_path, $superfix = 0);
if($dh = opendir($source_path)){
    $flo_arr = array();
    while (($file = readdir($dh)) !== false){
        if($file=="." || $file=="..") continue;
        $flo_arr[] = $file;
    }
    foreach($flo_arr as $floder){
        $floder_path = $source_path.$floder;
        if($dh = opendir($floder_path)){
            while (($file = readdir($dh)) !== false){
                if($file=="." || $file=="..") continue;
                if(strpos($file,'.pdf')!==false){
                    $source_pdf = $floder_path.'/'.$file;
                    // $html_floder = $out_html_path.str_replace('.pdf','',$file);
                    // $flag = pdf2html($source_pdf, $html_floder, $superfix = 0);
                    // $png_floder = $out_png_path.str_replace('.pdf','',$file);
                    // $flag = pdf2jpg($source_pdf, $png_floder, $width = 748, $height = 967);
                    $doc_floder = $out_doc_path.str_replace('.pdf','.doc',$file);
                    $cmd = "sudo convert ".$source_pdf." ".$doc_floder;
                    exec($cmd, $result, $status);
                }
            }
        }
    }
}

/**
 * [pdf2html pdf2html]
 * @param  [type]  $source_pdf    [pdf path]
 * @param  [type]  $output_folder []
 * @param  integer $superfix      [description]
 * @return [type]                 [false handle faile,number pdf pages]
 */
function pdf2html($source_pdf, $output_folder, $superfix = 0) {
    $cmd = "pdf2htmlEX"; //linux
    $output_folder = '.' . DIRECTORY_SEPARATOR . trim($output_folder, './\/');
    if (!is_dir($output_folder)) {
        exec('sudo mkdir -m0777 -p ' . $output_folder);
    }
    $cmd = "sudo $cmd --no-drm 1 --embed-css 0 --embed-image 0 --embed-font 0 --split-pages 1 --fit-width 748 --css-filename html.css --dest-dir \"$output_folder\" --page-filename page-%d.html --embed-external-font " . $superfix . " --auto-hint 1 \"$source_pdf\"";
    exec($cmd, $result, $status);
    if ($status === 0) {
        $i = 1;
        while (file_exists($output_folder . "/page-" . $i . ".html")) {
            $content = file_get_contents($output_folder . "/page-" . $i . ".html");
            $content = preg_replace("/(<img[^>]+)src=\"(.+)\/>/", "$1" . "src=\"" . ltrim($output_folder, '.') . '/' . "$2" . "/>", $content);
            unlink($output_folder . "/page-" . $i . ".html");
            file_put_contents($output_folder . "/page-" . $i . ".html", $content);
            $i += 1;
        }
        if (file_exists($output_folder . '/pdf2htmlEX-64x64.png')) {
            unlink($output_folder . '/pdf2htmlEX-64x64.png');
        }
        return $i - 1;
    }
    return false;
}

function pdf2jpg($pdf, $path, $width = 728, $height = 943) {
    if (!extension_loaded('imagick') || !file_exists($pdf)) {
        return false;
    }
    if (!preg_match("#\.pdf$#i", $pdf)) {
        return false;
    }

    $return = array();

    $return['pagecount'] = getPDFPages($pdf);
    if (!$return['pagecount']) {
        return false;
    }
    $img = new Imagick();
    //$img->setResolution(80, 80); //设置图像分辨率
    //$img->setCompressionQuality(100); //压缩比
    //文件有坏的,增加try,catch功能
    $path_old = $path;
    $end = $return['pagecount'];
    for ($i = 0; $i < $end; $i++) {
        $path = $path_old;
        try {
            $img->readImage($pdf . "[" . $i . "]"); //设置读取pdf
        } catch (Exception $e) {
            $exp = fopen('./fileerror.csv', 'a');
            $msg = array($pdf, $e->getMessage());
            fputcsv($exp, $msg);
            fclose($exp);
            return false;
        }
        $child_f = str_replace('.pdf', '', basename($pdf));
        $path .= '/' . $child_f;
        if (!is_dir($path)) {
            @mkdir($path, 0777, true);
        }
        $img->setImageFormat('png'); //格式

        $path .= "/" . $child_f;
        $filename = $path . '-l' . ($i + 1) . '.png';
        //return $filename;
        $img->scaleImage($width, $height, true);
        if ($img->writeImage($filename) == true) {
            $filename = $path . '-s' . ($i + 1) . '.png';
            $img->scaleImage(125, 162, true);
            $img->writeImage($filename);
            //写入
            $img->clear();
            $img->destroy();
        }

        $return[] = $filename;
    }
    return $return;
}


function pdfinfo($pdf) {
    $result = array();
    $cmd = "/usr/bin/pdfinfo"; // Linux
    $cmd = realpath($cmd);
    exec("$cmd \"$pdf\"", $output);
    $pagecount = 0;
    foreach ($output as $op) {
        if (preg_match("/Pages:\s*(\d+)/i", $op, $matches) === 1) {
            $pagecount = intval($matches[1]);
            $result['pages'] = $pagecount;
            continue;
        }
        if (preg_match("/Page size:\s*(\d+\.?\d+).*?(\d+\.?\d+)/i", $op, $match) === 1) {
            $result['width'] = doubleval($match[1]);
            $result['height'] = doubleval($match[2]);
            continue;
        }
    }
    return $result;
}

/*
 *读取pdf页数
 */
function getPDFPages($document) {
    $cmd = "/usr/bin/pdfinfo"; // Linux
    $cmd = realpath($cmd);
    exec("$cmd \"$document\"", $output);
    // Iterate through lines
    $pagecount = 0;
    foreach ($output as $op) {
        // Extract the number
        if (preg_match("/Pages:\s*(\d+)/i", $op, $matches) === 1) {
            $pagecount = intval($matches[1]);
            break;
        }
    }
    return $pagecount;
}
//pdf 转换 doc html png end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值