我正在尝试使用 PHP 从 url 获取 jpg 图像并调整其大小,然后将其显示在网页中,而无需保存新的调整大小的图像。 我的问题是,当我使用此脚本测试页面时,图像没有显示在页面上,只是浏览器中显示一个小的空白方 block 。
<?php
$source_image = imagecreatefromjpeg("https://www.******.com/blog/post/b/l/health.jpg");
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = 300;
$dest_imagey = 200;
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0,
$dest_imagex,
$dest_imagey, $source_imagex, $source_imagey);
header("Content-Type: image/jpeg");
imagejpeg($dest_image,NULL,80);
?>
<img src='<?php echo "$dest_image";?>'>
最佳答案
首先确保您的图片网址正确。假设您有一些 resize.php
用于调整图像大小。
resize.php
<?php
$source_image = imagecreatefromjpeg("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSKTUtEWtyngG0EVhrOOZqJYhVPoFg5rzma6Xgn6Sy-RQCDCT950g");
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = 100;
$dest_imagey = 100;
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0,
$dest_imagex,
$dest_imagey, $source_imagex, $source_imagey);
header("Content-Type: image/jpeg");
imagejpeg($dest_image,NULL,80);
?>
这里我刚刚用我确信存在的地址更改了 URL
然后在一些html
中使用它:
<img src = 'resize.php' />
关于php - 调整图像大小而不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48527959/