Compress,Reduce,Resize The Image Before Uploading To Database With PHP
Last Updated : Jul 1, 2023
In this tutorial we will show you an easy and quick way to PHP image resize before uploading it to the server, image manipulation is always be one of the most difficult task for a programmer.
Because images are need to small and beautiful after manipulation and this takes time and good programming knowledge.
But nowadays, PHP make this very simple you can do any kind of image manipulation you want with the help of its GD Library.
Before we start you have the knowledge of how to upload image to the server if you dont know please see our php image upload tutorial and then start.
There are many other libraries which are also used for image manipulation like Imagick, jQuery Plugins etc.
You can manipulate the image in just two simple steps:-
- Make a HTML form to upload the image
- Recieve The Image And Manipulate
Step 1. Make a HTML form
We make a HTML form with post method and save it with a name upload_form.html
<html> <body> <form method="POST" action="getdata.php" enctype="multipart/form-data"> <input type="file" name="image1"> <input type="submit" name="upload_image" value="Upload"> </form> </body> </html>
We submit the data from this HTML form to resize_image.php where we do image manipulation.
Step 2. Recieve The Image And Manipulate
In this step we get the image and then resize.
<?php $upload_image = $_FILES[" image1 "][ "name" ]; $folder = "/xampp/htdocs/images/"; move_uploaded_file($_FILES[" image1 "][" tmp_name "], "$folder".$_FILES[" image1 "][" name "]); $file = '/xampp/htdocs/images/'.$_FILES[" image1 "][" name "];
We have to save image in the directory first and then we do image manipulation. You can use any path of directory from where you want to save the image.
You may also like jquery image resize.
$uploadimage = $folder.$_FILES[" image1 "][" name "]; $newname = $_FILES[" image1 "][" name "]; // Set the resize_image name $resize_image = $folder.$newname."_resize.jpg"; $actual_image = $folder.$newname.".jpg"; // It gets the size of the image list( $width,$height ) = getimagesize( $uploadimage ); // It makes the new image width of 350 $newwidth = 350; // It makes the new image height of 350 $newheight = 350; // It loads the images we use jpeg function you can use any function like imagecreatefromjpeg $thumb = imagecreatetruecolor( $newwidth, $newheight ); $source = imagecreatefromjpeg( $resize_image ); // Resize the $thumb image. imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // It then save the new image to the location specified by $resize_image variable imagejpeg( $thumb, $resize_image, 100 ); // 100 Represents the quality of an image you can set and ant number in place of 100. Default quality is 75 $out_image=addslashes(file_get_contents($resize_image)); // After that you can insert the path of the resized image into the database mysql_connect(' localhost ' , root ,' ' ); mysql_select_db(' image_database '); $insertquery = " insert into resize_images values('1,$out_image') "; $result = mysql_query( $insertquery ); ?>
For more details and PHP image manipulations functions you can learn from this site.
That's all, this is how to do image resize using PHP before uploading with the help of PHP GD Library, HTML and MySQL.
You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
I hope this tutorial on resize image before upload using php helps you and the steps and method mentioned above are easy to follow and implement.