简体   繁体   中英

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. Even large images has a size on like 65kb.

If so, witch function should I use?

Thanks,

use the imagecopyresampled function:

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

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 )
  3. Resample image with another width & height ( http://php.net/manual/en/function.imagecopyresampled.php )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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