[英]How To Use ImageTool Component for CakePHP
我正在做一个CakePHP项目,在这里我需要应用一些图像转换。 我选择尝试以下一种方法: https : //github.com/raitisg/CakePHP-ImageTool-Component 。 但是,即使像这样的github页面上给出的示例,我似乎也无法弄清楚如何使用它:
$status = ImageTool::resize(array(
'input' => $input_file,
'output' => $output_file,
'width' => 600,
'height' => 600,
'keepRatio' => true,
'paddings' => false,
'afterCallbacks' => array(
array('watermark', array('watermark' => $watermark_file, 'position' => 'bottom-right')),
array('unsharpMask'),
)
));
我想在控制器中使用ImageTool,在该控制器中浏览包含图像的目录,这些图像将被转换并复制到另一个目录(及其路径进入数据库)。 我知道上面的示例是一个静态调用,但是$ status在这里意味着什么? 我应该在哪里设置$ input_file,$ output_file和$ watermark_file?
我在CakePHP网站上找到了ImageTool,但找不到任何现实示例。
您必须将组件添加到控制器的$component
属性中。 您也可以通过debug()
查看$status
内部。
尝试这个:
<?php
class TestController extends AppController {
public $components = array('ImageTool');
public function index() {
$status = $this->ImageTool->resize(array(
'input' => 'my-original-photo.jpg',
'output' => 'my-resized-photo.jpg',
'width' => 600,
'height' => 600,
'keepRatio' => true,
'paddings' => false,
'afterCallbacks' => array(
array('watermark', array('watermark' => 'watermark.jpg', 'position' => 'bottom-right')),
array('unsharpMask'),
)
));
debug($status);
}
}
您已经在我的存储库中获得了所有用法和配置信息,并进行了一些改进!
https://bitbucket.org/gestudio/cakephp-2-imagetoolcomponent
例子
首先包括该类:
App::import('Vendor', 'ImageTool');
使缩略图尺寸为100x100像素
$status = ImageTool::resize(array(
'input' => $input_file,
'output' => $output_file,
'width' => 100,
'height' => 100
));
调整图像大小(同时保持比例),最大宽度和高度均设置为600px。 之后,在右下角放置水印图像并锐化最终结果(使用默认设置)
$status = ImageTool::resize(array(
'input' => $input_file,
'output' => $output_file,
'width' => 600,
'height' => 600,
'keepRatio' => true,
'paddings' => false,
'afterCallbacks' => array(
array('watermark', array('watermark' => $watermark_file, 'position' => 'bottom-right')),
array('unsharpMask'),
)
));
自动旋转图像(获取GD资源,然后将其传递给下一个函数(灰度)-与上一个示例类似,但不使用afterCallbacks选项)
$image = ImageTool::autorotate(array(
'input' => $input_file,
'output' => null
));
if ($image) {
$status = ImageTool::grayscale(array(
'input' => $image,
'output' => $output_file
));
} else {
$status = false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.