我需要在Zend Framework 2中实现图像调整功能(最好使用gd2库扩展)。我无法找到任何相应的组件/助手。您有相关参考资料吗?
如果我想要创建一个,应该在哪里添加?在旧的Zend框架中,有一个Action Helper的概念,那么在Zend Framework 2中呢?
请给出最佳解决方案建议。
如果我想要创建一个,应该在哪里添加?在旧的Zend框架中,有一个Action Helper的概念,那么在Zend Framework 2中呢?
请给出最佳解决方案建议。
我目前使用Imagine和Zend Framework 2来处理这个。
php composer.phar require imagine/Imagine:0.3.*
为 Imagine
服务创建一个服务工厂(在 YourModule::getServiceConfig
中):
return array(
'invokables' => array(
// defining it as invokable here, any factory will do too
'my_image_service' => 'Imagine\Gd\Imagine',
),
);
将其用于你的逻辑中(这里提供一个使用控制器的小例子):
public function imageAction()
{
$file = $this->params('file'); // @todo: apply STRICT validation!
$width = $this->params('width', 30); // @todo: apply validation!
$height = $this->params('height', 30); // @todo: apply validation!
$imagine = $this->getServiceLocator()->get('my_image_service');
$image = $imagine->open($file);
$transformation = new \Imagine\Filter\Transformation();
$transformation->thumbnail(new \Imagine\Image\Box($width, $height));
$transformation->apply($image);
$response = $this->getResponse();
$response->setContent($image->get('png'));
$response
->getHeaders()
->addHeaderLine('Content-Transfer-Encoding', 'binary')
->addHeaderLine('Content-Type', 'image/png')
->addHeaderLine('Content-Length', mb_strlen($imageContent));
return $response;
}
显然,这是一种“快速而简单”的方法,因为您应该执行以下操作(可选但对于可重用性很有好处):
Imagine
的人,我找到了另一个解决方案WebinoImageThumb,对我来说完美地解决了问题。如果您不想阅读完整的文档,请看以下简要说明:php composer.phar require webino/webino-image-thumb:dev-develop
并将WebinoImageThumb
添加为config/application.config.php
中的活动模块,后者看起来类似于:<?php
return array(
// This should be an array of module namespaces used in the application.
'modules' => array(
'Application',
'WebinoImageThumb'
),
现在在您的控制器操作中使用此代码:
// at top on your controller
use Zend\Validator\File\Size;
use Zend\Validator\File\ImageSize;
use Zend\Validator\File\IsImage;
use Zend\Http\Request
// in action
$file = $request->getFiles();
$fileAdapter = new \Zend\File\Transfer\Adapter\Http();
$imageValidator = new IsImage();
if ($imageValidator->isValid($file['file_url']['tmp_name'])) {
$fileParts = explode('.', $file['file_url']['name']);
$filter = new \Zend\Filter\File\Rename(array(
"target" => "file/path/to/image." . $fileParts[1],
"randomize" => true,
));
try {
$filePath = $filter->filter($file['file_url'])['tmp_name'];
$thumbnailer = $this->getServiceLocator()
->get('WebinoImageThumb');
$thumb = $thumbnailer->create($filePath, $options = [], $plugins = []);
$thumb->adaptiveResize(540, 340)->save($filePath);
} catch (\Exception $e) {
return new ViewModel(array('form' => $form,
'file_errors' => array($e->getMessage())));
}
} else {
return new ViewModel(array('form' => $form,
'file_errors' => $imageValidator->getMessages()));
}
Good luck..!!
使用一个服务来实现这个功能,并将其注入到需要该功能的控制器中。
这里有一个名为WebinoImageThumb的模块,它是Zend Framework 2中的一部分。请查看此模块。它具有以下一些很棒的功能:
public function imageAction()
{
// ...
$imagine = $this->getImagineService();
$size = new \Imagine\Image\Box(150, 150);
$mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET;
$image = $imagine->open($destinationPath);
$image->thumbnail($size, $mode)->save($destinationPath);
// ...
}
public function getImagineService()
{
if ($this->imagineService === null)
{
$this->imagineService = $this->getServiceLocator()->get('my_image_service');
}
return $this->imagineService;
}