PHP - How to compress PNG image using pngquant?
Today, In this tutorial, we will learn how to reduce png image size while uploading in core PHP. You can simply use this script in other PHP framework like Laravel, codeigniter, cake PHP etc.
compress file size is very important for our server. If you have 4MB image then you have take large memory space to store and another thing it take time to load when you display on web page or application. But If you reduce image size then it more better for it take lass memory space and it take lass time to load image or file on web. Here we are going to use "pngquant" command-line utility and library for compress image. pngquant will help to compress png image.
So, you have to just follow three step and get full example of compress png image using pngquant library.
Step 1: Install pngquant
In first step, we will install "pngquant" in your system. We must install "pngquant" command-line utility and library that way we can use their command. So if you are using ubuntu server then you can install by following command:
sudo apt-get install pngquant
If you are using windows or mac then you can found installer from here : pngquant.org.
Step 2: Create index.php
Here we will create index.php file on your root directory. In this file we will add form with input file and submit button. So let's put bellow code on index.php file.
index.php
<!DOCTYPE html>
<html>
<head>
<title>PHP - PNG Image Compress Example</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="image" >
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Step 3: Create upload.php
In last step we will create upload.php file on root directory. In this file we write code for compress png image using "pngquant" library. So let's create upload.php file and put bellow code in that file.
upload.php
<?php
if(!empty($_FILES["image"])){
$imageName = time().'.png';
$readFromPath = $_FILES["image"]["tmp_name"];
$saveToPath = 'uploads/'.$imageName;
$compressedPngContent = compress_png($readFromPath);
file_put_contents($saveToPath, $compressedPngContent);
print_r('File is saved as '.$imageName.' , please check on uploads folder.');
}else{
print_r('Image is empty');
}
function compress_png($pathToPngFile, $maxQuality = 90)
{
if (!file_exists($pathToPngFile)) {
throw new \Exception("File does not exist: $pathToPngFile");
}
$min_quality = 60;
$compressedPngContent = shell_exec("pngquant --quality=$min_quality-$maxQuality - < ".escapeshellarg( $pathToPngFile));
if (!$compressedPngContent) {
throw new \Exception("Conversion to compressed PNG failed. Is pngquant 1.8+ installed on the server?");
}
return $compressedPngContent;
}
?>
Ok Now we are ready to run Above example, But We have to create "uploads" folder on your root directory before run example. all images will upload on "uploads" directory so make sure permission too.
So let's run bellow command on your root directory for quick run:
php -S localhost:8000
Now you can open bellow URL on your browser:
http://localhost:8000/
Make sure uploaded image should be 'png'.
GOOD LUCK !!
compress file size is very important for our server. If you have 4MB image then you have take large memory space to store and another thing it take time to load when you display on web page or application. But If you reduce image size then it more better for it take lass memory space and it take lass time to load image or file on web. Here we are going to use "pngquant" command-line utility and library for compress image. pngquant will help to compress png image.
So, you have to just follow three step and get full example of compress png image using pngquant library.
Step 1: Install pngquant
In first step, we will install "pngquant" in your system. We must install "pngquant" command-line utility and library that way we can use their command. So if you are using ubuntu server then you can install by following command:
sudo apt-get install pngquant
If you are using windows or mac then you can found installer from here : pngquant.org.
Step 2: Create index.php
Here we will create index.php file on your root directory. In this file we will add form with input file and submit button. So let's put bellow code on index.php file.
index.php
<!DOCTYPE html>
<html>
<head>
<title>PHP - PNG Image Compress Example</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="image" >
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Step 3: Create upload.php
In last step we will create upload.php file on root directory. In this file we write code for compress png image using "pngquant" library. So let's create upload.php file and put bellow code in that file.
upload.php
<?php
if(!empty($_FILES["image"])){
$imageName = time().'.png';
$readFromPath = $_FILES["image"]["tmp_name"];
$saveToPath = 'uploads/'.$imageName;
$compressedPngContent = compress_png($readFromPath);
file_put_contents($saveToPath, $compressedPngContent);
print_r('File is saved as '.$imageName.' , please check on uploads folder.');
}else{
print_r('Image is empty');
}
function compress_png($pathToPngFile, $maxQuality = 90)
{
if (!file_exists($pathToPngFile)) {
throw new \Exception("File does not exist: $pathToPngFile");
}
$min_quality = 60;
$compressedPngContent = shell_exec("pngquant --quality=$min_quality-$maxQuality - < ".escapeshellarg( $pathToPngFile));
if (!$compressedPngContent) {
throw new \Exception("Conversion to compressed PNG failed. Is pngquant 1.8+ installed on the server?");
}
return $compressedPngContent;
}
?>
Ok Now we are ready to run Above example, But We have to create "uploads" folder on your root directory before run example. all images will upload on "uploads" directory so make sure permission too.
So let's run bellow command on your root directory for quick run:
php -S localhost:8000
Now you can open bellow URL on your browser:
http://localhost:8000/
Make sure uploaded image should be 'png'.
GOOD LUCK !!
No comments:
Post a Comment