Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:49:45+00:00 2026-06-11T20:49:45+00:00

I have a php script where the user can upload images. I want to

  • 0

I have a php script where the user can upload images.
I want to make the script lower the image quality (jpeg) if the file size is bigger than ‘X’ kbytes.

Something like this:

if( $_FILES['uploaded_img']['size'] > $file_size_limit ){
     // code that lowers the quality of the uploaded image but keeps the image width and height
}

What is the best approach for this?

ps: I don’t want to change image width and height.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:49:47+00:00Added an answer on June 11, 2026 at 8:49 pm

    Sure you can. Do something like this.

    $upload = $_FILES['uploaded_img'];
    $uploadPath = 'new/path/for/upload/';
    $uploadName = pathinfo($upload['name'], PATHINFO_FILENAME);
    $restrainedQuality = 75; //0 = lowest, 100 = highest. ~75 = default
    $sizeLimit = 2000;
    
    if($upload['size'] > $sizeLimit) {
        //open a stream for the uploaded image
        $streamHandle = @fopen($upload['tmp_name'], 'r');
        //create a image resource from the contents of the uploaded image
        $resource = imagecreatefromstring(stream_get_contents($streamHandle));
    
        if(!$resource)
            die('Something wrong with the upload!');
    
        //close our file stream
        @fclose($streamHandle);
    
        //move the uploaded file with a lesser quality
        imagejpeg($resource, $uploadPath . $uploadName . '.jpg', $restrainedQuality); 
        //delete the temporary upload
        @unlink($upload['tmp_name']);
    } else {
        //the file size is less than the limit, just move the temp file into its appropriate directory
        move_uploaded_file($upload['tmp_name'], $uploadPath . $upload['name']);
    }
    

    This will accept any image format supported by PHP GD (Assuming that it’s installed on your server. Most likely is). If the image is less than the limit, it will just upload the original image to the path you specify.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have php script from where user can upload the video file. The file
I (finally) completed a php script where a user can upload a file and
I have a file upload script that works [finally] but want to have users
i have a form in which user can upload 100 mb file which results
I have a cancel button where the user can cancel on a file upload
I want any php script which can demonstrate me how to upload multiple files
I'm making a script where the user can upload an image of his pet.
I have a PHP script that creates other PHP files based on user input.
I have simple question. User supplies URL to my PHP script where I fetch
I have a very simple script that creates a user: <?php include 'mysqlserver.php'; session_start();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.