Or Weinberger
Or Weinberger

Reputation: 7482

PHP Image manipulation

I'm trying to figure out how imgur.com is building their gallery (the one on the homepage to the right).

My problem is that I would like to create a thumbnail gallery from the images I have, but I want the width/height ratio to be correct so the image would not look weird.

So I can't just take the uploaded image and change the height and width to 100px/100px as the ratio differs.

Do they crop the image, then create a thumbnail? How do they crop it? How do they set the crop ratio?

Thanks,

Upvotes: 1

Views: 313

Answers (3)

Imal Hasaranga Perera
Imal Hasaranga Perera

Reputation: 10029

This can be very easily done using inbuilt methods in the ImageArtist which is a pure gd OO wrapper.

advantage of using GD over Imagick is that GD extension is supported by wide range of hosting providers, so your code which works fine in local most of the time works fine after hosting.

here is a sample code.

$img = new Image("./cover.jpg");

$img->scaleToWidth(100);  // make the width 100px and make sure to resize height accroding to the ratio
$img->scaleToHeight(100);  // make the hegith 100px and make sure to resize width accroding to the ratio

/* if you want to crop the image */
$img->crop(5,5,90,90);  // this will crop a rectangle keeping 5px border

/* finally save it */
$img2->save("./final.png",IMAGETYPE_PNG);

Upvotes: 0

usoban
usoban

Reputation: 5478

Calculate the thumbnail size...

$sizes = getimagesize('img.jpg');

$w = $sizes[0];
$h = $sizes[1];

$new_w = 200; // some value
$new_h = $h * ($new_w / $w);

http://www.php.net/manual/en/function.getimagesize.php

Alternatively, if you'd like to crop the image, use some PHP library. I'd recommend WigeImage. Its docs are well written, and it offers easy cropping, resizing, mirroring, rotating, etc. :)

Note that WideImage and other image functions documented in PHP docs require GD extension installed on your server.

Upvotes: 4

markus
markus

Reputation: 40685

Install imagick on your local machine and play around with it, it can do all you're asking for. It doesn't help a lot to ask how does X or Y do it because every technology does it differently, in PHP (the tag you've chosen) you can do this with imagick.

Upvotes: 3

Related Questions