How to Reduce or Compress Image File Size while Uploading Using PHP code?

It is a quite common process to upload images on the websites dynamically. However, while you upload the image with large file size in a website, it is likely to take more time to load. This necessitates the developer to write down the code to decrease the file size of image while dynamically uploading the image to the website. With the help of PHP coding, it is possible to reduce the size of files of uploaded images when they are loading. Definitely, you would have to compromise with the image quality i.e. is the file size has to be reduced.
Below is the Code to reduce file size for the image:
<?php function compress($source, $destination, $quality) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source); imagejpeg($image, $destination, $quality); return $destination; } $source_img = 'source.jpg'; $destination_img = 'destination .jpg'; $d = compress($source_img, $destination_img, 90); ?>
$d = compress($source_img, $destination_img, 90);
PHP function is used in the code that will pass the source image, destination image and quality for the image that takes to compress.
$info = getimagesize($source);
The getimagesize() function is also used to know the size of given image file and to return the dimensions including the file type.
Example:
$info = getimagesize($source); print_r($info);
Output:
Array ( [0] => 1280 [1] => 768 [2] => 2 [3] => width="1280" height="768" [bits] => 8 [channels] => 3 [mime] => image/jpeg )
$image = imagecreatefromjpeg($source); $image = imagecreatefromgif($source); $image = imagecreatefrompng($source);
All the functions used in the coding are intended to make a new image from the given URL or file. Those functions are meant for returning an image identifier indicating the image acquired from the given file name.
imagejpeg($image, $destination, $quality);
The imagejpeg() function is added in the code to make a JPEG file from the provided image.
Syntax: imagejpeg ( $source_image, $destination_image, $quality )
Quality ($quality): Quality is optional that range from 0 representing the worst quality and smaller file to hundred representing the best quality and biggest file. The range75 is considered as the default.
Note:
The GD library is utilized for creating dynamic image. GD library is used to create JPG, PNG or GIF. This is more essential to run every image creation function in PHP language. When your server is not compatible with GD library, then the complete functionality associated with the image creation won’t work out.
Here is the Complete code to reduce the image file size using PHP:
<?php $name = ''; $type = ''; $size = ''; $error = ''; function compress_image($source_url, $destination_url, $quality) { $info = getimagesize($source_url); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url); imagejpeg($image, $destination_url, $quality); return $destination_url; } if ($_POST) { if ($_FILES["file"]["error"] > 0) { $error = $_FILES["file"]["error"]; } else if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) { $url = 'C:/Users/admin/Downloads/compressed.jpg'; $filename = compress_image($_FILES["file"]["tmp_name"], $url, 80); }else { $error = "Uploaded image should be jpg or gif or png"; } } ?> <html> <head> <title>Php code compress the image</title> </head> <body> <div class="error"> <?php if($_POST){ if ($error) { ?> <label class="error"><?php echo $error; ?></label> <?php } } ?> </div> <fieldset class="well"> <legend>Upload Image:</legend> <form action="" name="img_compress" id="img_compress" method="post" enctype="multipart/form-data"> <ul> <li> <label>Upload:</label> <input type="file" name="file" id="file"/> </li> <li> <input type="submit" name="submit" id="submit" class="submit btn-success"/> </li> </ul> </form> </fieldset> </body> </html>
The PHP code is beneficial in decreasing the file size of image, while uploading it to save your valuable time.
its Woo PHP image code
I have tried with ‘imagewebp();’
I have error while using this,
Fatal error: Uncaught Error: Call to undefined function imagewebp().
I have XAMPP for Linux 7.0.8*
And enabled with GD Support.
I want to enable webp support.
What I want to do?
Can you help me out
found any interesting article like yours. It’s pretty price sufficient for me.
In my opinion, if all webmasters and bloggers made excellent content as you
did, the net will likely be a lot more helpful than ever before.
stumbleupon every day. It’s always helpful to
read articles from other authors and use a
little something from other websites.
And i am glad reading your article on how to reduce image file size. However should
statement on few normal issues, The website style is
perfect, the articles is truly nice : D. Good activity,
cheers
on our site. Maintain the great writing.
Continue the excellent writing.
subject? I’d be very grateful if you could elaborate a little bit
more. Bless you!
Thanks
Any responses would be greatly appreciated.
how to acheive this with multiple image upload in php. i just need to compress image and move to uploaded folder