Welcome Guest, Not a member yet? Register   Sign In
Setting preferences in a config file
#1

[eluser]huzzi[/eluser]
I would like to put the image resizing preferences in a config file as per user guide.

Quote:If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called image_lib.php, add the $config array in that file. Then save the file in: config/image_lib.php and it will be used automatically. You will NOT need to use the $this->image_lib->initialize function if you save your preferences in a config file.

My question is how would I set the image_source inside the controller using the above method?

Code:
$resize['source_image'] = $source_image;

Any suggestion?

Many thanks.
#2

[eluser]Thorpe Obazee[/eluser]
If you are to upload different files every time, I doubt you'll be setting the source image in the config file. You'll set them in the controller like this:

Code:
'source_image'   => './image_folder/image.jpg'
#3

[eluser]huzzi[/eluser]
How would you pass this to the library?

[quote author="bargainph" date="1242807812"]If you are to upload different files every time, I doubt you'll be setting the source image in the config file. You'll set them in the controller like this:

Code:
'source_image'   => './image_folder/image.jpg'
[/quote]
#4

[eluser]Dam1an[/eluser]
I would assume you would pass that as you did before
Any config items which you set in the controller will probably override the ones in the config, but the ones you don't set in the controller will default to the config values (I assume)
#5

[eluser]xwero[/eluser]
AFAIK when a library is loaded it checks if the settings are added to the load method or if a config file is found.
Code:
function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
{
   // Is there an associated config file for this class?
   if ($config === NULL)
   {
      // We test for both uppercase and lowercase, for servers that
     // are case-sensitive with regard to file names
     if (file_exists(APPPATH.'config/'.strtolower($class).EXT))
     {
        include_once(APPPATH.'config/'.strtolower($class).EXT);
     }
     else
     {
        if (file_exists(APPPATH.'config/'.ucfirst(strtolower($class)).EXT))
        {
           include_once(APPPATH.'config/'.ucfirst(strtolower($class)).EXT);
        }
     }
  }
  // ...


A quick workaround would be to load a custom config file for the class, or other settings in the case of the example below
Code:
<?php
$config['image_lib'] = array('resize'=>array(
                                'image_library' => 'gd2',
                                'create_thumb' => TRUE,
                                'maintain_ratio' => TRUE,
                                'width' = 75,
                                'height' = 50,
                                )
                        );
And in your controller you do
Code:
$this->load->config('settings');
$settings = $this->config->item('image_lib');

$resize = array();
$resize['source_image'] = $source_image;

$this->load->library('image_lib', array_merge($settings['resize'],$resize));

$this->image_lib->resize();

A permanent solution could be to remove the null check, change the config parameter to settings and add following code after the inclusion
Code:
if(is_array($settings) && is_array($config))
{
   $config = array_merge($config,$settings);
}
Then you can have a config file and in the controller do
Code:
$resize = array();
$resize['source_image'] = $source_image;

$this->load->library('image_lib', $resize);
But then you have a problem with the different settings in the image_lib class. What if you want a different image processing library for resizing and rotation?
#6

[eluser]huzzi[/eluser]
Thanks alot, I'll give this a try.

Code:
$this->load->config('settings');
$settings = $this->config->item('image_lib');

$resize = array();
$resize['source_image'] = $source_image;

$this->load->library('image_lib', array_merge($settings['resize'],$resize));

$this->image_lib->resize();
#7

[eluser]huzzi[/eluser]
Thanks alot for that.

[quote author="xwero" date="1242839573"]AFAIK when a library is loaded it checks if the settings are added to the load method or if a config file is found.
Code:
function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
{
   // Is there an associated config file for this class?
   if ($config === NULL)
   {
      // We test for both uppercase and lowercase, for servers that
     // are case-sensitive with regard to file names
     if (file_exists(APPPATH.'config/'.strtolower($class).EXT))
     {
        include_once(APPPATH.'config/'.strtolower($class).EXT);
     }
     else
     {
        if (file_exists(APPPATH.'config/'.ucfirst(strtolower($class)).EXT))
        {
           include_once(APPPATH.'config/'.ucfirst(strtolower($class)).EXT);
        }
     }
  }
  // ...


A quick workaround would be to load a custom config file for the class, or other settings in the case of the example below
Code:
<?php
$config['image_lib'] = array('resize'=>array(
                                'image_library' => 'gd2',
                                'create_thumb' => TRUE,
                                'maintain_ratio' => TRUE,
                                'width' = 75,
                                'height' = 50,
                                )
                        );
And in your controller you do
Code:
$this->load->config('settings');
$settings = $this->config->item('image_lib');

$resize = array();
$resize['source_image'] = $source_image;

$this->load->library('image_lib', array_merge($settings['resize'],$resize));

$this->image_lib->resize();

A permanent solution could be to remove the null check change the config parameter to settings and add following code after the inclusion
Code:
if(is_array($settings) && is_array($config))
{
   $config = array_merge($config,$settings);
}
Then you can have a config file and in the controller do
Code:
$resize = array();
$resize['source_image'] = $source_image;

$this->load->library('image_lib', $resize);
But then you have a problem with the different settings in the image_lib class. What if you want a different image processing library for resizing and rotation?[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB