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 8397465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:47:57+00:00 2026-06-09T20:47:57+00:00

I have a script, that’s pretty huge for an actual simple task, because it’s

  • 0

I have a script, that’s pretty huge for an actual simple task, because it’s nested very often. What it does is this:

  • get the current size of an image
  • get the new maximum dimensions
  • calculate the dimensions the image is going to be resized to (aspect ratio is always kept):
    • if image is smaller than new dimensions: keep original size
    • if max new dimensions have an x and y-value: scale image to fit both values
    • if max new dimensions have only a x-value: scale to fit at least x
    • if max new dimensions have only a y-value: scale to fit at least y

This is the script:

//example values, whereas 'new' is the maximum range for the new image
$dimensions = array(1920,1200);
$dimensionsNew  = array(160, 0);
// percentage the image must be scaled
$percentages    = array(0,0);

//calculate scale range
if ($dimensionsNew[0] != 0) $percentages[0] = $dimensions[0] / $dimensionsNew[0];
if ($dimensionsNew[1] != 0) $percentages[1] = $dimensions[1] / $dimensionsNew[1];

//get new dimensions
if ( ($dimensions[0] <= $dimensionsNew[0]) && ($dimensions[1] <= $dimensionsNew[1]) ) {
    $dimensionsNew[0]       = $dimensions[0];
    $dimensionsNew[1]       = $dimensions[1];
} elseif ($dimensionsNew[0] == 0) {
    if ($dimensions[1] <= $dimensionsNew[1]) {
        $dimensionsNew[0]   = $dimensions[0];
        $dimensionsNew[1]   = $dimensions[1];
    } else {
        $dimensionsNew[0]   = ceil($dimensions[0] / $percentages[1]);
    }
} elseif ($dimensionsNew[1] == 0) {
    if ($dimensions[0] <= $dimensionsNew[0]) {
        $dimensionsNew[0]   = $dimensions[0];
        $dimensionsNew[1]   = $dimensions[1];
    } else {
        $dimensionsNew[1]   = ceil($dimensions[1] / $percentages[0]);
    }
} elseif ($percentages[0] < $percentages[1]) {
    $dimensionsNew[1]       = ceil($dimensions[1] / $percentages[1]);
} else {
    $dimensionsNew[0]       = ceil($dimensions[0] / $percentages[0]);
}

It does what it should do. But is there an easier way of doing this? I think I looked at this code too often and can’t find a way to optimize.

  • 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-09T20:47:58+00:00Added an answer on June 9, 2026 at 8:47 pm

    You should use “min” on the rations of the sizes to work out the scale required. In this is >1 then no shrinking ncessary, if <1 then shrink by the right amount

    //example values, whereas 'new' is the maximum range for the new image
    $dimensions = array(1920,1200);
    $dimensionsNew  = array(160, 0);   // NOTE: 0 won't work, but it's your example ;)
    
    // What scale do we need to go to
    $scaleRequired = min($dimensionsNew[0] / $dimensions[0], $dimensionsNew[1] / $dimensions[1]);
    
    if ($scaleRequired < 1) {
        $finalDimensions = array (
             $dimensions[0] * $scaleRequired, 
             $dimensions[1] * $scaleRequired);
    
        // Resize to $finalDimensions
    
    } else {
        // Do nothing
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script that does only the parent folder, it does not rename
I have this script that run to fix my menu bar to the browser
I have this script that will let the user download the data from the
I have this script that preloads a stack of images on my index page
I have script.sh that must be run as user2. However, this script can only
I have this script that I'd like to in addition save as a xls
I have a script that does graphing, using jqplot . It works fine when
I have script that reads remote file content and writes it to local server.
I have a script that recursively loops through all the sub directories and compresses
I have a script that submits data via an ajax POST. It is called

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.