How to compress images in PHP?
Thank you very much for your valuable question. Actually image competition is very important for SEO because if any user find that website is loading too slowly than that user will drop out or go away from the website.
Follow the steps to reduce or compress images in PHP
1) Create one file âindex.phpâ and one folder named âuploadsâ in your server
First of all, you need to create one new file named âindex.phpâ. After index.php file creation, create one folder named âuploadsâ in your server. A file named âindex.phpâ is for executing the code and âuploadsâ folder is used for store compressed/reduced images.
2) PHP code integration
In this portion, I have created one function for image compression. The function is used to compress âjpeg/jpg, gif, pngâ images. The next part is to store the image in the âuploadsâ folder. The code snippet is given below.
[code]
<?php
ini_set("error_reporting", 1);
function compress($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);
return $destination;
}
if (isset($_POST['submit'])) {
if ($_FILES["file"]["error"] > 0)
{
$error = $_FILES["file"]["error"];
}
else if (($_FILES["file"]["type"] == "image/gif") ||
($_FILES["file"]["type"] == "image/jpeg") ||
($_FILES["file"]["type"] == "image/png") ||
($_FILES["file"]["type"] == "image/pjpeg"))
{
$destination_url = 'uploads/demo.jpg';
$source_img = $_FILES["file"]["tmp_name"];
// decrease the number (50) for more compression
$fianl_file = compress($source_img, $destination_url, 50);
$error = "Image Compressed successfully";
}else {
$error = "Uploaded image should be jpg or gif or png";
}
}
?>
[/code]
3) HTML code integration
In this step, I have created one form to choose the image which user want to compress. Now, the user can choose the image and the image will be compressed without losing the quality. Refer the html code snippet below.
[code]
<form action="index.php" name="img_compress" id="img_compress" method="post" enctype="multipart/form-data">
<ul>
<li>
<label>Upload:</label>
<input type="file" name="file" id="file"/>
</li>
<li>
<input type="submit" name="submit" id="submit" class="submit btn-success"/>
</li>
</ul>
</form>
[/code]
4) Full project code : Copy the below code and paste it in âindex.phpâ file
The second step is just to copy the below code and paste it in âindex.phpâ file and run in your browser.
[code]
<?php
ini_set("error_reporting", 1);
function compress($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);
return $destination;
}
if (isset($_POST['submit'])) {
if ($_FILES["file"]["error"] > 0)
{
$error = $_FILES["file"]["error"];
}
else if (($_FILES["file"]["type"] == "image/gif") ||
($_FILES["file"]["type"] == "image/jpeg") ||
($_FILES["file"]["type"] == "image/png") ||
($_FILES["file"]["type"] == "image/pjpeg"))
{
$destination_url = 'uploads/demo.jpg';
$source_img = $_FILES["file"]["tmp_name"];
$fianl_file = compress($source_img, $destination_url, 50);
$error = "Image Compressed successfully";
}else {
$error = "Uploaded image should be jpg or gif or png";
}
}
?>
<html>
<head>
<title>Php code compress the image</title>
</head>
<body>
<fieldset class="well">
<legend>Upload Image:</legend>
<form action="index.php" name="img_compress" id="img_compress" method="post" enctype="multipart/form-data">
<ul>
<li>
<label>Upload:</label>
<input type="file" name="file" id="file"/>
</li>
<li>
<input type="submit" name="submit" id="submit" class="submit btn-success"/>
</li>
</ul>
</form>
</fieldset>
<br><br><br>
<center>
<?php if($_POST){ if ($error) { ?>
<h3><?php echo $error; ?></h3>
<?php }} ?>
</center>
</body>
</html>
[/code]
I hope you will like this tutorial and enjoyed it. If you have any questions then ask me here.
Thanks