user2507316
user2507316

Reputation: 169

How to resize an image in YII?

When I try to load and resize an image:

$image = Yii::app()->image->load($path);
$image->resize(400, 100);

I get this error

Property "CWebApplication.image" is not defined.

I've tried in the config file to put this:

'image'=>array(
          'class'=>'application.extensions.image.CImageComponent',
            // GD or ImageMagick
            'driver'=>'GD',
            // ImageMagick setup path
            'params'=>array('directory'=>'/opt/local/bin'),
        ),

anyway I'am getting an error, how to resize an image in yii any ideas?, is there any native class to work with images?

Upvotes: 2

Views: 4914

Answers (3)

Mohan Kumar
Mohan Kumar

Reputation: 169

You must add the ImageMagick setup path in config file "main.php"

here the sample for windows

'image'=>array(
        'class'=>'application.extensions.image.CImageComponent',
        // GD or ImageMagick
        'driver'=>'GD',
        // ImageMagick setup path
        'params'=>array('directory'=>'D:/Program Files/ImageMagick-6.4.8-Q16'),
    ),

for linux

'image'=>array(
        'class'=>'application.extensions.image.CImageComponent',
        // GD or ImageMagick
        'driver'=>'GD',
        // ImageMagick setup path
        'params' => array('directory' => '/usr/bin'),
    ),

and then you can resize the image like this

$image = Yii::app()->image->load('images/test.jpg');
$image->resize(400, 100)->rotate(-45)->quality(75)->sharpen(20);
$image->save(); // or $image->save('images/small.jpg');

use the flowing extension image extension

Upvotes: 0

Mahendran Sakkarai
Mahendran Sakkarai

Reputation: 8549

I think its too late response. But it will help others. I have a got into a similar problem. I fixed it by the following method.

From your question i understand that you are using an extension called "image" in yii. In the page of the extension itself they given a description called "usage". I followed that to fix.

I have added some note in the usage documentation and added below which may lead to that issue.

The following code is the component registration in the config file(config/main.php):

'import'=>array(
    ...
    'application.helpers.*',
    ...
),

May be you may done mistake in this following part. you need to register the "image" component inside the components section in the config/main.php file.

'components'=>array(
       'image'=>array(
          'class'=>'application.extensions.image.CImageComponent',
            // GD or ImageMagick
            'driver'=>'GD',
            // ImageMagick setup path
            'params'=>array('directory'=>'/opt/local/bin'),
        ),...
)

See the following code example:

$image = Yii::app()->image->load('images/test.jpg');
$image->resize(400, 100)->rotate(-45)->quality(75)->sharpen(20);
$image->save(); // or $image->save('images/small.jpg');//You missed this line

(or) you can also use like below without registering the image component in the config/main.php file.

Yii::import('application.extensions.image.Image');
$image = new Image('images/test.jpg');
$image->resize(400, 100)->rotate(-45)->quality(75)->sharpen(20);
$image->render();

Upvotes: 1

user133408
user133408

Reputation:

When configuring application components put them in components config section, it seems that you put image component in wrong place.

This should work:

return [
    // ....
    'components' => [
        'image' => [
            'class' => 'application.extensions.image.CImageComponent',
            // GD or ImageMagick
            'driver' => 'GD',
            // ImageMagick setup path
            'params' => ['directory' => '/opt/local/bin'],
        ],
    ]
];

Upvotes: 3

Related Questions