Link to home
Start Free TrialLog in
Avatar of matthew016
matthew016Flag for Belgium

asked on

Resizing image, need algo

Hello,

I need to resize an image as a thumbnail.
I need to compute the correct dimensions by keeping the ratio.

The params are :
old_x, old_y (the current dimensions of the image),
new_w (the width of the thumbnail), new_y (the height of the thumbnail)

The new values must be assigned in thumb_w and thumb_h

Thank you.
Avatar of Robin Hickmott
Robin Hickmott

Avatar of matthew016

ASKER

No.

As you can read in this tutorial it is only resizing according to a given width

"The first and the second is correspondingly the path to the directory that contains original images and the path to the directory in which thumbnails will be placed. The third parameter is the width you want for the thumbnail images."
SOLUTION
Avatar of Joe Wu
Joe Wu
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well the first problem your going to run into is that photos taken in landscapre are going to have different proportions to ones taken in landscape.

What are you suggesting a reduction by a certain percentage? your going to need a base width or height to start with for example 100px.

Its then a case of taking the original size and dividiging it by the new width to get the ratio and then applying that to the height as well.
myThumbnail.php?url=folder/image.jpg
or even just
myThumbnail.php?url=image.php

Correction i was meaning

myThumbnail.php?url=folder/image.jpg
or even just
myThumbnail.php?url=image.jpg
@nizsmo

Could you please explain me why you need a "while" for this ? and why all the calculations are based on the width/height "+1" ?

@rhickmott

the width may not exceed 90 and height may not exceed 120 so I have no choice here.
this should take care of the calculations for you

        $old_width =  800;
      $old_height = 600;
      
      $thumb_w = 90;
      
      $thumb_h = round(($thumb_w/$old_width)*$old_height);

      if($thumb_h >120) {

            $height = 120;
            
            $thumb_w = round(($thumb_h/$old_height)*$old_width);
      
      }

      
      
      print $thumb_w;
      
      print "<br>";
      
      print $thumb_h;
matthew016:

I wrote the scale image code a LOOONNNGGG time ago being honest i really cannot remember...

Did it work ok for you though? I think everything will resize proportionally, I wrote the code based on that requirement i vaguely remember.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@nizsmo

I think I can get it cleaner

@rhickmott

Looks very nice but it seems not what I want still ...

E.g. :

      // If Image Actual Width or Height smaller than thumbnail then use it
      if (($image_width <= $thumb_width) || ($image_height <= $thumb_height) ) {
        $tn_width       =       $image_width;
        $tn_height       =       $image_height;
      }

I think this will accept an image if 80px width and 5000 px height if I'm not wrong ... (if max width is set to 90 and height to 120)
I think it runs relatively efficiently, as I am using it on my own sites.
Any reason why you are wanting to simplify? I guess for readability?

But it works for you right?
@nizsmo

I need code I understand.

@all

Ok guys, I can't believe I had to pump the code myself :

$old_w = // get image width
$old_h = // get image height

$new_w = 90;
$new_h = 120;

$x_ratio = $new_w / $old_w;
$y_ratio = $new_h / $old_h;

if ( ( $old_w <= $new_w ) && ( $old_h <= $new_h ) ) {
     $thumb_w = $old_w;
     $thumb_h = $old_h;
}
elseif ( $y_ratio <= $x_ratio ) {
     $thumb_w = round ( $old_w * $y_ratio );
     $thumb_h = round ( $old_h * $y_ratio );
}
else {
     $thumb_w = round ( $old_w * $x_ratio );
     $thumb_h = round ( $old_h * $x_ratio );
}


What's wrong with you guys
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@steelseth12

Indeed, i tought this would never work.
But still incomplete, for e.g. old_width 10 and old_height 10,
but yes ... that's just an if condition ...
@matthew016

You being a  java expert and asking for the algorithm i provided just that,  and thought you could manage from there on ... which it seems now you can .

>>But still incomplete, for e.g. old_width 10 and old_height 10,

that wasnt in the original post and i didnt feel like making another effort after you ignored me the first time.

Anyways thanks for the points.
Sorry you are right, anyway i accepted your solution.
Thank you.