mpdf 使用图像时除以零

mpdf division by zero when using images

我正在尝试使用 mpdf 库构建 pdf...

        include('convert-to-pdf/mpdf.php');


        // build the content for the preview...

        ob_start();

                include (DIR_BOOKS.'/'.$book_path.'/preview-male.php');
                $html = ob_get_contents();
        ob_end_clean(); 

        // build the overlay on the child image 

        $mpdf=new mPDF('utf-8', array(221,221));


        $stylesheet = file_get_contents(DIR_BOOKS.$book_path.'/stylesheet.css');
        $mpdf->WriteHTML($stylesheet,1);    
        $mpdf->WriteHTML($html,2);      

        $mpdf->Output(DIR_TEMPORARY_IMAGES.$this->session->data['pdf_id'].'.pdf','F');

然后是 mpdf 中的代码,我在其中遇到错误...

$this->WriteHTML($html , 4);    // parameter 4 saves output to $this->headerbuffer
$actual_h = $this->y - $y;
$use_w = $w;
$use_h = $h;

//Line 13865 
$ratio = $actual_h / $use_w;

if ($overflow!='hidden' && $overflow!='visible') {
    //Line 13868 
    $target = $h/$w;
    if (($ratio / $target ) > 1) {
        $nl = CEIL($actual_h / $this->lineheight);
        $l = $use_w * $nl;
        $est_w = sqrt(($l * $this->lineheight) / $target) * 0.8;
        $use_w += ($est_w - $use_w) - ($w/100);
    }
    //Line 13875
    $bpcstart = ($ratio / $target);
    $bpcctr = 1;
    while(($ratio / $target ) > 1) {

        if ($this->progressBar) { $this->UpdateProgressBar(4,intval(100/($ratio/$target)),('Auto-sizing fixed-position block: '.$bpcctr++)); }  // *PROGRESS-BAR*

        $this->x = $x;
        $this->y = $y;

        if (($ratio / $target) > 1.5 || ($ratio / $target) < 0.6) {
            $use_w += ($w/$this->incrementFPR1);
        }
        else if (($ratio / $target) > 1.2 || ($ratio / $target) < 0.85) {
            $use_w += ($w/$this->incrementFPR2);
        }
        //Line 13890
        else if (($ratio / $target) > 1.1 || ($ratio / $target) < 0.91) {
            $use_w += ($w/$this->incrementFPR3);
        }
        else {
            $use_w += ($w/$this->incrementFPR4);
        }

        $use_h = $use_w * $target ;
        $this->rMargin=$this->w - $use_w - $x;
        $this->pgwidth = $this->w - $this->lMargin - $this->rMargin;
        $this->HTMLheaderPageLinks = array();
        $this->HTMLheaderPageAnnots = array();
        $this->HTMLheaderPageForms = array();
        $this->pageBackgrounds = array();
        $this->WriteHTML($html , 4);    // parameter 4 saves output to $this->headerbuffer
        $actual_h = $this->y - $y;
        $ratio = $actual_h / $use_w;
    }
    if ($this->progressBar) { $this->UpdateProgressBar(4,'100',' '); }  // *PROGRESS-BAR*
}
$shrink_f = $w/$use_w;

我的 php 文件正在从中获取 html...

<div class="page-1-image-1"><img src="https://www.example.com/working-image.jpg" /> </div>

当我在我的 mpdf html 代码中包含图像时出现以下错误....

<b>Warning</b>: Division by zero in <b>/var/www/html/example/mpdf.php</b> on line <b>13865</b>
<b>Warning</b>: Division by zero in <b>/var/www/html/example/mpdf.php</b> on line <b>13868</b>
<b>Warning</b>: Division by zero in <b>/var/www/html/example/mpdf.php</b> on line <b>13869</b>
<b>Warning</b>: Division by zero in <b>/var/www/html/example/mpdf.php</b> on line <b>13875</b>
<b>Warning</b>: Division by zero in <b>/var/www/html/example/stuff.php</b> on line <b>13877</b>
<b>Warning</b>: Division by zero in <b>/var/www/html/example/mpdf.php</b> on line <b>13910</b>
<B>mPDF error: </B>Please do not use values equal to zero for scaling

我已经尝试了自己新生成的图像,也尝试了其他非常好的在线图像 url,但没有任何乐趣。我试过 png 和 jpg。

我怀疑这些错误是因为它根本没有加载图像的问题。提前致谢。

您正在尝试将一个数除以 0,这是行不通的!这在数学中是不允许的。 (例如 5 / 0 = ? -> ? * 0 = 5

例如这一行:

$ratio = $actual_h / $use_w;

你得到一个错误,因为 $use_w 是 0!那么如何防止这些错误呢?


您可以这样检查:

if($use_w != 0) {
    //Here you could end the script OR assign it to 1, just make sure you never use $use_w with the value 0
    $ratio = $actual_h / $use_w;
}