为了账号安全,请及时绑定邮箱和手机立即绑定

无法使用 PHP 在 PDF 中显示调整大小的图像

无法使用 PHP 在 PDF 中显示调整大小的图像

PHP
炎炎设计 2022-05-27 13:19:45
我正在尝试从外部 URL 加载图像,然后调整其大小并以 PDF 格式显示。我现在正在尝试使用单个图像来实现它,但整个功能将在一个foreach循环中处理大量非常大的图像。首先,我调整了图像的大小,然后获取图像的内容,应用 base65 编码,从中构建一个源字符串并将该字符串添加到我的 img src 标签中。这是我的代码 -    $filename = 'https://jooinn.com/images/nature-319.jpg'; // URL of the image    $percent = 0.25; // percentage of resize    // Content type    header('Content-type: image/jpeg');    // Get new dimensions    list($width, $height) = getimagesize($filename);    $new_width = $width * $percent;    $new_height = $height * $percent;    // Resample    $image_p = imagecreatetruecolor($new_width, $new_height);    $image = imagecreatefromjpeg($filename);    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);    // Output    $imageData = base64_encode(file_get_contents($image_p));    // Format the image SRC:  data:{mime};base64,{data};    $src = 'data: '.mime_content_type($image_p).';base64,'.$imageData;    // Echo out a sample image    echo '<img src="' . $src . '">';    imagedestroy($image_p);我认为问题出在这条线上$imageData = base64_encode(file_get_contents($image_p));,我做错了。它可以很好地与 URL 一起使用,但我怎样才能使它适用于调整大小的图像呢?例如,只要我不使用调整大小的图像,以下内容就可以完美地工作 -    $filename = 'https://jooinn.com/images/nature-319.jpg'; // URL of the image    // Output    $imageData = base64_encode(file_get_contents($filename));    // Format the image SRC:  data:{mime};base64,{data};    $src = 'data: '.mime_content_type($filename).';base64,'.$imageData;    // Echo out a sample image    echo '<img src="' . $src . '">';
查看完整描述

1 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

确实,正如您所说,代码中的以下行是错误的:


$imageData = base64_encode(file_get_contents($image_p));

该$image_p变量不是文件名,而是由 imagecreatetruecolor 创建的资源。您首先必须使用 将其转换为 jpeg 文件imagejpeg()。您可以避免在使用ob_*xxx*函数编码为 base64 之前保存中间文件


ob_start();

imagejpeg($image_p);

$imageContent = ob_get_contents();

$imageData = base64_encode($imageContent);

ob_end_clean();

这行也有问题,同样$image_p不是文件名:


$src = 'data: '.mime_content_type($image_p).';base64,'.$imageData;

在创建 jpeg 文件时,只需将其替换为:


$src = 'data: image/jpeg;base64,'.$imageData;

为方便起见,这里是完整的工作脚本:


$filename = 'https://jooinn.com/images/nature-319.jpg'; // URL of the image

$percent = 0.25; // percentage of resize


// Content type

header('Content-type: image/jpeg');


// Get new dimensions

list($width, $height) = getimagesize($filename);

$new_width = $width * $percent;

$new_height = $height * $percent;


// Resample

$image_p = imagecreatetruecolor($new_width, $new_height);

$image = imagecreatefromjpeg($filename);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);


// Output

ob_start();

imagejpeg($image_p);

$imageContent = ob_get_contents();

$imageData = base64_encode($imageContent);

ob_end_clean();


// Format the image SRC:  data:{mime};base64,{data};

$src = 'data: image/jpeg;base64,'.$imageData;


// Echo out a sample image

echo '<img src="' . $src . '">';

imagedestroy($image_p);


查看完整回答
反对 回复 2022-05-27
  • 1 回答
  • 0 关注
  • 150 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号