PHP allows you to compress images without sacrificing quality. There are some built-in functions for compressing and resizing images in PHP.
Many popular Core-PHP frameworks and CMSs use the default function to generate thumbnails and images without sacrificing image quality.
When uploading an image to a website, the user usually does not optimize it. In this case, our app should be able to optimize the image before uploading it to the server.
This tutorial will learn how to compress images using the PHP function. This script is used in PHP to optimize image uploads. Image compression can be easily implemented using PHP functions.
Read more: How to center a DIV in CSS, Bootstrap, and Tailwindcss
<form method='post' action='' enctype='multipart/form-data'>
<input type='file' name='image_name' >
<input type='submit' value='Upload' name='upload'>
</form>
<?php
if(isset($_POST['upload'])){
$imagename = $_FILES['image_name']['name'];
$allowed_extension = array('png','jpeg','jpg');
$path = "uploads/".$imagename ;
$extension = pathinfo($location, PATHINFO_EXTENSION);
if(in_array($allowed_extension,$extension)){
//Compress Image
compressImage($_FILES['image_name']['tmp_name'],$path ,70);
}else{
echo "Invalid image type.";
}
}
// Compress image function
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);
}
?>
We checked the image’s validity based on the mime type and created a related duplicate of the image in PHP using imagecreatefromjpeg()
, imagecreatefrompng()
, and imagecreatefromgif()
, and saved it in the $image variable.
Finally, we used the imagejpeg()
function to create a jpeg image with the help of a $image
variable.
The imagejpeg()
is essentially an image creator function that works with raw image data. We passed three arguments to the imagejpeg()
function.
imagejpeg($image, $destination, $quality);
- $image: Raw image data we created and stored in
$image
- $destination: The path where we want to create our new image
- $quality: The image quality we want to set
References
You can read more about these functions on the official site of PHP.
PHP
Details: https://www.php.net/manual/en/function.imagecreatefromgifimagecreatefromgif
()
The PHP
Details: https://www.php.net/manual/en/function.imagecreatefrompngimagecreatefrompng
()
The PHP imagecreatefromjpeg()
Details: https://www.php.net/manual/en/function.imagecreatefromjpeg.php
PHP
Details: https://www.php.net/manual/en/function.imagejpeg.phpimagejpeg
()
Conclusion
we learned about the image upload and compression process, including all necessary validations.
Image compression helps to improve website loading speed. As a result, we can use the above technique to compress an image before uploading it.
Add comment