php - Include functions file in Code Igniter -


i'm new ci include code below (which sits in file called color.php) within view , reference functions. doesn't seem work when per normal php include include('/assets/color.php'); i've tried adding color.php helper , loading in view again not working … any on how best include great.

class getmostcommoncolors {     var $preview_width    = 150;     var $preview_height   = 150;      var $error;      /**      * returns colors of image in array, ordered in descending order, keys colors, , values count of color.      *      * @return array      */     function get_color( $img, $count=20, $reduce_brightness=true, $reduce_gradients=true, $delta=16 )     {         if (is_readable( $img ))         {             if ( $delta > 2 )             {                 $half_delta = $delta / 2 - 1;             }             else             {                 $half_delta = 0;             }             // have resize image, because need significant colors.             $size = getimagesize($img);             $scale = 1;             if ($size[0]>0)             $scale = min($this->preview_width/$size[0], $this->preview_height/$size[1]);             if ($scale < 1)             {                 $width = floor($scale*$size[0]);                 $height = floor($scale*$size[1]);             }             else             {                 $width = $size[0];                 $height = $size[1];             }             $image_resized = imagecreatetruecolor($width, $height);             if ($size[2] == 1)             $image_orig = imagecreatefromgif($img);             if ($size[2] == 2)             $image_orig = imagecreatefromjpeg($img);             if ($size[2] == 3)             $image_orig = imagecreatefrompng($img);             // need nearest neighbor resizing, because doesn't alter colors             imagecopyresampled($image_resized, $image_orig, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);             $im = $image_resized;             $imgwidth = imagesx($im);             $imgheight = imagesy($im);             $total_pixel_count = 0;             ($y=0; $y < $imgheight; $y++)             {                 ($x=0; $x < $imgwidth; $x++)                 {                     $total_pixel_count++;                     $index = imagecolorat($im,$x,$y);                     $colors = imagecolorsforindex($im,$index);                     // round colors, reduce number of duplicate colors                     if ( $delta > 1 )                     {                         $colors['red'] = intval((($colors['red'])+$half_delta)/$delta)*$delta;                         $colors['green'] = intval((($colors['green'])+$half_delta)/$delta)*$delta;                         $colors['blue'] = intval((($colors['blue'])+$half_delta)/$delta)*$delta;                         if ($colors['red'] >= 256)                         {                             $colors['red'] = 255;                         }                         if ($colors['green'] >= 256)                         {                             $colors['green'] = 255;                         }                         if ($colors['blue'] >= 256)                         {                             $colors['blue'] = 255;                         }                      }                      $hex = substr("0".dechex($colors['red']),-2).substr("0".dechex($colors['green']),-2).substr("0".dechex($colors['blue']),-2);                      if ( ! isset( $hexarray[$hex] ) )                     {                         $hexarray[$hex] = 1;                     }                     else                     {                         $hexarray[$hex]++;                     }                 }             }              // reduce gradient colors             if ( $reduce_gradients )             {                 // if want *eliminate* gradient variations use:                 // ksort( $hexarray );                 arsort( $hexarray, sort_numeric );                  $gradients = array();                 foreach ($hexarray $hex => $num)                 {                     if ( ! isset($gradients[$hex]) )                     {                         $new_hex = $this->_find_adjacent( $hex, $gradients, $delta );                         $gradients[$hex] = $new_hex;                     }                     else                     {                         $new_hex = $gradients[$hex];                     }                      if ($hex != $new_hex)                     {                         $hexarray[$hex] = 0;                         $hexarray[$new_hex] += $num;                     }                 }             }              // reduce brightness variations             if ( $reduce_brightness )             {                 // if want *eliminate* brightness variations use:                 // ksort( $hexarray );                 arsort( $hexarray, sort_numeric );                  $brightness = array();                 foreach ($hexarray $hex => $num)                 {                     if ( ! isset($brightness[$hex]) )                     {                         $new_hex = $this->_normalize( $hex, $brightness, $delta );                         $brightness[$hex] = $new_hex;                     }                     else                     {                         $new_hex = $brightness[$hex];                     }                      if ($hex != $new_hex)                     {                         $hexarray[$hex] = 0;                         $hexarray[$new_hex] += $num;                     }                 }             }              arsort( $hexarray, sort_numeric );              // convert counts percentages             foreach ($hexarray $key => $value)             {                 $hexarray[$key] = (float)$value / $total_pixel_count;             }              if ( $count > 0 )             {                 // works in php5                 // return array_slice( $hexarray, 0, $count, true );                  $arr = array();                 foreach ($hexarray $key => $value)                 {                     if ($count == 0)                     {                         break;                     }                     $count--;                     $arr[$key] = $value;                 }                 return $arr;             }             else             {                 return $hexarray;             }          }         else         {             $this->error = "image ".$img." not exist or unreadable";             return false;         }     }      function _normalize( $hex, $hexarray, $delta )     {         $lowest = 255;         $highest = 0;         $colors['red'] = hexdec( substr( $hex, 0, 2 ) );         $colors['green']  = hexdec( substr( $hex, 2, 2 ) );         $colors['blue'] = hexdec( substr( $hex, 4, 2 ) );          if ($colors['red'] < $lowest)         {             $lowest = $colors['red'];         }         if ($colors['green'] < $lowest )         {             $lowest = $colors['green'];         }         if ($colors['blue'] < $lowest )         {             $lowest = $colors['blue'];         }          if ($colors['red'] > $highest)         {             $highest = $colors['red'];         }         if ($colors['green'] > $highest )         {             $highest = $colors['green'];         }         if ($colors['blue'] > $highest )         {             $highest = $colors['blue'];         }          // not normalize white, black, or shades of grey unless low delta         if ( $lowest == $highest )         {             if ($delta <= 32)             {                 if ( $lowest == 0 || $highest >= (255 - $delta) )                 {                     return $hex;                 }             }             else             {                 return $hex;             }         }          (; $highest < 256; $lowest += $delta, $highest += $delta)         {             $new_hex = substr("0".dechex($colors['red'] - $lowest),-2).substr("0".dechex($colors['green'] - $lowest),-2).substr("0".dechex($colors['blue'] - $lowest),-2);              if ( isset( $hexarray[$new_hex] ) )             {                 // same color, different brightness - use instead                 return $new_hex;             }         }          return $hex;     }      function _find_adjacent( $hex, $gradients, $delta )     {         $red = hexdec( substr( $hex, 0, 2 ) );         $green  = hexdec( substr( $hex, 2, 2 ) );         $blue = hexdec( substr( $hex, 4, 2 ) );          if ($red > $delta)         {             $new_hex = substr("0".dechex($red - $delta),-2).substr("0".dechex($green),-2).substr("0".dechex($blue),-2);             if ( isset($gradients[$new_hex]) )             {                 return $gradients[$new_hex];             }         }         if ($green > $delta)         {             $new_hex = substr("0".dechex($red),-2).substr("0".dechex($green - $delta),-2).substr("0".dechex($blue),-2);             if ( isset($gradients[$new_hex]) )             {                 return $gradients[$new_hex];             }         }         if ($blue > $delta)         {             $new_hex = substr("0".dechex($red),-2).substr("0".dechex($green),-2).substr("0".dechex($blue - $delta),-2);             if ( isset($gradients[$new_hex]) )             {                 return $gradients[$new_hex];             }         }          if ($red < (255 - $delta))         {             $new_hex = substr("0".dechex($red + $delta),-2).substr("0".dechex($green),-2).substr("0".dechex($blue),-2);             if ( isset($gradients[$new_hex]) )             {                 return $gradients[$new_hex];             }         }         if ($green < (255 - $delta))         {             $new_hex = substr("0".dechex($red),-2).substr("0".dechex($green + $delta),-2).substr("0".dechex($blue),-2);             if ( isset($gradients[$new_hex]) )             {                 return $gradients[$new_hex];             }         }         if ($blue < (255 - $delta))         {             $new_hex = substr("0".dechex($red),-2).substr("0".dechex($green),-2).substr("0".dechex($blue + $delta),-2);             if ( isset($gradients[$new_hex]) )             {                 return $gradients[$new_hex];             }         }          return $hex;     } } 

// loading library within view

$this->load->library('getmostcommoncolors'); $ex = new getmostcommoncolors(); $colors=$ex->get_color($artwork->large_image, 5); 

put file library folder , load the library using code

$this->load->library('getmostcommoncolors'); 

this link you..

library detail documentation


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -