简体   繁体   English

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

[英]Can i change image file size with php?

I made a small function to upload images then show them on my website. 我做了一个小功能上传图像然后在我的网站上显示它们。

My question is now, can I change the file size of a image, on same time as I upload it? 我现在的问题是,我可以在上传的同时更改图像的文件大小吗?

Take facebook as example. 以facebook为例。 Even large images has a size on like 65kb. 即使是大型图像的大小也只有65kb。

If so, witch function should I use? 如果是这样,我应该使用女巫功能吗?

Thanks, 谢谢,

use the imagecopyresampled function: 使用imagecopyresampled函数:

$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.

If you want to reduce the size of the image without resizing it; 如果你想减小图像的大小而不调整它的大小; use the imagejpeg function (with a low quality factor) 使用imagejpeg函数(质量因子较低)

There are next ways to change image size: 有以下方法可以更改图像大小:

  1. Convert to another image format. 转换为其他图像格式。
  2. Save image with lossy compression ( http://en.wikipedia.org/wiki/Lossy_compression ) 使用有损压缩保存图像( http://en.wikipedia.org/wiki/Lossy_compression
  3. Resample image with another width & height ( http://php.net/manual/en/function.imagecopyresampled.php ) 重新采样另一个宽度和高度的图像( http://php.net/manual/en/function.imagecopyresampled.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM