PHP 显示缩小并调整大小的图像

发布于 2024-10-14 20:11:08 字数 130 浏览 4 评论 0原文

有没有一种方法可以在服务器上显示缩小并调整大小的图像而不保存缩小的图像?就像如果图像是 500x500 250KB,我希望它在用户浏览器中显示 50x50 45kb。

如果没有办法,我如何临时创建一个文件夹并在粘贴一段时间后删除?

Is there a way with to display an image on a server reduced and resized without saving an reduced one? Like if an image was 500x500 250KB, I want it to display 50x50 45kb in a user's browser.

If there isn't a way, how can I temporarily create one to go to a folder and delete later after sometime has pasted?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放我走吧 2024-10-21 20:11:08
header("Content-type: image/jpeg");
$image = imagecreatefromjpeg($url);
$thumbImage = imagecreatetruecolor(50, 50);
imagecopyresized($thumbImage, $image, 0, 0, 0, 0, 50, 50, 500, 500);
imagejpeg($thumbImage,"",85);
imagedestroy($image);
imagedestroy($thumbImage);

但是您应该以某种方式缓存结果以降低服务器上的负载。

header("Content-type: image/jpeg");
$image = imagecreatefromjpeg($url);
$thumbImage = imagecreatetruecolor(50, 50);
imagecopyresized($thumbImage, $image, 0, 0, 0, 0, 50, 50, 500, 500);
imagejpeg($thumbImage,"",85);
imagedestroy($image);
imagedestroy($thumbImage);

But you should cache the result somehow to lower the load on the server.

你与清晨阳光 2024-10-21 20:11:08

我强烈建议您在上传时存储较小的版本并保留原始版本,而不是尝试缩小大图像的大小。使用 S3 等云托管,您可以以低廉的成本完成此任务。

最合乎逻辑的过程是执行一次昂贵的操作(缩放照片)而不是多次。

在回答您的其他问题时,您可以使用 GD 或 imagemagick 来缩小图像。有数百个关于如何执行此操作的教程。如果你想走这条路,我会谷歌一个教程并尝试一下。

I would strongly suggest that rather than trying to resize down a large image that you at upload time store a smaller version and keep the original version. Using cloud hosting like S3 you can do this cheaply.

The most logical process is to perform the expensive operation (scaling the photo) once rather than multiple times.

In answer to your other question you can use GD or imagemagick to scale down an image. There are hundreds of tutorials on how to do this. If you want to go down this route I would Google a tutorial and have a go.

哎呦我呸! 2024-10-21 20:11:08

听起来您想要做的就是设置 HTML 图像标记的 heightwidth 属性。

<img src='blah' height=50 width=50 />

也可以使用百分比。

CSS 允许更多的控制,并且可以应用于所有图像(或某个类的所有图像)。有了它,您可以指定 max-heightmax-width 这不会影响较小的图像。下面将适用于具有类 resizedimages 的所有图像

.resizedimages {max-height: 400px; max-width: 400px}

如果您对保存调整大小的图像不感兴趣,那么这听起来完全是客户端问题。 PHP 与它无关(除了可能渲染必要的 HTML)

注意:如果用户右键单击并保存在图像上,它仍然是完整的 250kB。这些纯粹是显示问题。如果这方面对您很重要,那么除了保存缩小副本之外,我不知道有什么方法。

这些可能不是最好的资源,只是谷歌排名靠前的一些资源。

http://www.w3schools.com/tags/tag_img.asp
http://www.fastechws.com/tricks/web/image -最大宽度高度.php

It sounds like all you want to do is set the height and width attributes of the HTML image tag.

<img src='blah' height=50 width=50 />

percentages can be used also.

CSS allows for a little more control, and can be applied to all images (or all images of a certain class). With it you can specifcy max-height and max-width which will not effect smaller images. Below will apply to all images with class resizedimages

.resizedimages {max-height: 400px; max-width: 400px}

If you are not interested in saving a resized image, then this sounds like entirely a client side issue. PHP has nothing to do with it (except perhaps rendering the necessary HTML)

NOTE: If the user goes to right click and save on the image it will still be the full 250kB. These are purely display issues. If this aspect is important to you then I don't know any way other than saving a reduced copy.

These probably arent the best resources, just a few that came up near the top of google.

http://www.w3schools.com/tags/tag_img.asp
http://www.fastechws.com/tricks/web/image-max-width-height.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文