Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_area_colourise_image.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design_area/design_area.inc';
19 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
57  public function setAttrValue($name, $value)
58  {
59  // if this is one of the colour attributes then let's grab the value, add it the
60  // $this->colours and then save that array
61  if ($name == 'black_colour' || $name == 'white_colour'
62  || stristr($name, 'from_colour') || stristr($name, 'to_colour')) {
63  $new_value = $this->vars['colours']['value'];
64  if (!is_array($new_value)) $new_value = Array();
65  $new_value[$name] = $value;
66  $name = 'colours';
67  $value = $new_value;
68  }
69 
70  if ($name == 'type' && $value != 'multiple_colours') {
71  $value = 'greyscale';
72  }
73 
74  return parent::setAttrValue($name, $value);
75 
76  }//end setAttrValue()
77 
78 
87  public function attr($name)
88  {
89  if (empty($this->vars['colours']['value'][$name])) {
90  return parent::attr($name);
91  } else {
92  return $this->vars['colours']['value'][$name];
93  }
94 
95  }//end attr()
96 
97 
104  protected function _printPaintingCode()
105  {
106  if (!isset($this->_tmp['image'])) {
107  trigger_error('no image');
108  return;
109  }
110  $image = 'assets/'.$this->type().'/'.$this->id.'/'.$this->_tmp['image'];
111 
112  if ($this->attr('filename_only')) {
113  // they only want us to echo out the filename
114  echo '<', '?php echo $DATA_PATH; ?>/', $image;
115  } else {
116  // print out a full tag
117  echo '<img src="<', '?php echo $DATA_PATH; ?', '>/', $image, '" ', $this->attr('extra'), ' />';
118  }
119 
120  }//end _printPaintingCode()
121 
122 
131  protected function _canInit()
132  {
133  $image_asset = $GLOBALS['DESIGN_BEING_GENERATED']->getAssociatedFile($this->attr('image'));
134  if (is_null($image_asset)) {
135  trigger_localised_error('CORE0184', E_USER_WARNING, $this->attr('id_name'), $this->attr('image'));
136  return FALSE;
137  }
138 
139  // check the source file exists
140  $source = $image_asset->data_path.'/'.$image_asset->attr('name');
141  if (!file_exists($source)) {
142  trigger_localised_error('CORE0183', E_USER_WARNING, $this->attr('id_name'), $source);
143  return FALSE;
144  }
145 
146  if ($this->attr('type') == 'greyscale') {
147  $colours = $this->attr('colours');
148  if (!isset($colours['black_colour']) || !isset($colours['white_colour'])) {
149  trigger_localised_error('CORE0186', E_USER_WARNING, $this->attr('id_name'));
150  return FALSE;
151  }
152  }
153 
154  return TRUE;
155 
156  }//end _canInit()
157 
158 
167  protected function _printInitCode()
168  {
169  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
170  require_once SQ_FUDGE_PATH.'/image/image_manip.inc';
171 
172  $image_asset = $GLOBALS['DESIGN_BEING_GENERATED']->getAssociatedFile($this->attr('image'));
173  $source = $image_asset->data_path.'/'.$image_asset->attr('name');
174 
175  // dest file has design id so that if we are used by customisations they each have their own file
176  $dest = $this->data_path_public.'/colourised_'.$GLOBALS['DESIGN_BEING_GENERATED']->id.'_'.$this->attr('image');
177 
178  if (!create_directory($this->data_path_public)) {
179  trigger_localised_error('CORE0185', E_USER_WARNING, $this->attr('id_name'), $this->data_path_public);
180  return;
181  }
182 
183  $colours = $this->attr('colours');
184 
185  switch ($this->attr('type')) {
186 
187  case 'greyscale' :
188  if (Image_Manip::gradientPalette($source, $dest, $colours['black_colour'], $colours['white_colour'])) {
189  $this->_tmp['image'] = basename($dest);
190  }
191  break;
192 
193  case 'multiple_colours' :
194  $colour_map = Array();
195  $i = 1;
196  while (isset($colours['from_colour_'.$i]) && isset($colours['to_colour_'.$i])) {
197  $colour_map[$colours['from_colour_'.$i]] = $colours['to_colour_'.$i];
198  $i++;
199  }
200  if (Image_Manip::remapColour($source, $dest, $colour_map, $this->attr('tolerance'))) {
201  $this->_tmp['image'] = basename($dest);
202  }
203  break;
204 
205  default :
206  trigger_localised_error('CORE0182', E_USER_WARNING, $this->attr('id_name'), $this->get_val('type'));
207 
208  }//end switch
209 
210  }//end _printInitCode()
211 
212 
222  public function getProtectedAttrs()
223  {
224  $res = parent::getProtectedAttrs();
225  $res[] = 'colours';
226  return $res;
227 
228  }//end getProtectedAttrs()
229 
230 
231 }//end class
232 ?>