views:

345

answers:

6

Hello guys. I was wondering if someone can help me with an image resize function written in PHP but one wich has to resize an image, but in a manner like PHPThumb does. So, if I set the width and height of the new image, the function must fit the uploaded image (and mantain aspect ratio) in the new width and height.

Any help is appreciated.

Thank you.

A: 

I would use GD or ImageMagick. Both great libraries to do that and more.

Pablo Santa Cruz
A: 

at least this is small (and useful): http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

The MYYN
Thanks. I will check it out.
Psyche
A: 

When i started learning OOP, i wroted this image class to practice myself.

It uses GD, ans surely can be improved, hope it helps.

edit: after 2 downvotes for a issue where i dont have any control, i put the class on pastie.

The class

The Example

Remember, the class works fine, but it was a practice in order to learn basics OOP.

I hope this will be ok, you lazy users ;)

at Christmas we are all better people

DaNieL
-1 for requiring me to login when tried to view your class
stereofrog
LOL i cant believe, im not the one who require you to login, that's how phpclasses.org works, not my fault.didnt you never seen that site?
DaNieL
phpclasses.org is one of the worst sites ever, so you basically earned your -1 for supporting it
stereofrog
and may i ask why it has a so bad reputation?
DaNieL
don't know about "reputation", it just sucksbtw your class looks good, downvote revoked ;-)
stereofrog
hahahah thanks, but now im pretty curious to know why phpclasses sucks.. is use just a couple of time, but didnt look that bad ;) i'll google for it!
DaNieL
+1  A: 
  • the resize then crop approach: compute the aspect ratio of the source image and use it as a constraint to create an intermediate image which is going to be larger than the destination image (either in width or height) - finally crop the intermediate image to the destination dimensions.

  • use seam carving! also called "liquid rescale" in ImageMagick (see also there)

If you can rely on ImageMagick being installed on your server, it offers many more features than GD.

Gregory Pakosz
A: 

from my understanding, OP's question is more about calculating new dimensions, rather than resizing

@Psyche, this is actually a simple arithmetic issue. Suppose you have an 640x480 image and want to show it in a 200x200 "box".

$sw = 640; $sh = 480;
$dw = 200; $dh = 200;

find out the aspect ratio of the box, compare it with that of original and calculate either width of height for the new image

$sr = $sw / $sh;
$dr = $dw / $dh;
if($sr > $dr)
    $dh = round($dw / $sr);
else
    $dw = round($dh * $sr);

this gives you 200x150 and this is the size to which you need to scale the original image

stereofrog
Got it. Thanks mate!
Psyche
A: 

I wrote this a couple years ago and it does exactly what you're looking for. Keep in mind this only calculates the width and height, you must call Imagick yourself to actually apply these calculations.

/**
* ImageIntelligentResize()
* 
* @global Intelligently resizes images using a providid max width and height
* @param mixed $imagePath
* @param mixed $maxWidth
* @param mixed $maxHeight
* @param mixed $alwaysUpscale
* @return
*/
function ImageIntelligentResize( $imagePath, $maxWidth, $maxHeight, $alwaysUpscale )
{
    // garbage in, garbage out
    if ( IsNullOrEmpty($imagePath) || !is_file($imagePath) || IsNullOrEmpty($maxWidth) || IsNullOrEmpty($maxHeight) )
    {
     return array("width"=>"", "height"=>"");
    }

    // if our thumbnail size is too big, adjust it via HTML
    $size = getimagesize($imagePath);
    $origWidth = $size[0];
    $origHeight = $size[1];

    // Check if the image we're grabbing is larger than the max width or height or if we always want it resized
    if ( $alwaysUpscale || $origWidth > $maxWidth || $origHeight > $maxHeight )
    { 
     // it is so let's resize the image intelligently
     // check if our image is landscape or portrait
     if ( $origWidth > $origHeight )
     {
      // target image is landscape/wide (ex: 4x3)
      $newWidth = $maxWidth;
      $ratio = $maxWidth / $origWidth;
      $newHeight = floor($origHeight * $ratio);
      // make sure the image wasn't heigher than expected
      if ($newHeight > $maxHeight)
      {
       // it is so limit by the height
       $newHeight = $maxHeight;
       $ratio = $maxHeight / $origHeight;
       $newWidth = floor($origWidth * $ratio);
      }
     }
     else
     {
      // target image is portrait/tall (ex: 3x4)
      $newHeight = $maxHeight;
      $ratio = $maxHeight / $origHeight;
      $newWidth = floor($origWidth * $ratio);
      // make sure the image wasn't wider than expected
      if ($newWidth > $maxWidth)
      {
       // it is so limit by the width
       $newWidth = $maxWidth;
       $ratio = $maxWidth / $origWidth;
       $newHeight = floor($origHeight * $ratio);
      }
     }
    }
    // it's not, so just use the current height and width
    else
    {
     $newWidth = $origWidth;
     $newHeight = $origHeight;
    } 

    return array("width"=>$newWidth, "height"=>$newHeight);
}
TravisO