Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-在附加在线pdf(mpdf)时调整图像大小_Php_Image_Mpdf_Gdlib - Fatal编程技术网

PHP-在附加在线pdf(mpdf)时调整图像大小

PHP-在附加在线pdf(mpdf)时调整图像大小,php,image,mpdf,gdlib,Php,Image,Mpdf,Gdlib,我正在生成一个包含一些图像的pdf。由于图像的大小很大,生成的PDF的大小也会很大。是否有一种方法可以在将图像附加到pdf时动态减小图像的大小 我试过这样的东西 注1:$imgPath从循环中传递,我正在使用MPDF生成pdf。如果您查看正在使用的简单图像代码,您将在resize方法中看到函数。这是由resizeToWidth方法依次调用的,因此您的图像正在被重新采样。这很有效,而且速度相当快 $img = imagecreatefromjpeg('image.jpg'); $originalW

我正在生成一个包含一些图像的pdf。由于图像的大小很大,生成的PDF的大小也会很大。是否有一种方法可以在将图像附加到pdf时动态减小图像的大小

我试过这样的东西


注1:$imgPath从循环中传递,我正在使用MPDF生成pdf。

如果您查看正在使用的简单图像代码,您将在
resize
方法中看到函数。这是由
resizeToWidth
方法依次调用的,因此您的图像正在被重新采样。

这很有效,而且速度相当快

$img = imagecreatefromjpeg('image.jpg');
$originalWidth  = imagesx($image);
$originalHeight = imagesy($image);
$scale      = min($previewWidth/$originalWidth, $previewHeight/$originalHeight);
$newWidth  = ceil($scale*$originalWidth);
$newHeight = ceil($scale*$originalHeight);
$newPic = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newPic, $image,0, 0, 0, 0,$newWidth, $newHeight, $originalWidth, $originalHeight);
ob_start();
imagejpeg($newPic, NULL, 70);
$jpg = ob_get_clean();
ob_clean();
$fp = fopen('newPic.jpg','w');
fwrite($fp,$jpg);
fclose($fp);

这是我的标题,这是我如何使用mpdf希望这将帮助你

$htmlTable_header='<style></style><table width="100%" border="0" style="border-collapse:collapse;margin-top:-40px;font-family:arial !important; font-size:11px;" cellspacing="0" cellpadding="0">
              <tr>
                <td width="10">&nbsp;</td>
                <td width="150" valign="top"><img src="http://localhost/abc/images/abc_logo.png" width="150" height="100" border="0" alt="" title="abc" />';


$mpdf->SetHTMLHeader($htmlTable_header);
$mpdf->SetHTMLFooter($htmlTable_footer);

$mpdf->WriteHTML($htmlTable_body);
$mpdf->SetDisplayMode('fullpage');

$mpdf->Output('service_report_pdf/'.$_REQUEST['id'].'.pdf',F);
$htmlTable\u header=>
';
$mpdf->SetHTMLHeader($htmlTable_头);
$mpdf->SetHTMLFooter($htmlTable\u footer);
$mpdf->WriteHTML($htmlTable\u body);
$mpdf->SetDisplayMode('fullpage');
$mpdf->Output('service\u report\u pdf/'.$\u REQUEST['id'..pdf',F);

谢谢你,狐狸飞。但这里我的问题是,当我生成pdf时,它显示“无法加载pdf”。在MPDF中,我在图像标签中输出图像,如$html;您需要确定错误是与图像创建有关,还是与pdf创建有关。运行创建pdf代码,但不包括图像-是否现在创建pdf?但看起来像是语法错误-您的连接不正确:
$html.='pdf正在生成。如果我调用$image->output(),则pdf不会生成并给出错误。现在我只使用像
$html.='Output().'>'这样的图像标记。我可以创建没有此图像标签的pdf。我猜在将图像附加到pdfRead时会出现一些错误。注意:“输出功能允许您直接将图像输出到浏览器,而无需保存文件。”因此我非常确定您需要保存图像,然后将保存的图像文件包含在pdf中。是的,我也想到了这个选项。但这只会占用我服务器上的空间,对吗?。不过,我也会尝试一下,非常感谢你们的支持。
$htmlTable_header='<style></style><table width="100%" border="0" style="border-collapse:collapse;margin-top:-40px;font-family:arial !important; font-size:11px;" cellspacing="0" cellpadding="0">
              <tr>
                <td width="10">&nbsp;</td>
                <td width="150" valign="top"><img src="http://localhost/abc/images/abc_logo.png" width="150" height="100" border="0" alt="" title="abc" />';


$mpdf->SetHTMLHeader($htmlTable_header);
$mpdf->SetHTMLFooter($htmlTable_footer);

$mpdf->WriteHTML($htmlTable_body);
$mpdf->SetDisplayMode('fullpage');

$mpdf->Output('service_report_pdf/'.$_REQUEST['id'].'.pdf',F);