保存使用 PHP 调整大小的图像
我正在努力改进我的 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);
?>
我的问题是,如何保存这个调整大小的图像?我需要吗?有没有办法操作调整大小的图像而不保存它?
I am working on improving my Facebook app. I need to be able to resize an image, then save it to a directory on the server. This is the code I have to resize:
<?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);
?>
My question is, how would I save this resized image? Would I need to? Is there a way to manipulate the resized image without saving it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据imagejpeg()手册,可选的第二个参数可以指定一个文件名,它将是写入.
通常最好将结果写入磁盘以进行一些基本缓存,这样并非每个传入请求都会导致(资源密集型)GD 调用。
According to the manual on imagejpeg(), the optional second parameter can specify a file name, which it will be written into.
It's usually a good idea to write the results to disk for some basic caching, so that not every incoming request leads to a (resource intensive) GD call.
应该是工作
it should be work
http://www.php.net/manual/en/function.imagecopyresampled .php#90038
http://www.php.net/manual/en/function.imagecopyresampled.php#90038