我是CI的新手,但想在视图中包含下面的代码(位于名为color.php的文件中)并引用这些函数。但是,当我按照正常的PHP包括<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<script type="text/javascript">
$(function() {
var startDate;
var endDate;
var selectCurrentWeek = function() {
window.setTimeout(function () {
$('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active')
}, 1);
}
$('.week-picker').datepicker( {
showOtherMonths: true,
selectOtherMonths: true,
onSelect: function(dateText, inst) {
var date = $(this).datepicker('getDate');
var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat;
$('#startDate').text($.datepicker.formatDate( dateFormat, startDate, inst.settings ));
$('#endDate').text($.datepicker.formatDate( dateFormat, endDate, inst.settings ));
$(".days").empty();
for (var newDate = new Date(startDate); newDate <= endDate; newDate.setDate(newDate.getDate() + 1)) {
var currDay=days[newDate.getDay()];
var currDate=newDate.getDate();
$(".days").append('<label>'+currDay+' :'+currDate+'</label><br>');
}
selectCurrentWeek();
},
beforeShowDay: function(date) {
var cssClass = '';
if(date >= startDate && date <= endDate)
cssClass = 'ui-datepicker-current-day';
return [true, cssClass];
},
onChangeMonthYear: function(year, month, inst) {
selectCurrentWeek();
}
});
$('.week-picker .ui-datepicker-calendar tr').on('mousemove', function() { $(this).find('td a').addClass('ui-state-hover'); });
$('.week-picker .ui-datepicker-calendar tr').on('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover'); });
});
</script>
</head>
<body>
<div class="week-picker"></div>
<br /><br />
<label>Week :</label> <span id="startDate"></span> - <span id="endDate"></span>
<div class="days"></div>
</body>
</html>
这样做时似乎不起作用我还尝试添加include('/assets/color.php');
作为帮助并将其加载到视图中但又无法正常工作...任何有关如何最好的帮助包括这将是伟大的。
color.php
//从视图中加载库
class GetMostCommonColors
{
var $PREVIEW_WIDTH = 150;
var $PREVIEW_HEIGHT = 150;
var $error;
/**
* Returns the colors of the image in an array, ordered in descending order, where the keys are the colors, and the values are the count of the 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;
}
// WE HAVE TO RESIZE THE IMAGE, BECAUSE WE ONLY NEED THE MOST 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);
// WE NEED NEAREST NEIGHBOR RESIZING, BECAUSE IT DOESN'T ALTER THE 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;
for ($y=0; $y < $imgHeight; $y++)
{
for ($x=0; $x < $imgWidth; $x++)
{
$total_pixel_count++;
$index = imagecolorat($im,$x,$y);
$colors = imagecolorsforindex($im,$index);
// ROUND THE COLORS, TO REDUCE THE 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 you want to *eliminate* gradient variations use:
// ksort( $hexarray );
arsort( $hexarray, SORT_NUMERIC );
$gradients = array();
foreach ($hexarray as $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 you want to *eliminate* brightness variations use:
// ksort( $hexarray );
arsort( $hexarray, SORT_NUMERIC );
$brightness = array();
foreach ($hexarray as $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 to percentages
foreach ($hexarray as $key => $value)
{
$hexarray[$key] = (float)$value / $total_pixel_count;
}
if ( $count > 0 )
{
// only works in PHP5
// return array_slice( $hexarray, 0, $count, true );
$arr = array();
foreach ($hexarray as $key => $value)
{
if ($count == 0)
{
break;
}
$count--;
$arr[$key] = $value;
}
return $arr;
}
else
{
return $hexarray;
}
}
else
{
$this->error = "Image ".$img." does not exist or is 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'];
}
// Do 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;
}
}
for (; $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 it 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;
}
}
答案 0 :(得分:0)
将文件放入库文件夹并使用此代码加载库
$this->load->library('GetMostCommonColors');
此链接可以帮助您..
答案 1 :(得分:-1)
似乎路径不正确,请尝试以下代码:
require_once(dirname(__FILE__).'/assets/color.php');
或
require_once __DIR__ . '/assets/color.php';
但
include('/assets/color.php');
如果资源文件夹是主文件所在的文件夹的子文件夹,也可以正常工作。