PHP Classes

PHP Image Compressor on Upload: Compress uploaded image file reducing its quality

Recommend this page to a friend!
  Info   View files Example   View files View files (13)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2023-06-26 (8 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 84 All time: 10,000 This week: 100Up
Version License PHP version Categories
php-image-compressor 1.0.0The PHP License5PHP 5, Graphics, Compression
Description 

Author

This package can compress uploaded image files reducing their quality.

It provides functions that can take an uploaded image and generate another shot with a lower quality factor to make it a smaller file size.

The compressed image file can be served as the current script output or stored in a server-side file.

Picture of Adeleye Ayodeji
  Performance   Level  
Name: Adeleye Ayodeji is available for providing paid consulting. Contact Adeleye Ayodeji .
Classes: 16 packages by
Country: Nigeria Nigeria
Age: ???
All time rank: 309121 in Nigeria Nigeria
Week rank: 367 Up13 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 11x

Example

<?php
   
//Connection to base
       
$con = mysqli_connect("localhost", "root", "", "compress") or die();

       
//For live server
        //$con = mysqli_connect("localhost", "betttiie_compress", "compress", "betttiie_compress") or die();


       
$query2 = $con->query("SELECT * FROM image ORDER BY id DESC") or die();

        if(isset(
$_POST['upload'])){
           
// Getting file name
           
$filename = $_FILES['imagefile']['name'];
           
// Valid extension
           
$valid_ext = array('png','jpeg','jpg');
           
// Location
           
$location = "images/".$filename;
           
// file extension
           
$file_extension = pathinfo($location, PATHINFO_EXTENSION);
           
$file_extension = strtolower($file_extension);
           
// Check extension
           
if(in_array($file_extension,$valid_ext)){
               
// Compress Image
               
if(compressImage($_FILES['imagefile']['tmp_name'],$location,20)){
                   
// Do nothing;
               
}else{
                   
//Send data to base
                    
$query = $con->query("INSERT INTO image(name) VALUES('$filename')");
                     if (
$query) {
                         echo(
"Image Posted and Compressed");
                     }
                };

                }else{
                    echo
"Invalid file type.";
                }
            }

       
// Compress image
       
function compressImage($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);

        }

        function
imagemb($url)
        {
           
//$url = "https://result.maxfemcollege.com.ng/images/IMG_20200120_133610_6.jpg";
           
$image = get_headers($url, 1);
           
$bytes = $image["Content-Length"];
           
$mb = $bytes/(1024 * 1024);
            echo
number_format($mb,2) . " MB";
        }


       
//Compress on the go
       
function compressImageonthego($source, $quality) {
           
$info = getimagesize($source);
           
$extension = explode(".",$source);

           
$newname = "temp".rand(10,100);

            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, "images/".$newname.".".$extension[1] ,$quality);

          echo
"<b>".$newname.".".$extension[1]."</b>";

        }

   
?>

<!doctype html>
<html>
    <head>
        <style type="text/css">
            .image{
                margin-left: auto;
    margin-right: auto;
    width: 50%;
    text-align: center;
    background: lightgrey;
    padding: 20px;
    border: 5px solid gainsboro;
            }

            .image input[type="submit"]{
                width: 100%;
    margin: 9px;
    background: grey;
    padding: 12px;
    outline: none;
    border: none;
    color: white;
            }
            .image input[type="file"]{
                    width: 50%;
    margin: 10px;
    background: grey;
    color: white;
            }

            img {
    border: 5px solid white;
    box-sizing: border-box;
    box-shadow: 0px 3px 8px 2px grey;
    margin-bottom: 10px;
}
        </style>
    </head>
    <body>
        <h2 align="center">PHP Image Compressor</h2>
        <div class="image">
        <?php
       
//Fetching image from base
       
while ($qresult = mysqli_fetch_assoc($query2)) {
           
?>
<img height="150" src="images/<?php echo($qresult['name']); ?>">
                <br>
            <?php
       
}
       
?>
<!-- Upload form -->
        <form method='post' action='' enctype='multipart/form-data'>
            <input type='file' name='imagefile' >
            <input type='submit' value='Compress' name='upload'>
        </form>
        </div>
        <script type="text/javascript">
           console.log("<?php
          
echo('Hello world')
      
?>") ;
        </script>
    </body>
</html>


Details

php-image-compressor-on-upload

Easily compress image before uploading your images to the server. Also support compressing image on the go without uploading a file


  Files folder image Files  
File Role Description
Files folder imageimages (6 files)
Files folder imageloader (1 file)
Accessible without login Plain text file ajaxcompress.php Aux. Auxiliary script
Accessible without login Plain text file image.sql Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file onthego.php Aux. Auxiliary script
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  images  
File Role Description
  Accessible without login Image file IMG_20190625_085804.jpg Data Auxiliary data
  Accessible without login Image file IMG_20191212_084409.jpg Data Auxiliary data
  Accessible without login Image file IMG_20200106_115938_506.jpg Data Auxiliary data
  Accessible without login Image file IMG_20200117_023557_494.jpg Data Auxiliary data
  Accessible without login Image file IMG_20200120_133610_6.jpg Data Auxiliary data
  Accessible without login Image file IMG_20200121_033008_943.jpg Data Auxiliary data

  Files folder image Files  /  loader  
File Role Description
  Accessible without login Image file loader.gif Icon Icon image

 Version Control Unique User Downloads Download Rankings  
 100%
Total:84
This week:0
All time:10,000
This week:100Up