gpt4 book ai didi

php - 保存使用 PHP 调整大小的图像

转载 作者:可可西里 更新时间:2023-11-01 00:01:25 24 4
gpt4 key购买 nike

我正在努力改进我的 Facebook 应用程序。我需要能够调整图像大小,然后将其保存到服务器上的目录中。这是我必须调整大小的代码:

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// 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
imagejpeg($image_p, null, 100);
?>

我的问题是,如何保存这个调整大小的图像?我需要吗?有没有一种方法可以在不保存图像的情况下处理调整大小的图像

最佳答案

根据manual on imagejpeg() ,可选的第二个参数可以指定一个文件名,它将被写入。

Filename

The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.

To skip this argument in order to provide the quality parameter, use NULL.

将结果写入磁盘以进行一些基本缓存通常是个好主意,这样并不是每个传入请求都会导致(资源密集型)GD 调用。

关于php - 保存使用 PHP 调整大小的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3950441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com