我可以用php更改图像文件大小吗


Can i change image file size with php?

我做了一个小功能来上传图像,然后在我的网站上显示它们。

我现在的问题是,我可以在上传图像的同时更改图像的文件大小吗?

以facebook为例。即使是大的图像也有大约65kb的大小。

如果是,我应该使用开关功能吗?

谢谢,

使用imagecopy重采样函数:

$source_image = imagecreatefromjpeg("youtimagejpg");
$src_w= imagesx($source_image);
$src_h= imagesy($source_image);
$dest_image = imagecreatetruecolor(100, 100); //targeted width and height
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, 100, 100, $source_w, $source_h);
imagejpeg($dest_image,NULL,80); 
//Replace null by the path if you want to save on the server, otherwise the image will be sent to the client intead.

如果您想在不调整图像大小的情况下缩小图像的大小;使用imagejpeg函数(具有低质量因子)

以下是更改图像大小的方法:

  1. 转换为其他图像格式
  2. 使用有损压缩保存图像(http://en.wikipedia.org/wiki/Lossy_compression)
  3. 对具有另一宽度的图像重新采样&高度(http://php.net/manual/en/function.imagecopyresampled.php)