tags:

views:

44

answers:

4

I am working on a form that adds employee information to a MySQL table and I need to attach a single photo to the file. This is part of a Content Management System and the users do not know how to resize photos, so I need to resize the image to a set size after it is uploaded. How can this be done with PHP? Please note, I am not trying to accomplish a thumbnail image, simply scale the one that is uploaded.

+1  A: 

Does your server have the GD library for PHP installed? (if not, can you get it instlled?)

PHP has a wide range of functions for working with image data.

http://php.net/manual/en/book.image.php

(if you take a look at the above and still don't really know what to do, let me know and I will add some more information)

paullb
I do indeed have GD installed and I would like to know how to use it to handle this.. the manual is a bit hefty. Any details specific to this task would be very helpful
Benjamin
A: 

An easy way out is to use timthumb. It crops, zooms and resizes images on user request.

Haris
+1  A: 

You should check out Verot 's php upload class. It handles all image modification options through very simple coding.

Example code:

$foo = new Upload($_FILES['form_field']);
if ($foo->uploaded) {
  // save uploaded image with no changes
  $foo->Process('/home/user/files/');
  if ($foo->processed) {
    echo 'original image copied';
  } else {
    echo 'error : ' . $foo->error;
  }
  // save uploaded image with a new name
  $foo->file_new_name_body = 'foo';
  $foo->Process('/home/user/files/');
  if ($foo->processed) {
    echo 'image renamed "foo" copied';
  } else {
    echo 'error : ' . $foo->error;
  }
  // save uploaded image with a new name,
  // resized to 100px wide
  $foo->file_new_name_body = 'image_resized';
  $foo->image_resize = true;
  $foo->image_convert = gif;
  $foo->image_x = 100;
  $foo->image_ratio_y = true;
  $foo->Process('/home/user/files/');
  if ($foo->processed) {
    echo 'image renamed, resized x=100
          and converted to GIF';
    $foo->Clean();
  } else {
    echo 'error : ' . $foo->error;
  }
}
pixeline
Thanks, this looks like it may work pretty well.. :)
Benjamin
A: 

Most PHP installations have the gd library extension built-in. The typical issue people run into here is that they try to use the obviously-name imagecopyresized() function (which can create ugly images), instead of using imagecopyresampled(), which produces much nicer output.

If your server has ImageMagick installed, you can usually get somewhat nicer results by building up a nice shell command to drive imagemagick.

timdev