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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:06:24+00:00 2026-06-14T16:06:24+00:00

I’m trying to downsize image uploaded by users at the time of upload. This

  • 0

I’m trying to downsize image uploaded by users at the time of upload.
This is no problem but I want to get specific with sizes now.

I’m looking for some advice on an algorithm im struggling to produce that will take any shape image – square or rectangle of any widths/heights and downsize it.

This image needs to be downsized to a target box size (this changes but is stored in a variable)..
So it needs to downsize the image so that both the width and height are larger than the width and height of the target maintaining aspect ratio. but only just..
The target size will be used to crop the image so there is no white space around the edges etc.

I’m looking for just the algorithm behind creating the correct resize based on different dimension images – I can handle the cropping, even resizing in most cases but it fails in a few so i’m reaching out.

I’m not really asking for PHP code more pseudo. Either is fine obviously.

Thanks Kindly.

Current code.. but I’ve gone through so many iterations this might not work at all anymore.. 😛

$image = $image_orig->clone();
$wRatio = $imageprops['width'] / $dimensions[0];   // imageprops = original image dimens.
$hRatio = $imageprops['height'] / $dimensions[1];  // $dimensions[0] = new width
$maxRatio = max($wRatio,$hRatio);                  // $dimensions[1] = new height

var_dump('thumb');

$ratio = ($imageprops['width'] - $dimensions[0]) > ($imageprops['height'] - $dimensions[1]);
$shape = ($imageprops['width'] > $imageprops['height']);
$error = ($imageprops['width'] / $imageprops['height']);

if( $error < 0.95 || $error > 1.05 ) { // its NOT a square
    if($shape){  // longer width
        $scale = $imageprops['height'] / $dimensions[0];
        var_dump('1');

        $height = $imageprops['height'] / $scale;
        $image->scaleImage(0, $height);
    } else {
        $scale = $imageprops['width'] / $dimensions[1];
        var_dump('2');

        $width = $imageprops['width'] / $scale;
        $image->scaleImage($width, 0);


    }
} elseif($ratio) { // is square
    $scale = $imageprops['height'] / $dimensions[1];
    $newWidth = $imageprops['width'] / $scale;
    $extra = 0;

    $height = $dimensions[1]+$extra;
    $image->scaleImage(0, $height);
} else {
    $scale = $imageprops['width'] / $dimensions[0];
    $newHeight = $imageprops['height'] / $scale;
    $extra = 0;

    $width = $dimensions[0]+$extra;
    $image->scaleImage($width, 0);

}


$image_size = $image->getImageGeometry();

$image->cropImage($dimensions[0], $dimensions[1],($image_size['width']-$dimensions[0])/2,($image_size['height']-$dimensions[1])/2);
  • 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-14T16:06:26+00:00Added an answer on June 14, 2026 at 4:06 pm

    Notice: I wrote this answer before the original poster edited his question to include things which clarified points which has since changed what I believed the original question was asking.


    So, there are a few concepts and ideas that you can throw around to try and solve what it is you are intending to achieve. (Gravity Cropping, Content Aware Cropping, Content Aware Rescaling etc)

    Because you are always decreasing the size of the original image, you are essentially just looking to “chop” out a section of the image. Very much like this:

    enter image description here

    The issue however, is that you frequently want to make sure that you select the best region of the image so that you don’t crop on an irrelevant segment of the image. This is known as content-aware image cropping, and by just searching using “Content Aware Image Crop” you can find a wealth of information.

    Anyway, moving on, depending on your exact use case, you might discover that actually you don’t want to chop out anything form the image, and instead you want to “liquid scale and crop” the image, so that you do “Content Aware Resizing”. The difference with content aware resizing is that the resizing ignores all aspect ratios of the image and instead looks at neighbouring edges and colors in order to resize the image in a fluidic way.

    So, luckily enough, Imagick comes with it’s very own [liquidRescaleImage][3] function.

    You can use it like

    $im->liquidRescaleImage(480, 260, 3, 18);

    Using Liquid Rescale can rescale out quite nicely. Here is an example below which is anything but perfect, but I purposefully have created an example that isn’t perfect so you can actually see that liquidRescale changes the composition of the image, rather than just adjusts the aspect ratio.

    Original

    Original

    Content Aware Liquid Rescale (450×350)

    Rescale

    If however, you just want to scale an image, and keep the aspect ratio, you might want to do what sites such as Flickr do, where they make the images longestLength to be equal to a value. (Flickr for example has 6 or so different dimension sizes)

    We resize your photos to more web-friendly dimensions. Each image has
    a 75×75 and 150×150 square thumbnail and 100-, 240-, 320-, 500-, 640-
    800*-, 1024-, 1600*-, and 2048*-pixel versions (that’s the length of
    the longest side), as well as your original file.

    Basic Class that Replicates the Flickr Resize Policies…

    <?php    
    class Scale {        
        public function getImageScale($x, $y, $longestLength, $allowDistort = false) {
            //Set the default NEW values to be the old, in case it doesn't even need scaling
            list($nx, $ny) = array($x, $y);            
                if ($x > $y) {                   
                    if ($longestLength > $x && !$allowDistort) {
                        return array($x, $y);
                    }
                    $r = $x / $longestLength;
                    return array($longestLength, $y / $r);
                } else {
                    if ($longestLength > $x && !$allowDistort) {
                        return array($x, $y);
                    }
                    $r = $y / $longestLength;
                    return array($x / $r, $longestLength);
                }
            return array($nx, $ny);
        }       
    }
    

    And then if you were to pass in the Image Dimensions like this:

    $originalImageX = 480;
    $originalImageY = 260;    
    $scale = new Scale();
    var_dump($scale->getImageScale($originalImageX,$originalImageY,120 ) );    
    
    /* Outputs
       array(2) {
      [0]=>
      int(120)
      [1]=>
      int(65)
    } */
    

    Also, there is this Content Aware Cropping class on Git, that I have previously adapted the base class/Structure previously to use in my own project so I know that it works nicely. I’m not sure on the licencing of this class, but you can obviously take a look at it and slice out what works for you as I have done previously.

    https://gist.github.com/2468935#file_content_aware_image.php

    (And PS. Provide all the information in your questions straight up next time…)

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.