TCPDF图像宽高比问题

时间:2015-05-26 18:29:06

标签: php tcpdf

我有TCPDF图像插入问题。

enter image description here

这是我正在使用的代码:

$this->Image($this->data['logo'], PDF_MARGIN_LEFT, 5, 60, 20, '', '', 'M', true);

我将resize设置为true,但TCPDF库不尊重图像比率。

如何强制保留图像比例?

有关我正在使用TCPDF 6.2.8(2015-04-29)的信息。

感谢您的支持, 大卫。

2 个答案:

答案 0 :(得分:2)

这是我找到的解决方案:

    if ('' != $this->data['logo'] && is_file($this->data['logo'])) {
        // select only one constraint : height or width. 60x20 image emplacement => ratio = 3 (60/20)
        list($image_w, $image_h) = getimagesize($this->data['logo']);
        if (3 > $image_w / $image_h) list($w, $h) = array(0, 20);
        else list($w, $h) = array(60, 0);

        $this->Image($this->data['logo'], PDF_MARGIN_LEFT, 5, $w, $h, '', '', 'M');
    }

我发现了图像进驻率(60/20 = 3),我把它与图像比率进行了比较,我在设定宽度或高度之间做出选择。

感谢JamesG帮助我找到解决方案。

大卫。

答案 1 :(得分:1)

如果要保留图像的宽高比,只需将width参数或height参数设置为零。

在您的示例中,我可以看到您已将宽度设置为60,将高度设置为20.尝试将宽度设置为0并将高度设置为20 - 我认为您将获得良好的结果。你也可以省去resize参数。