简体   繁体   English

如何为CakePHP使用ImageTool组件

[英]How To Use ImageTool Component for CakePHP

I'm working on a CakePHP project where I need to apply some image transformations. 我正在做一个CakePHP项目,在这里我需要应用一些图像转换。 I chose to try this one: https://github.com/raitisg/CakePHP-ImageTool-Component . 我选择尝试以下一种方法: https : //github.com/raitisg/CakePHP-ImageTool-Component However, I can't seem to figure out how to use it even by the examples given on the github-page like this one: 但是,即使像这样的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'),
    )
));

I want to use ImageTool in a Controller where I run through a directory with images that shall get transformed and copied to another directory (and their paths into a database). 我想在控制器中使用ImageTool,在该控制器中浏览包含图像的目录,这些图像将被转换并复制到另一个目录(及其路径进入数据库)。 I know the above example is a static call, but what does $status mean here? 我知道上面的示例是一个静态调用,但是$ status在这里意味着什么? Where am I supposed to set $input_file, $output_file and $watermark_file? 我应该在哪里设置$ input_file,$ output_file和$ watermark_file?

I found ImageTool referenced on the CakePHP site, but couldn't find any real life examples. 我在CakePHP网站上找到了ImageTool,但找不到任何现实示例。

You have to add your component to the $component property of the controller. 您必须将组件添加到控制器的$component属性中。 Also you can take a look inside of $status via debug() . 您也可以通过debug()查看$status内部。

Try this: 尝试这个:

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

    }

}

You've got all the usage and config info in my repo, some improvements applied! 您已经在我的存储库中获得了所有用法和配置信息,并进行了一些改进!

https://bitbucket.org/gestudio/cakephp-2-imagetoolcomponent https://bitbucket.org/gestudio/cakephp-2-imagetoolcomponent

Examples 例子

First include the class: 首先包括该类:

App::import('Vendor', 'ImageTool');

Make thumbnail 100x100px in size 使缩略图尺寸为100x100像素

$status = ImageTool::resize(array(
    'input' => $input_file,
    'output' => $output_file,
    'width' => 100,
    'height' => 100
));

Resize image (while keeping ratio) with max width and height both set to 600px. 调整图像大小(同时保持比例),最大宽度和高度均设置为600px。 After that place watermark image in bottom-right corner and sharpen end result (with default settings) 之后,在右下角放置水印图像并锐化最终结果(使用默认设置)

$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'),
    )
));

Autorotate image (getting back GD resource and then passing it to the next function (greyscale) - similar to previous example but without using afterCallbacks option) 自动旋转图像(获取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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM