IMAGE_TO_FPDF

$true_array_image = CommonUtils::to_pdf_image_handle($image_array,$max_pt,'/saved_attachments/',$to_pdf_save_dir,$the_id,$postfix_str);

$status_code =  CommonUtils::generate_pdf($true_array_image,'/saved_attachments/',$to_pdf_save_dir,$the_id,
    $postfix_str,$binary_file_name,$max_pt['width'],$max_pt['height'],1);

等比例计算所有图片取大小

static function to_pdf_image_handle($image_array,&$max_pt,$sub_path,$save_file_name_type,$the_id,$postfix_str){
    $max_pt["width"] = "0";
    $max_pt["height"] = "0";
    $true_array_image = array();
    foreach($image_array as $k => $v){
        $img_info = getimagesize($v);
        $imgtype = substr($v,strrpos($v,'.')+1);
        $mime =str_replace('jpeg','jpg', str_replace('image/','',$img_info['mime']));

        switch ($save_file_name_type){
            case IMAGE_TO_PDF_GENERATE_SAVE_TYPE_FOR_HANDOUT:
                $binary_identifier_str = "courseMaterials";
                break;
         }
        $file_name =  . (substr($v, strlen(HQ_SC_HANDOUT), strlen($v) - strlen(HQ_SC_HANDOUT)));

        if($imgtype != $mime){
            $file_subdirectory = RESOURCE_PATH . $sub_path . $binary_identifier_str. "_" . ($the_id%100) . "/sub_". (($the_id/100)%100) . "/";
            $imgurl_new = $file_subdirectory.$k.".".$mime;

            if(!is_dir($file_subdirectory)){
                if (!mkdir($file_subdirectory, 0777, true)) {
                    return  ERR_BINARY_FILE_SAVE;
                }
            }
            $topng = copy($file_name,$imgurl_new);
            if ($topng){
                $file_name = $imgurl_new;
            }
        }


        if (!file_exists($file_name)){
            continue;
        }
        $true_array_image[] = $file_name;
        if ($img_info[0] > $max_pt["width"]){
            $max_pt["width"] = $img_info[0];
        }
        if ($img_info[1] > $max_pt["height"]){
            $max_pt["height"] = $img_info[1];
        }
    }
    $max_pt['width'] = $max_pt["width"] /4 *3 + 40;
    $max_pt["height"] = $max_pt["height"] /4 *3 + 40;
    return $true_array_image;
}

生成PDF

static function generate_pdf($for_image,$sub_path,$save_file_name_type,$the_id,$postfix_str,&$binary_file_name,$width,$height,$is_delete=0){
    define('FPDF_FONTPATH','/data/HQ/SC_Educations/hq_htdocs/HQ_Educations/library/FPDF/font/'); //定义font 文件夹所在路径
    $pdf = new FPDF('L', 'pt', [$width,$height]); //创建新的FPDF 对象,竖向放纸,单位为毫米,纸张大小A4

    $pdf->SetFont('Courier','I',20); //设置字体样式
    //转成数组

    switch ($save_file_name_type){
        case IMAGE_TO_PDF_GENERATE_SAVE_TYPE_FOR_HANDOUT:
            $binary_identifier_str = "courseMaterials";
            break;
    }

    $file_subdirectory = RESOURCE_PATH . $sub_path . $binary_identifier_str. "_" . ($the_id%100) . "/sub_". (($the_id/100)%100) . "/";


    foreach($for_image as $k => $v){
        $pdf->AddPage(); //增加一页

        $pdf->Image($v,20,20,0,0); //增加一张图片,文件名为sight.jpg
    }
    if (!file_exists($file_subdirectory)){
        if (!mkdir($file_subdirectory, 0777, true)) {
            return  ERR_BINARY_FILE_SAVE;
        }
    }

    //扫描目录获取相关文件
    if (is_dir($file_subdirectory) && $is_delete){
        $handles = scandir($file_subdirectory);
        $postfix_first = $the_id."_".explode('_',$postfix_str)[0];
        foreach ($handles as $handle){
            if($postfix_first == substr($handle,0,strlen($postfix_first))){
                @unlink($file_subdirectory.$handle);
            }
            if (substr(strrchr($handle, '.'), 1) == "png"){
                @unlink($file_subdirectory.$handle);
            }
        }
    }

    $binary_file_name = $file_subdirectory .$the_id. "_". $postfix_str. ".pdf";
    $pdf->Output($binary_file_name,'F');

    $binary_file_name = substr($binary_file_name, strlen(RESOURCE_PATH), strlen($binary_file_name) - strlen(RESOURCE_PATH));
    return SC_SUCCESS;
}
ForDream:当前文章用户名 http://lowfk.com/index.php/2021/08/24/112.html:当前文章地址
THE END
分享
二维码
< <上一篇
下一篇>>