TCPDF Image aspect ratio issue

烂漫一生 提交于 2020-01-14 03:36:09

问题


I have an issue with TCPDF Image inserting.

Here's the code i'm using :

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

I've set the resize at true, but the TCPDF library do not respect image ratio.

How can I force preserving image ratio ?

For information I'm using TCPDF 6.2.8 (2015-04-29).

Thanks for your support, David.


回答1:


Here's the solution I found :

    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');
    }

I've found the image emplacement ratio (60/20=3), I compared it to the image ratio and I choose between set the width or the heigh.

Thanks to JamesG to help me to find my solution.

David.




回答2:


If you want to preserve the image's aspect ratio, just set either the width parameter or the height parameter to zero.

In your example I can see that you've set the width to 60 and the height to 20. Try setting the width to 0 and leaving the height at 20 - I think you will get a good result. And you can probably leave off the resize parameter too.



来源:https://stackoverflow.com/questions/30466192/tcpdf-image-aspect-ratio-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!