Welcome Guest, Not a member yet? Register   Sign In
MY_Image_lib
#1

[eluser]bralens[/eluser]
I want to extend CI_Image_lib class but how can i access to variables which are in that class from my child class?
for example i want to get image width and stuff like that.

Thanks Smile

P.S There was a problem in other part of code and $this->parent_variable works Smile
#2

[eluser]Aken[/eluser]
Any property of CI_Image_lib will be available in MY_Image_lib if you extend it properly. Not sure what you're asking. Try posting your code or something.
#3

[eluser]bralens[/eluser]
Hello, again.
So i was searching for thumbnail creating library, function etc for CI, but only found functions where photo fits in to passed dimensions, but i need where photo fills passed dimensions. So here is it.

Code:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

/**
* MY_Image_lib
*
* Class to extend CI native Image manipulation library.
*
* @package CodeIgniter
* @subpackage  Image_lib
* @author  Roberts Guļāns
*/

class MY_Image_lib extends CI_Image_lib
{

    // default back up
    var $def_height    = '100';
    var $def_width     = '100';

    public function __construct($props = array())
    {

        parent::__construct($props);

        log_message('debug', "MY Image Lib Class Initialized");
    }


    /**
     * Resize photo to fill dimension
     *
     * @access  public
     * @return  bool
     */

    public function fill()
    {
        $image_sizes = $this->_get_image_size($this->full_src_path);

        // get dimension ratios
        $photo_ratio     = $image_sizes['width'] / $image_sizes['height'];
        $dimension_ratio = $this->def_width / $this->def_height;

        // if ratios are equal
        if($photo_ratio == $dimension_ratio)
        {
            // can use resize to fill all area
            return $this->resize();
        }
        else
        {
            // if photo is more extended in width than area needed to fill
            if($photo_ratio < $dimension_ratio)
            {
                // on top and bottom photo will be beyond area borders
                $this->width = $this->def_width;
                $this->height = $this->def_width / $photo_ratio;
            }
            else
            {
                // on the sides photo will be beyond area borders
                $this->height = $this->def_height;
                $this->width = $this->def_height * $photo_ratio;
            }
            return $this->resize();                
        }
    }


    /**
     * Crop dead center
     *
     * @access  public
     * @return  bool
    */

    public function center_crop()
    {

        $image_sizes  = $this->_get_image_size($this->full_src_path);
        
        $this->x_axis = ( $image_sizes['width'] - $this->def_width ) / 2;
        $this->y_axis = ( $image_sizes['height'] - $this->def_height ) / 2;
        
        $this->width  = $this->def_width;
        $this->height = $this->def_height;
        

        return $this->crop();
    }


    /**
     * Create thumbnail using fill and center_crop functions
     *
     * @access  public
     * @param string $file full/path/to/photo.ext
     * @param int|string $width
     * @param int|string $height
     * @return  bool
     */

    public function thumbnail($width = '', $height = '')
    {

        if($width != '' && $height != '')
        {
            $this->def_width  = $width;
            $this->def_height = $height;
        }
        elseif($width != '' && $height == '')
        {
            // if passed width but dont have height
            // make it square
            $this->def_width  = $width;
            $this->def_height = $width;
        }
    
        if($this->fill())
        {
            $this->_my_clear(FALSE);
            if($this->center_crop())
            {
                return TRUE;
            }
        }

        return FALSE;
    }


    /**
     * Clear Image_lib variables and set new ones
     *
     * @access  public
     * @param bool $create_thumb create new file or not
    */

    private function _my_clear($create_thumb = TRUE)
    {
        $config['image_library'] = $this->image_library;
        $config['source_image']  = $this->full_dst_path;
        $config['width']         = $this->def_width;
        $config['height']        = $this->def_height;
        $config['create_thumb']  = $create_thumb;

        $this->clear();

        $this->initialize($config);
    }


    /**
     * Get the image sizes for an image
     * @access  public
     * @param string $image
     * @return array
     */
    public function _get_image_size($image)
    {
        $image_data = getimagesize($image);
        $image_data['width']  = $image_data[0];
        $image_data['height'] = $image_data[1];
        
        return $image_data;
    }

}

This is my first library, that i made. Smile

Initialization
Code:
$this->load->library('image_lib');

  $config['image_library']  = 'gd2';
  $config['source_image']   = 'full/path/to/photo.ext';
  $config['width']          = '120';
  $config['height']         = '120';

                // must add next two lines
  $config['def_width']      = $config['width'];
  $config['def_height']     = $config['height'];  

  $this->image_lib->initialize($config);

using: will use default values passed in initialization
Code:
$this->image_lib->thumbnail();

using: will create square, 100px width and height
Code:
$this->image_lib->thumbnail(100);

using: will create rectangle. width: 100 and height: 200
Code:
$this->image_lib->thumbnail(100, 200);




Theme © iAndrew 2016 - Forum software by © MyBB