开发者

Resize a photo after being submitted with a form via PHP

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 开发者_如何学Cthe 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.


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;
  }
}


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)


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜