Imagemagick -一次缩小/裁剪/保存为jpeg


imagemagick - downsize/crop/save to jpeg in one go?

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

我需要做的是创建每个小缩略图,在飞行中使用imagemagick。

我可以很容易地做到这一点,但我还需要裁剪它,使缩略图始终是100x100。

提供的图像不会是相同的比例,所以简单的缩小是行不通的。

我可以缩小,裁剪到100x100,并保存为jpeg所有在一个步骤?

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

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

resize将缩小,extent(与gravity结合)将裁剪,其余部分将以修改后的名称以JPEG格式保存在不同的目录中。

简短的回答:不。总共3步。

长一点的回答:你可以用命令行界面来做。在PHP中,唯一的方法是编写一个函数来执行您的要求。然后,对于每个图像,你可以调用你的函数。我不确定这是否比单独使用3个Imagick函数更有益。

我喜欢sfThumbnailPlugin。它同时包裹ImageMagick或GD

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

的例子:

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