imagemagick - 一次性缩小/裁剪/保存为 jpeg?

发布于 2024-11-03 16:00:08 字数 201 浏览 1 评论 0原文

我有一个 php 脚本,它将接收一堆上传的图像。

我需要做的是使用 imagemagick 即时创建每个的小缩略图。

我可以很容易地做到这一点,但我还需要对其进行裁剪,以便缩略图始终为 100x100。

提供的图像比例不同,因此简单地缩小尺寸是行不通的。

我可以一步缩小尺寸、裁剪为 100x100 并保存为 jpeg 吗?

I have a php script that will receive a bunch of images uploaded.

What I need to do is create a small thumbnail of each, on the fly using imagemagick.

I can do that easy enough but I also need to crop it so that the thumbnail is always 100x100.

the images supplied won't be the same proportions so simply downsizing won't work.

Can I downsize, crop to 100x100 and save to jpeg all in one step?

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

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

发布评论

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

评论(3

海夕 2024-11-10 16:00:08

我相信这应该做你想要的:

convert 'just_uploaded/*' -resize 100x100^ -gravity center -extent 100x100 -set filename:f '%t' +adjoin 'just_uploaded_thumbs/%[filename:f].jpg'

resize将缩小尺寸,extent(与gravity结合)将裁剪,其余的就完成了使用修改后的名称以 JPEG 格式保存在不同的目录中。

I believe this should do what you want:

convert 'just_uploaded/*' -resize 100x100^ -gravity center -extent 100x100 -set filename:f '%t' +adjoin 'just_uploaded_thumbs/%[filename:f].jpg'

resize will downsize, extent (in combination with gravity) will crop, and the rest takes care of saving with a modified name, in JPEG format, in a different directory.

月光色 2024-11-10 16:00:08

简短的回答:不。这将是 3 个步骤,不少于。

更长的答案:您可以使用命令行界面来完成此操作。在 PHP 中,唯一的方法是编写一个函数来执行您所要求的操作。然后,对于每个图像,您只需调用您的函数即可。我不知道这比单独使用 3 个 Imagick 功能更有利......

Short answer: no. That'll be 3 steps, no less.

Longer answer: you can do it using the command line interface. In PHP, the only way is to write a function that will do what you ask. Then, for each image, you can just call your function. I'm not sure how this is more beneficial than just using the 3 Imagick functions separately...

韶华倾负 2024-11-10 16:00:08

我喜欢 sfThumbnailPlugin。它围绕 ImageMagick 或 GD

http://www.symfony-project.org/plugins/sfThumbnailPlugin< /a>

示例:

public function executeUpload()
{
  // Retrieve the name of the uploaded file
  $fileName = $this->getRequest()->getFileName('file');

  // Create the thumbnail
  $thumbnail = new sfThumbnail(150, 150);
  $thumbnail->loadFile($this->getRequest()->getFilePath('file'));
  $thumbnail->save(sfConfig::get('sf_upload_dir').'/thumbnail/'.$fileName, 'image/png');

  // Move the uploaded file to the 'uploads' directory
  $this->getRequest()->moveFile('file', sfConfig::get('sf_upload_dir').'/'.$fileName);

  // Do whatever is next
  $this->redirect('media/show?filename='.$fileName); 
}

I like the sfThumbnailPlugin. It wraps around both ImageMagick or GD

http://www.symfony-project.org/plugins/sfThumbnailPlugin

Example:

public function executeUpload()
{
  // Retrieve the name of the uploaded file
  $fileName = $this->getRequest()->getFileName('file');

  // Create the thumbnail
  $thumbnail = new sfThumbnail(150, 150);
  $thumbnail->loadFile($this->getRequest()->getFilePath('file'));
  $thumbnail->save(sfConfig::get('sf_upload_dir').'/thumbnail/'.$fileName, 'image/png');

  // Move the uploaded file to the 'uploads' directory
  $this->getRequest()->moveFile('file', sfConfig::get('sf_upload_dir').'/'.$fileName);

  // Do whatever is next
  $this->redirect('media/show?filename='.$fileName); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文