我需要在zend框架2中实现图像大小调整功能(最好使用gd2库扩展)。
我找不到相同的组件/帮助器。有没有参考?
如果我想创建一个,我应该在哪里添加它。在较旧的Zend框架中,有一个Action Helper的概念,那么Zend框架2呢?
请在此处提出最佳解决方案。
答案 0 :(得分:17)
我目前使用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;
}
这显然是“快速而肮脏”的方式,因为您应该遵循以下规则(可重复使用的可选但良好的做法):
答案 1 :(得分:2)
为此使用服务并将其注入需要该功能的控制器。
答案 2 :(得分:2)
以下是Zend Framework 2中名为WebinoImageThumb的模块。查看此内容。它有一些很棒的功能,如 -
答案 3 :(得分:2)
对于那些无法像我一样正确整合Imagine
的人..
我找到了另一个解决方案WebinoImageThumb here,它对我来说非常好。如果您不想阅读完整的文档,这里几乎没有解释:
运行: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()));
}
祝你好运.. !!
答案 4 :(得分:0)
为了在运行中调整上传图像的大小,您应该这样做:
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;
}