jquery Compress or resize Image File Size before Uploading PHP Code

jquery Compress or resize Image File Size before Uploading PHP Code

In this Post We Will Explain About is jquery Compress or resize Image File Size before Uploading PHP Code With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Decrease image filesize in jQuery before uploadExample

In this post we will show you Best way to implement Compress,Reduce,Resize The Image Before Uploading To Database, hear for Get File Size before Uploading in JavaScript and jQuerywith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

It is just default to upload the particulre images dynamically to the websites. But when we image upload the large file size image on the website, it consumes a lot of time while loading. So the developer should write the code to reduce the image file size while uploading the image dynamically to the website. Using PHP, you can easily reduce the file size of those uploaded images during time of upload. Of course, when reducing the file size we sacrifice the image img_qlty.

Also Read :  Google Customize Timelines Charts

Source Code to reduce file size for the image

<?php 
	function img_compress($img_source, $liveupload, $img_qlty) {

		$img_information = getimagesize($img_source);

		if ($img_information['mime'] == 'image/jpeg') 
			$live_ifoimg = imagecreatefromjpeg($img_source);

		elseif ($img_information['mime'] == 'image/gif') 
			$live_ifoimg = imagecreatefromgif($img_source);

		elseif ($img_information['mime'] == 'image/png') 
			$live_ifoimg = imagecreatefrompng($img_source);

		imagejpeg($image, $liveupload, $img_qlty);

		return $liveupload;
	}

	$img_source_img = 'img_source.jpg';
	$upload_img = 'liveupload .jpg';

	$d = img_compress($img_source_img, $upload_img, 90);
 ?>

$d = img_compress($img_source_img, $upload_img, 90);

This is simple a php function that passes the img_source image ( i.e., $img_source_img ), liveupload image ( $upload_img ) and img_qlty for the image that will take to img_compress ( i.e., 90 ).

$img_information = getimagesize($img_source);

The getimagesize() php function is used to very simple find the size of any data given image file source and return the information dimensions along with the some file type.

Example:

$img_information = getimagesize($img_source);
print_r($img_information);

Output:

Array ( [0] => 1850 [1] => 768 [2] => 2 [3] => width="1850" height="768" [bits] => 8 [channels] => 3 [mime] => image/jpeg )

$live_ifoimg = imagecreatefromjpeg($img_source);
$live_ifoimg = imagecreatefromgif($img_source);
$live_ifoimg = imagecreatefrompng($img_source);

All the above PHP functions are used to create a simple new image from the given simple file or URL. as well as These functions are used to simple return an image as well as jpg and format identifier representing the particulre image obtained from simple the given file name.

imagejpeg($image, $liveupload, $img_qlty);

imagejpeg() php function is used to create a like as a JPEG file format from the given image.

Also Read :  PHP Image Compression before Uploading

Syntax: imagejpeg ( $img_source_image, $liveupload_image, $img_qlty )

Quality ($img_qlty): img_qlty is optional, and ranges from 0 (worst img_qlty, smaller file) to 100 (best img_qlty, biggest file). The default simplerange is 75.

Note :The GD used to the library is used for dynamic image new creation. From PHP we simple use with the GD library to new or create GIF format, PNG format or JPG format. This is very simple as well as important to run all the image new creation function core in PHP. If your server not any doesn’t support the GD php library then all the function above functionality related to the php image creation will simple not work.

Full source code to reduce the image file size:

<?php
	$file_name = ''; $file_type = ''; $file_size = ''; $live_error = '';
	function img_compress_image($img_source_url, $liveupload_url, $img_qlty) {

		$img_information = getimagesize($img_source_url);

    		if ($img_information['mime'] == 'image/jpeg')
        			$live_ifoimg = imagecreatefromjpeg($img_source_url);

    		elseif ($img_information['mime'] == 'image/gif')
        			$live_ifoimg = imagecreatefromgif($img_source_url);

   		elseif ($img_information['mime'] == 'image/png')
        			$live_ifoimg = imagecreatefrompng($img_source_url);

    		imagejpeg($image, $liveupload_url, $img_qlty);
		return $liveupload_url;
	}

	if ($_POST) {

    		if ($_FILES["file"]["live_error"] > 0) {
        			$live_error = $_FILES["file"]["live_error"];
    		} 
    		else if (($_FILES["file"]["type"] == "image/gif") || 
			($_FILES["file"]["type"] == "image/jpeg") || 
			($_FILES["file"]["type"] == "image/png") || 
			($_FILES["file"]["type"] == "image/pjpeg")) {

        			$url = 'liveupload_simple .jpg';

        			$filename = img_compress_image($_FILES["file"]["tmp_name"], $url, 80);
        			$buffer = file_get_contents($url);

        			/*Pakainfo.com  Force download simple dialog... */
        			header("Content-Type: application/force-download");
        			header("Content-Type: application/octet-stream");
        			header("Content-Type: application/download");

					/* Pakainfo.com Don't allow simple caching... */
        			header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

        			/*Pakainfo.com  Set data type simple, size simple and simple filename */
        			header("Content-Type: application/octet-stream");
        			header("Content-Transfer-Encoding: binary");
        			header("Content-Length: " . strlen($buffer));
        			header("Content-Disposition: attachment; filename=$url");

        			/*Pakainfo.com  Send our file... */
        			echo $buffer;
    		}else {
        			$live_error = "Uploaded image should be jpg or gif or png";
    		}
	}
?>
<html>
    	<head>
        		<title>Php code img_compress the image</title>
    	</head>
    	<body>

		<div class="message">
                    	<?php
                    		if($_POST){
                        		if ($live_error) {
                            		?>
                            		<label class="live_error"><?php echo $live_error; ?></label>
                        <?php
                            		}
                        	}
                    	?>
                	</div>
		<fieldset class="well">
            	    	<legend>simple here you can file Upload Image:</legend>                
			<form action="" name="liveform" id="liveform" method="post" enctype="multipart/form-data">
				<ul>
			            	<li>
						<label>simple File Upload:</label>
			                                <input type="file" name="file" id="file"/>
					</li>
					<li>simple
						<input type="submit" name="submit" id="submit" class="submit btn-warning"/>
					</li>
				</ul>
			</form>
		</fieldset>
	</body>
</html>

Example

Also Read :  Javascript Progress Bar percentage Example

I hope you have Got What is Image resizing client-side with JavaScript before upload to the server And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Recommended For You.

PHP Image Compression before Uploading Today, We want to share with you PHP Image Compression before Uploading. In this post we will