![]() $sql = "INSERT INTO compress_image(image)VALUES ('".$new_profle_pic. Step 6:- Now we are storing the image name in the database.Īfter that, we need to store image name into the database. It takes value from 0 – 100 and the default value is 75. File qualityĮxecute imagejpeg() method to store image to the destination. Here we use a compressedImage() function to compress PNG, JPEG and GIF images to save memory. Step 4:- Create Images folder to store imagesĬreate new folder images for store uploaded images.Ĭompress Image size while Uploading in PHP This form is having a post method attribute set to action and the encryptedattribute is set to multipart/form-data (for upload a file) The following sample HTML code given below creates an uploader HTML form. Syntax : – mysqli_connect( hostname, password, username, database) The mysqli_connect() function opens a new connection to the MySQL server. Step 2:- Create a connection to the database in the PHP file. CREATE TABLE `codingmantra`.`compress_image` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `image` VARCHAR(250) NOT NULL, PRIMARY KEY (`id`)) ENGINE = InnoDB Step 1:- Create a compress_image table in the database. In the above article, we learned a complete process of image upload and compression with necessary validations.In this tutorial, we are going to learn about how to compress image size while uploading with PHP Here we have set the destination path to uploads/ in our working directory. Note: Change the destination directory according to your config. In the end, we checked for error in the upload process and used our compress_image() function to handle compression of the uploaded image. $quality: The rate of image quality we want to maintain.$destination_url: Destination Folder/Directory path where we want to create a new image.$image: Raw image data we created and stored in $image variable.img imagecreatefromjpeg ('myimage.jpg') // load the image-to-be-saved // 50 is quality change from 0 (worst quality,smaller file) - 100 (best quality) imagejpeg (img,'myimagenew.jpg',50) unlink ('myimage. There are three arguments we passed in imagejpeg() function. 4 Answers Sorted by: 25 you're not telling if you're using GD, so i assume this. Imagejpeg() function is basically image creator function from raw image data. In the compress_image() function we first found for image specifications by using getimagesize() function and checked for its mime type.įrom mime type, we checked the image is valid or not and create a related replica of image in PHP according to jpeg, png, gif using imagecreatefromjpeg(), imagecreatefrompng(), imagecreatefromgif() respectively and saved it in $image variable.įinally, we created a jpeg image with the help of a $image variable by using imagejpeg() function. As a prerequisite, install the PHP’s GD Graphics Library. If we want to maintain 100% quality we can simply apply 100 or we can pass 80 if we want to compress and lose the image quality by 20%. You can compress images in PHP in one of the following ways: Through PHP’s built-in functions, such as imagejpeg (), which takes the path of the output image and quality specification between 1 and 100. $quality: It is the rate of Image quality we want to maintain from 1 to 100.$destination_url: It is the destination path where we want to upload our Image.$source_url: It is the temporary location in our case of file upload.In this function, we passed three variables of the uploaded file. Next, we created a compress_image() function to upload and compress the image. Like, File Name , File Type , File Size , Temporary Location of Uploaded File , and Error in File Upload Process if any. For File and Image upload POST method is compulsory.Īfter that, we defined some variables related to the uploaded file. In the second step first, we checked for the form request method. $file_name, 80) Įcho "Uploaded image should be jpg or gif or png." $filename = compress_image($temp_name, "uploads/". STEP 2: Add below PHP code at the top of PHP fileĮlse if (($file_type = "image/gif") || ($file_type = "image/jpeg") || ($file_type = "image/png") || ($file_type = "image/pjpeg")) Note: enctype=”multipart/form-data” is compulsory for image upload precess via post method in form. ![]() ![]() How to compress an image without losing quality in PHP STEP 1: Create an HTML formįirst, we have to create a simple HTML form to upload an image file. Here we are going to learn how to compress an image in PHP without losing its quality in just 2 easy steps. Many popular Core-PHP frameworks and CMS use default function to generate thumbnail and images without losing image quality. PHP provides some default functions to compress images and to resize them. Even more, we can resize images in PHP to generate thumbnails and lightweight web images. PHP provides the ability to compress images without losing the quality of it. How to Compress Image Size Without Losing Quality in PHP
0 Comments
Leave a Reply. |