RE: How to compress images in PHP?
How to compress images in PHP?
Thank you very much for your valuable question. Actually image competition is very important for SEO because if any user find that website is loading too slowly than that user will drop out or go away from the website.
Follow the steps to reduce or compress images in PHP
1) Create one file “index.php” and one folder named “uploads” in your server
First of all, you need to create one new file named “index.php”. After index.php file creation, create one folder named “uploads” in your server. A file named “index.php” is for executing the code and “uploads” folder is used for store compressed/reduced images.
2) PHP code integration
In this portion, I have created one function for image compression. The function is used to compress “jpeg/jpg, gif, png” images. The next part is to store the image in the “uploads” folder. The code snippet is given below.
<?php ini_set("error_reporting", 1); 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; } if (isset($_POST['submit'])) { 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")) { $destination_url = 'uploads/demo.jpg'; $source_img = $_FILES["file"]["tmp_name"]; // decrease the number (50) for more compression $fianl_file = compress($source_img, $destination_url, 50); $error = "Image Compressed successfully"; }else { $error = "Uploaded image should be jpg or gif or png"; } } ?>
3) HTML code integration
In this step, I have created one form to choose the image which user want to compress. Now, the user can choose the image and the image will be compressed without losing the quality. Refer the html code snippet below.
<form action="index.php" 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>
4) Full project code : Copy the below code and paste it in “index.php” file
The second step is just to copy the below code and paste it in “index.php” file and run in your browser.
<?php ini_set("error_reporting", 1); 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; } if (isset($_POST['submit'])) { 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")) { $destination_url = 'uploads/demo.jpg'; $source_img = $_FILES["file"]["tmp_name"]; $fianl_file = compress($source_img, $destination_url, 50); $error = "Image Compressed successfully"; }else { $error = "Uploaded image should be jpg or gif or png"; } } ?> <html> <head> <title>Php code compress the image</title> </head> <body> <fieldset class="well"> <legend>Upload Image:</legend> <form action="index.php" 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> <br><br><br> <center> <?php if($_POST){ if ($error) { ?> <h3><?php echo $error; ?></h3> <?php }} ?> </center> </body> </html>
I hope you will like this tutorial and enjoyed it. If you have any questions then ask me here.
Thanks