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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:16:49+00:00 2026-05-23T14:16:49+00:00

I have a defined fixed width and height to resize an image. However, I

  • 0

I have a defined fixed width and height to resize an image. However, I have problem a with this, because the image can have any kind of size ratio (it can be vertical or the horizontal). In this case, fixed width and height cause a problem. I want to calculate width and height in a smarter way.

For example lets say I have defined width 1024px and height 768px. And I want to resize an image which is vertical (height 1100px and width 200px). So in my case it will resize to fixed size (1024×768), so the width will be increased from 100px to 768px, and it will be very ugly. Similarly if the image has height less than 768px, it will increase the height by force to 768px.

Therefore I would like to calculate the new image size based on the original image size ratio. Lets say if the above example image should be resized to maximum height of 768px, but then what about the width? it’s already less than my “maximum width”, which is 200px, so should the width remain unchanged? or should it be further decreased?

Similarly, if the image has the height 200px, and the width 1100px. So the width should be decreased to 1024px, but what about the height?

The third problem is that, let’s suppose if both height and width are more than the maximum height and maximum width, let’s say width: 1100px and height:4000px. Now since width and height both are more than the maximum width and maximum height but the image is vertical, it will make it horizontal. So how can I check if in this case if I should resize the image according to maximum height, or according to maximum width?

I appreciate any help with this.

  • 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-05-23T14:16:49+00:00Added an answer on May 23, 2026 at 2:16 pm

    Here’s code from my personal grab bag of image resizing code. First, data you need:

    list($originalWidth, $originalHeight) = getimagesize($imageFile);
    $ratio = $originalWidth / $originalHeight;
    

    Then, this algorithm fits the image into the target size as best it can, keeping the original aspect ratio, not stretching the image larger than the original:

    $targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight));
    
    if ($ratio < 1) {
        $targetWidth = $targetHeight * $ratio;
    } else {
        $targetHeight = $targetWidth / $ratio;
    }
    
    $srcWidth = $originalWidth;
    $srcHeight = $originalHeight;
    $srcX = $srcY = 0;
    

    This crops the image to fill the target size completely, not stretching it:

    $targetWidth = $targetHeight = min($originalWidth, $originalHeight, $size);
    
    if ($ratio < 1) {
        $srcX = 0;
        $srcY = ($originalHeight / 2) - ($originalWidth / 2);
        $srcWidth = $srcHeight = $originalWidth;
    } else {
        $srcY = 0;
        $srcX = ($originalWidth / 2) - ($originalHeight / 2);
        $srcWidth = $srcHeight = $originalHeight;
    }
    

    And this does the actual resizing:

    $targetImage = imagecreatetruecolor($targetWidth, $targetHeight);
    imagecopyresampled($targetImage, $originalImage, 0, 0, $srcX, $srcY, $targetWidth, $targetHeight, $srcWidth, $srcHeight);
    

    In this case the $size is just one number for both width and height (square target size). I’m sure you can modify it to use non-square targets. It should also give you an inspiration on what other resizing algorithms you can use.

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

Sidebar

Related Questions

I have a container div with a fix height defined. This div contains two
Problem Problem shaping Image sequence position and size are fixed and known beforehand (it's
I have a DIV defined with a fixed height: .w { height: 100px; overflow:
I have defined a tag with a CSS attribute overflow set to scroll. This
how can I have 2 block elements (block because I want to apply a
Does anyone have any recommendations for dealing with user-defined data structures? i.e. your users
It looks like in edit and add forms all field have fixed width. I
I have this main.xml layout which has a fixed header and footer. The body
I am creating a page where I do not want any fixed px height
We have some JavaScript code that resizes <div/> elements (adjusts height/width/padding/margin etc.) based on

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.