通过 PHP 通过表单提交后调整照片大小

发布于 2024-08-28 18:03:58 字数 142 浏览 4 评论 0原文

我正在开发一个将员工信息添加到 MySQL 表的表单,并且我需要将一张照片附加到该文件中。这是内容管理系统的一部分,用户不知道如何调整照片大小,因此我需要在上传后将图像大小调整为设定大小。如何使用 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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

囚我心虐我身 2024-09-04 18:03:58

您应该查看 Verot 的 php 上传类。它通过非常简单的编码处理所有图像修改选项。

示例代码:

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

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;
  }
}
最偏执的依靠 2024-09-04 18:03:58

您的服务器是否安装了 PHP 的 GD 库? (如果没有,您能安装它吗?)

PHP 具有广泛的处理图像数据的函数。

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

(如果您看完上面的内容仍然不知道该怎么做,请告诉我,我将添加更多信息)

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)

疑心病 2024-09-04 18:03:58

一个简单的方法是使用 timthumb。它可以根据用户请求裁剪、缩放图像并调整图像大小。

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

木緿 2024-09-04 18:03:58

大多数 PHP 安装都内置了 gd 库扩展。人们在这里遇到的典型问题是,他们尝试使用名称明显的 imagecopyresized() 函数(它可能会创建丑陋的图像),而不是使用 imagecopyresampled() ,后者会产生更好的输出。

如果您的服务器安装了 ImageMagick,您通常可以通过构建一个漂亮的 shell 命令来驱动 imagemagick 来获得更好的结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文