compress image size in PHP
Hello, friends. I have come to you with a new tutorial. This tutorial is a compress image size in PHP without losing image quality and color. It will be more helpful for you while image upload into the database with a directory.
Many websites want to upload less size image when user upload large size of an image. That time you need to compress image size then permission to use for upload image into the server.
You need to know something about Image compression. It is the process of encoding or converting an image file in such a way that it consumes less space than the original file. It is a type of compression technique that reduces the size of an image file without affecting or degrading its quality. Follow the simple step to compress image size using php.
Step 1
- First of all create a .php file. Like image_upload.php.
- Define an uploading form or image upload text file in your php file.
- Create a folder in your project, where you store uploaded image. I already create a folder give folder name is “images”.
Step 2
Use below example code for compress image size.
$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);
I will use this code inside a function name that function I will define first in the image uploading time. For better understanding, you need to follow the full code.
HTML CODE
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <div class="container"> <h2>Compress Image Size While Uploading in PHP</h2> <form method="post" name="upload_form" id="upload_form" enctype="multipart/form-data"> <label>Choose Images to Upload</label> <input type="file" name="image_file" id="image_file"> <input type="submit" name="submit"> </form> </div> </body> </html>
PHP CODE
<?php function cmp_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); } if(isset($_POST["submit"])){ $file_type_error = ''; if($_FILES['image_file']['name']) { $upload_dir = "images/"; $up_file_type=$_FILES["image_file"]["type"]; if (($up_file_type == "image/gif") || ($up_file_type == "image/jpeg") || ($up_file_type == "image/png") || ($up_file_type == "image/pjpeg")) { $file_name = $_FILES["image_file"]["name"]; $tmp = explode('.', $file_name); $extension = end($tmp); $upload_file = $upload_dir.$file_name; if(move_uploaded_file($_FILES['image_file']['tmp_name'],$upload_file)){ $source_image = $upload_file; $image_destination = $upload_dir.$file_name; $compress_images = cmp_image($source_image, $image_destination, 80); die; } } else { $file_type_error = "Upload only jpg or gif or png file type"; } } } ?>
Now run the code and you will get the optimized image in the folder. Just check the size of this image. You can decrease the quality of this image but keep it in 90 or 80. If you have another doubt please put a comment in the bellow.
Click Here: To Get Code of Image Crop and upload in PHP