Compress,Reduce,Resize The Image Before Uploading To Database With PHP.
Tuesday, Apr 4, 2017
Resizing the image is always be the difficult task doing with programming languages but nowadays, PHP make this simple. With PHP you can do any king of image manipulation you want with the help of its GD Library. In this tutorial we show an easy way to resize the image before uploading it to the server before we start you have the knowledge of how to upload image to the server if you dont know please see our How to Upload Images 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>
You can validation form to make your code more secure.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 resize and crop image using jQuery.
$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 ); ?>
You can learn from this site all the Image Manipulating functons in PHP.
That's all, this is how to resize the image 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.