PHP provides the ability to compress images without losing the quality of it. Even more, we can resize images in PHP to generate thumbnails and lightweight web images.
PHP has some default functions to compress images and to resize them. Many popular Core-PHP frameworks and CMS use default function to generate thumbnail and images without losing image quality.
Index
Here we will learn how to compress an image in PHP without losing its quality in just 2 easy steps.
First, we have to create a simple HTML form to upload an image file.
<!DOCTYPE html>
<html>
<head>
<title>How to compress an image without losing quality in PHP</title>
</head>
<body>
<form action='' method='POST' enctype='multipart/form-data'>
<input name="image_file" type="file" accept="image/*">
<button type="submit">SUBMIT</button>
</form>
</body>
</html>
Note: enctype=”multipart/form-data” is compulsory for image upload precess via post method in form.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$file_name = $_FILES["image_file"]["name"];
$file_type = $_FILES["image_file"]["type"];
$temp_name = $_FILES["image_file"]["tmp_name"];
$file_size = $_FILES["image_file"]["size"];
$error = $_FILES["image_file"]["error"];
if(!$temp_name){
echo "ERROR: Please browse for file before uploading";
exit();
}
function compress_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);
echo "Image uploaded successfully.";
}
if ($error > 0) {
echo $error;
}else if (($file_type == "image/gif") || ($file_type == "image/jpeg") || ($file_type == "image/png") || ($file_type == "image/pjpeg")) {
$filename = compress_image($temp_name, "uploads/".$file_name, 80);
}else {
echo "Uploaded image should be jpg or gif or png.";
}
}
?>
In the second step first, we checked for the form request method. For File and Image upload POST method is compulsory.
After that, we defined some variables related to the uploaded file. Like, File Name, File Type, File Size, Temporary Location of Uploaded File, and Error in File Upload Process, if any.
Next, we created a compress_image() function to upload and compress the image. In this function, we passed three variables of the uploaded file.
In the compress_image() function, we first found for image specifications using getimagesize() function and checked for its mime type.
From mime type, we checked the image is valid or not and create a related replica of the image in PHP according to jpeg, png, gif using imagecreatefromjpeg(), imagecreatefrompng(), imagecreatefromgif() respectively and saved it in $image variable.
Finally, we created a jpeg image with the help of a $image variable by using imagejpeg() function.
imagejpeg() function is basically image creator function from raw image data. There are three arguments we passed in imagejpeg() function.
In the end, we checked for error in the upload process and used our compress_image() function to handle the uploaded image’s compression.
Note: Change the destination directory according to your config. Here we have set the destination path to uploads/ in our working directory.
Read Also: How to Image Preview Before Upload Using JavaScript
In the above article, we learned a complete process of image upload and compression with necessary validations.
Happy Programming 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…