1

How do you create a fixed size (height / width) of images/thumbnails In GD?

I know there is a lot of php scripts out there but that just scale it and height/width will always be different size.

I like the thumbnail like twitpic.com and facebook

user622378
  • 2,318
  • 6
  • 42
  • 63
  • 1
    possible duplicate [ImageMagick or GD Libary for image resizing and thumbnail creation?](http://stackoverflow.com/questions/4277823/imagemagick-or-gd-libary-for-image-resizing-and-thumbnail-creation) – seriousdev Mar 09 '11 at 23:35
  • possible duplicate of [Resize panoramic image to fixed size](http://stackoverflow.com/questions/5157998/resize-panoramic-image-to-fixed-size) – CanSpice Mar 09 '11 at 23:35

2 Answers2

0

You need to get the height and width of the image using getimagesize

and then resize it using imagecopyresized

The rest is all the same basic work done with GD to load and save the image.

Here's a basic example, if you want to take into account height/width ratios, then you have to do some additional maths.

<?php
header("Content-type: image/png");

$size = getimagesize($filename);
$image     = imagecreatefrompng($filename);
$thumbnail = imagecreate(100,100);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, 100, 100, $size[0], $size[1]);
imagepng($thumbnail);
imagedestroy($image);
imagedestroy($thumbnail);
Jacob
  • 8,278
  • 1
  • 23
  • 29
  • Thanks but I do know about this. Image look terrible when it resize to fixed hight and width. Need a way to crop specific area and then resize it. Just like twitpic.com – user622378 Mar 09 '11 at 23:54
  • if you want it to look proportionate then you have to get copy from a portion of the image that has the same height/width ratio. e.g. to make a 100x100 thumbnail from a 200x300 image, you need to take 200x200 of that image. Is this what you mean? – Jacob Mar 10 '11 at 01:41
0

It's easy with Thumbnailer:

$th=new Thumbnailer("your-photo.jpg");
$th->thumbSquare(100)->save("thumb.jpg");
user644602
  • 44
  • 2