我正在尝试从外部 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 . '">';
catspeake