free

Image Compressor Free Online Tool


In this Blog, we will make an Image Compressor Free Online Tool with the help of PHP, which will be of great use to us, with the help of this we can reduce the size of the image.

The size of the image is very valid, the smaller the size, the faster your website will load, this will increase your website speed and website rank will also come forward, so you can compress all the images and upload them to your website.

What does www.myprograming.com do?

https://myprograming.in/live/image-compressor/ Uses smart lossy compression techniques to reduce the file size of your WEBP, JPEG, and PNG files. By selectively reducing the number of colors in the image, fewer bytes are required to store the data. The effect is almost invisible but it makes a huge difference in file size! And don’t let the quality of your image decrease.

Click here for Image Compressor

Download Image Compressor PHP Source Code

To make an image compressor tool, we need to know PHP, first, we have to create an index.php file and create an uploading folder, which will store the image which will be compressed and with the help of PHP you have to write a query for auto delete below The source code for compressing images is Thank you.

Step1:- Create index.php File.

<form action="index.php" method="post" enctype="multipart/form-data">
    <label>Select Image File:</label><br>
    <input class="addimg pointer" type="file" required="" name="image"> <br>
    <input type="submit" name="submit" class="upload" value="Upload">
</form>


<?php 
 
/* 
 * Custom function to compress image size and 
 * upload to the server using PHP 
 */ 
function compressImage($source, $destination, $quality) { 
    // Get image info 
    $imgInfo = getimagesize($source); 
    $mime = $imgInfo['mime']; 
     
    // Create a new image from file 
    switch($mime){ 
        case 'image/jpeg': 
            $image = imagecreatefromjpeg($source); 
            break; 
        case 'image/png': 
            $image = imagecreatefrompng($source); 
            break; 
        case 'image/gif': 
            $image = imagecreatefromgif($source); 
            break; 
        default: 
            $image = imagecreatefromjpeg($source); 
    } 
     
    // Save image 
    imagejpeg($image, $destination, $quality); 
     
    // Return compressed image 
    return $destination; 
} 
 
 
// File upload path 
$uploadPath = "uploads/"; 
 
// If file upload form is submitted 
$status = $statusMsg = ''; 
if(isset($_POST["submit"])){ 
    $status = 'error'; 
     echo "<img src='loading-gif.gif'>";
    if(!empty($_FILES["image"]["name"])) { 
        // File info 
        $fileName = basename($_FILES["image"]["name"]); 
        $imageUploadPath = $uploadPath . $fileName; 
        $fileType = pathinfo($imageUploadPath, PATHINFO_EXTENSION); 
         
        // Allow certain file formats 
        $allowTypes = array('jpg','png','jpeg','gif'); 
        if(in_array($fileType, $allowTypes)){ 
            // Image temp source 
            $imageTemp = $_FILES["image"]["tmp_name"]; 
                    
            // Compress size and upload image 
            $compressedImage = compressImage($imageTemp, $imageUploadPath, 75); 
             

            if($compressedImage){ 
                $status = 'success';  
                $statusMsg = " Image compress success"; 
            }else{ 
                $statusMsg = "Image compress failed!"; 
            } 
        }else{ 
            $statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.'; 
        } 
    }else{ 
        $statusMsg = 'Please select an image file to upload.'; 
    } 
} 
 
// Display status message 
echo $statusMsg; 
 
 
?>

Step2:- Create a Folder named upload on the index.php File.


Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *