Topic: PHP and thumbnail images (imagecopyresampled)

Hey

I'm writing a little photo tagging app for our office as our pastor is sick of not being able to find photos he takes on mission trips etc. I need to create a thumbnail of an image using php and have done it using the code below however this just takes the image and scales it so that the width is 300px and the height is variable - what I would like to do is resize so that the width is 300px and then crop the height at 200px but have the middle 200px of the image showing instead of 200px from the top left.

I'm sure the solution is right in front of me and is hanging round the imagecopyresampled function but for some reason my brain isn't working well enough for me to grasp the solution. Hopefully you guys can help

Thanks

function make_thumbnail($file, $path, $tn_path){
    $new_height = 200;
    $new_width = 300;
    //make a thumbnail
    echo "<li>$file -> $path -> $tn_path";
    
    $srcimg = $path.$file;
    $thumb = $tn_path.$file;
    
    $img=imagecreatefromjpeg($srcimg);
    //works out scale or just moves image
    $height = imagesy($img);
    $width = imagesx($img);
    //work out scale rate
   // $scale = min($new_height/$height, $new_width/$width);
    $scale = ($new_width/$width);
    //if scale is less than 1 then the image needs to be reduced otherwise just move it
    if($scale>1){
      imagejpeg($img,$thumb,95);
    }else{
      //calculate new height and width using scale so that ratio is maintained
      $scaled_height = floor($scale*$height);
      $scaled_width = floor($scale*$width);
      //need to make sure our width is 300px!
      #Create a new temporary image
      $tmpimg = imagecreatetruecolor($scaled_width, $scaled_height);
      # Copy and resize old image into new image
      imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $scaled_width, $scaled_height, $width, $height);

      imagedestroy($img);
      $img = $tmpimg;
      
      //save file
      imagejpeg($img,$thumb,95);
    }
}

Re: PHP and thumbnail images (imagecopyresampled)

There may be an easier way to do it, but here's something I put together when I was struggling with the same issue.  This will work no matter what dimensions you give it.  The key to the whole thing was find the right values for $offset_x and $offset_y

$thumb_x = '200';
$thumb_y = '300';
$thumb_width = $thumb_x;
$thumb_height = $thumb_y;

$offset_x = 0;
$offset_y = 0;

$thumb = imagecreatetruecolor($thumb_width,$thumb_height);

if ($w/$thumb_width > $h/$thumb_height) {
        $new_thumb_width = round(($w/$h)*$thumb_height);
        $ratio = $w/$new_thumb_width;
        $offset_x = round((($new_thumb_width - $thumb_width)/2)*$ratio);
        $thumb_width = $new_thumb_width;
        }
elseif ($h/$thumb_height > $w/$thumb_width) {
        $new_thumb_height = round(($h/$w)*$thumb_width);
        $ratio = $h/$new_thumb_height;
        $offset_y = round((($new_thumb_height - $thumb_height)/2)*$ratio);
        $thumb_height = $new_thumb_height;
        }

imagecopyresampled($thumb, $img2, 0, 0, $offset_x, $offset_y, $thumb_width, $thumb_height, $w, $h);
ImageJPEG($thumb,$tpath,50);
imagedestroy($thumb);

You may have to tweak it some...this is just a cut and paste from a larger bunch of code (creating a new picture gallery).  Hope this helps!

Re: PHP and thumbnail images (imagecopyresampled)

Wow! You are incredibly good to your senior pastor! Why not just shove a copy of FastStone on his machine? That's a Windows app, but there must be things like this for any platform!

Used in combination with FastStone's JPEG comment editor (or any other one) and CRead (low-tech!), you'd have both visual and textual ability to search/sort/save JPG's.

More likely, you are doing something more sophisticated/specialized, but I thought it might be worth suggesting this, "just in case"!

Last edited by DjR (2008-11-13 09:47:07)

David Reimer  //  ↪ "If you know the resurrection is coming, it's impossible to be in utter darkness." — Tim Keller ↩

Re: PHP and thumbnail images (imagecopyresampled)

BroChris - i managed to take your code and get it working. Thanks loads - this was making my brain hurt!

DJR - FastStone would have been close to perfect but  I need the app to work on more than 1 machine - a web based solution makes it much easier for me to roll it out across our admittedly small network and make it available for everyone in our office. Plus it looked a little more complicated than I would like (keep it simple is the axiom flying round our office at the minute). Plus it gives me more flexibility in the future when someone decides to add a feature! and I am good to our senior pastor although he might disagree at times ;-)

Re: PHP and thumbnail images (imagecopyresampled)

I see you've got it all figure out now, but I just wanted to throw this out there as well.  A friend of mine wrote a great PHP class to do exactly this, you can find it here: http://www.lovemikeg.com/blog/2007/03/1 … generator/

A while back when I wrote my own CMS - I used this a lot for image gallery thumbnails - it's great.

Re: PHP and thumbnail images (imagecopyresampled)

mrbelfry wrote:

... I need the app to work on more than 1 machine - a web based solution makes it much easier ...

Ahh, well yes -- that makes quite a difference. Glad you got your solution!

David Reimer  //  ↪ "If you know the resurrection is coming, it's impossible to be in utter darkness." — Tim Keller ↩

Re: PHP and thumbnail images (imagecopyresampled)

DjR wrote:
mrbelfry wrote:

... I need the app to work on more than 1 machine - a web based solution makes it much easier ...

Ahh, well yes -- that makes quite a difference. Glad you got your solution!

We don't have that many machines in the office but I like to keep my movements to a minimum - i'm trying to be energy efficient and burn as few calories as possible. Plus I'd rather develop our own system in houseso that it fits how we work rather than having to adapt to how the software works