MAX POWER
MAX POWER

Reputation: 5438

Image Resizing and Compression

I'm implementing an image upload facility for my website. The uploading facility is complete but what I'm working on at the moment is manipulating the images. For this task I am using PHPThumb (http://phpthumb.gxdlabs.com).

Anyway as I go along I'm coming across potential issues, to do with resizing and compression. Basically I want to acheive the following results:

  1. The ideal image dimensions are: 800px width, 600px height. If an uploaded image exceeds either of these dimensions, it will need be resized to meet the requirements. Otherwise, leave as it is.

  2. The ideal file size is 200kb. If an uploaded image exceeds this then it will need to be compressed to meet this requirement. Otherwise, leave as it is.

So in a nutshell: 1) Check the dimensions, resize if required. 2) Check the filesize, compress if required.

Has anybody done anything like this / could you give me some pointers? Is PHPThumb the correct tool to do this in?

Upvotes: 2

Views: 3551

Answers (2)

Scorchio
Scorchio

Reputation: 2871

As long as I can see just from the docs, phpThumb should be good enough for this task. phpThumb can be used in an easy way like this:

Call phpThumb() just like you would a normal image. Examples:

<IMG SRC="phpThumb.php?src=/image.jpg&w=100">
<IMG SRC="phpThumb.php?src=http://example.com/foo.jpg">

See the "demo" link on http://phpthumb.sourceforge.net for more usage examples). Parameters that can be passed are listed below under "URL Parameters".

The phpThumb() website says that it has the following image-processing feature:

Quality can be auto-adjusted to fit a certain output byte size.

This looks like what you want. It works by passing a maximum byte size parameter. The readme tells you how:

maxb = MAXimum Byte size - output quality is auto-set to fit thumbnail into "maxb" bytes (compression quality is adjusted for JPEG, bit depth is adjusted for PNG and GIF)

edit: it seems that we were talking about different utilities with the same name. I did not found the same functionality in the other one, so I can only recommend using what I've found.

Upvotes: 4

Artefacto
Artefacto

Reputation: 97805

See the gd extension, in particular the functions getimagesize and imagecopyresized.

Enforcing a file size is more difficult, the best you can do is stick with jpeg and some arbitrary level of compression; for a certain combination of dimensions+compression, you can more or less predict the final file size of the image.

As to PHPThumb, I have no idea, as I've never used it.

Upvotes: 0

Related Questions