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

标签 php image resize save editing

我正在努力改进我的 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/

相关文章:

php - 数据库字符串的本地解码和生产解码之间的差异

php - Imagick 构造函数使 PHP/CGI 崩溃

java - 在 JFrame 中将图像绘制到 JPanel

python - 如何使用 Python/OpenCV 从医学图像中分割单个细胞?

jquery - 调整窗口大小时保持表格纵横比

php - mysql一段时间内每天的平均温度

php - 检查默认的用户选项

java - 如何创建大型 SWT 图像?

java - 调整大小时强制 JComponent 为正方形

android-layout - 如何在android中调整PIP模式的大小