php - 调整图像大小而不保存

标签 php

我正在尝试使用 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/

相关文章:

php - php中的回调函数和异常

PHP 5.3 中的 simplexml_load_file 错误

php - 为 mysql 数据库创建新用户并仅授予某些权限

php - 我可以使用 WGET 生成给定 URL 的网站站点地图吗?

php - 值(value)比较未按预期进行

javascript - 将 json 传递给 Vue 组件并循环

php - 用户 'root' @'localhost' 的访问被拒绝(使用密码 : YES) using ssh tunnel connection

Javascript 生成的提交按钮无法在 Firefox 中发布

javascript - strip 结帐后页面未重定向

php - 使用 PHP 发送的邮件花费太多时间