$error = "Uploaded image should be jpg or gif or png" $error = "Image Compressed successfully" $fianl_file = compress($source_img, $destination_url, 50) decrease the number (50) for more compression The code snippet is given below.Įlse if (($_FILES = "image/gif") || The next part is to store the image in the “uploads” folder. The function is used to compress “jpeg/jpg, gif, png” images. In this portion, I have created one function for image compression. A file named “index.php” is for executing the code and “uploads” folder is used for store compressed/reduced images. After index.php file creation, create one folder named “uploads” in your server. View Demo I have divided this tutorial in 5 simple steps 1) Create “index.php” file and “uploads” folder 2) PHP code integration 3) HTML code integration 4) Merge PHP and HTML code 5) Conclusion Follow the steps to reduce or compress images in PHPġ) Create one file “index.php” and one folder named “uploads” in your serverįirst of all, you need to create one new file named “index.php”. By using this function we can make images small-sized without losing image quality. Developers can use this PHP script and compress images before uploading the image. So it will increase the website loading time and obviously, the visitors don’t like to wait for loading and they leave the website.įor SEO ranking the site loading speed is now very important and we can improve SEO ranking by image compression/ optimizations. They don’t know the large size images take more time to load the website. Generally, users upload large size images on websites. You can find the demo link at the bottom of the page. This function is very easy to fix on your website and I have created the demo for your better understanding. This script is used to optimize/compress the images while uploading in PHP. With our compression script, you can compress different types of an image files (JPG, JPEG, PNG, and GIF).In this tutorial, I will show you how to reduce or compress images in PHP by using PHP function. The example code helps you to compress the image file without using any compression library. But, if you want to compress the image before upload, our custom PHP function ( compressImage()) is very useful. Generally, the move_uploaded_file() function is used to upload file in PHP. Preview and Rotate Image Before Upload using jQuery and PHP Conclusion Original Image Size: Compressed Image Size: "/> $destination – Server path to save the compressed image.$source – An image file source to compress size.This function accepts the following parameters and returns the compressed image. The compressImage() is a custom function that helps to compress and save image on the server using PHP. The upload.php file handles the image compression and upload operations. Make sure the tag contains the following attributes.Īfter the form submission, the file data is submitted to the upload.php file for further processing. File Upload FormĬreate an HTML form with a file input field and a submit button. In this tutorial, we will show you how to compress image before upload using PHP. The compressed image helps to reduce the uses of the server’s storage and load the web page faster. In the image compress functionality, the file size is reduced before upload. It’s always a good idea to compress images and optimize the file size before uploading them to the server.Ĭompress and optimize image before upload can be easily implemented using PHP. In most cases, the user does not optimize the image when uploading through the website. Image compression is very helpful to reduce the size of the image. If you want to load a large image without affecting the page load time, the image needs to be optimized to reduce the size. A large size image takes more time to load a web page.
0 Comments
Leave a Reply. |