FOLLOW US ON TWITTER



FOLLOW US



img/blogimg/php.jpg


@theprodevelopers 271

how to compress image size while uploading in php?


2023-07-10 11:12 · 2 min read

We use compressImg($source, $destination, $quality) function that helps to compress and save image on the server using PHP.

Here is the compression code.

Html Code

<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Select Image File:</label>
<input type="file" name="image">
<input type="submit" name="submit" value="Upload">
</form>

This function accepts the following parameters and returns the compressed image.

  • $source – An image file source to compress size.
  • $destination – Server path to save the compressed image.
  • $quality – Image quality.

php code

function compressImg($source, $destination, $quality) {
// Get image info
$info = getimagesize($source);


// Create a new image from file
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);

elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);

elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);

// Save image

imagejpeg($image, $destination, $quality);


// Return compressed image
return $destination;
}
TAGS:






POST COMMENTS
No Comment

Please login to post comment




POPULAR POSTS

best free web hosting services...
what is new in miui 11?...
free website ssl security using cloudfla...
do you want to make money online?...
CSS3 @IMPORT RULES...
What is HTML Anchor Tag and Hyperlinks?...
How to host a web page in 10 minutes....
CSS3 RESPONSIVE UI...
Tables HTML...
CSS3 FUNCTIONS...
How to use CSS3 Overflow Property?...
CSS3 FILTERS...


RECENT COMMENTS

2022-03-05 11:05
By coder on XAMPP - MySQL shutdown unexpectedly | How to fix MySQL crash unexpectedly
2021-10-12 12:34
By abnongoke on do you want to make money online?
2021-10-12 12:34
By abnongoke on how to get a free website domain name?
2021-10-12 12:34
By solar panel for shed on what is new in miui 11?
2021-10-12 12:34
By solar panel for shed on best free web hosting services
2021-10-12 12:34
By amos tanui on free website ssl security using cloudflare


SOCIAL SHARE



FOLLOW US ON TWITTER



FOLLOW US ON FACEBOOK



FOLLOW US







Recent Blogs



Contact Us

Subscribe Our News-Letter



Kashipur, 244713
Uttarakhand, India



© www.prodevskill.com 2023. All Rights Reserved. Powered by Pro Developer