Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
image_manip.inc
1 <?php
18 require_once dirname(__FILE__).'/../general/file_system.inc';
19 
31 {
32 
33 
46  public static function gradientPalette($input, $output, $colour_a, $colour_b)
47  {
48  require_once dirname(__FILE__).'/../colour/colour.inc';
49 
50  if (!($image = Image_Manip::fromFile($input))) {
51  trigger_error('Unable to open image for recolourizing: '.$input);
52  return false;
53  }
54 
55  // Anylize colours and calculate difference colour
56  $ca_array = Colour::htmlToRgb($colour_a);
57  $cb_array = Colour::htmlToRgb($colour_b);
58  $diff = Array(
59  'r' => ($cb_array['r'] - $ca_array['r']),
60  'g' => ($cb_array['g'] - $ca_array['g']),
61  'b' => ($cb_array['b'] - $ca_array['b']),
62  );
63 
64  // Get the number of colours in a palette
65  $palette_size = imageColorsTotal($image);
66  // Replace colours in palette
67  for ($i = 0; $i < $palette_size; $i++) {
68  // because the image is greyscale each value for the R, G & B will be the same
69  // therefore to find the percentage white this index is we divide by 256
70  $colour = imageColorsForIndex ($image, $i);
71  $fraction = (double) ($colour['red'] / 256);
72 
73  $r = (int) (255 * ($ca_array['r'] + $diff['r'] * $fraction));
74  $g = (int) (255 * ($ca_array['g'] + $diff['g'] * $fraction));
75  $b = (int) (255 * ($ca_array['b'] + $diff['b'] * $fraction));
76  imageColorSet($image,$i, $r, $g, $b);
77  }
78 
79  // Output result to file
80  $ret_val = Image_Manip::toFile($image, $output);
81  imageDestroy($image);
82  return $ret_val;
83 
84  }//end gradientPalette()
85 
86 
99  public static function remapColour($input, $output, $colour_map, $tolerance=0.2)
100  {
101  require_once dirname(__FILE__).'/../colour/colour.inc';
102 
103  if (!($image = Image_Manip::fromFile($input))) {
104  trigger_error('Unable to open image for recolourizing: '.$input);
105  return false;
106  }
107 
108  // Get the number of colours in a palette
109  $palette_size = imageColorsTotal($image);
110 
111  // Replace colours in palette
112  for ($i = 0; $i < $palette_size; $i++) {
113  $colour = imageColorsForIndex($image, $i);
114  // Convert to HTML colour
115  $colour = Colour::rgbToHtml(Colour::rgb($colour['red'],$colour['green'],$colour['blue']));
116  $rgb = Colour::htmlToRgb(Colour::remap($colour,$colour_map,$tolerance));
117  $r = $rgb['r'] * 255;
118  $g = $rgb['g'] * 255;
119  $b = $rgb['b'] * 255;
120  imagecolorset($image,$i, $r, $g, $b);
121  }
122 
123  // Output result to file
124  $ret_val = Image_Manip::toFile($image, $output);
125  imageDestroy($image);
126  return $ret_val;
127 
128  }//end remapColour()
129 
130 
141  public static function resize($input, $output, $w, $h)
142  {
143  // NOTE: $s_* = source image vars, $d_* = destination image vars
144  $s_img = Image_Manip::fromFile($input);
145 
146  // transparency needs to be handled separately for GIF/PNG
147  switch (get_file_type($output)) {
148  case 'gif':
149  $d_img = imageCreate($w, $h);
150  // preserve GIF transparency, if any
151  $colorTransparent = imagecolortransparent($s_img);
152  if ($colorTransparent > -1) {
153  imagepalettecopy($d_img,$s_img);
154  imagefill($d_img,0,0,$colorTransparent);
155  imagecolortransparent($d_img, $colorTransparent);
156  }
157  break;
158  case 'png':
159  // preserve PNG transparency
160  $d_img = imageCreateTrueColor($w, $h);
161  imagealphablending( $d_img, false );
162  imagesavealpha( $d_img, true );
163  break;
164  default:
165  $d_img = imageCreateTrueColor($w, $h);
166  }
167 
168  $ret_val = false;
169 
170  $ret_val = ImageCopyResampled($d_img, $s_img, 0, 0, 0, 0, $w, $h, imageSx($s_img), imageSy($s_img));
171  imageDestroy($s_img); // kill this to save memory before save
172 
173  if ($ret_val) {
174  $ret_val = Image_Manip::toFile($d_img, $output);
175  }
176 
177  // finally destory dest image
178  imageDestroy($d_img);
179 
180  return $ret_val;
181 
182  }//end resize()
183 
184 
192  public static function fromFile($input)
193  {
194  switch (get_file_type($input)) {
195  case 'gif' :
196  if ($image = imageCreateFromGif ($input)) {
197  return $image;
198  } else {
199  trigger_error('Unable to open GIF : '.$input);
200  }
201  break;
202  case 'png' :
203  if ($image = imageCreateFromPNG($input)) {
204  return $image;
205  } else {
206  trigger_error('Unable to open PNG : '.$input);
207  }
208  break;
209  case 'jpg' :
210  case 'jpeg':
211  if ($image = imageCreateFromJPEG($input)) {
212  return $image;
213  } else {
214  trigger_error('Unable to open JPEG : '.$input);
215  }
216  break;
217  default:
218  trigger_error('Unable to open unknown image type : '.$input);
219  break;
220  }
221  return false;
222 
223  }//end fromFile()
224 
225 
235  public static function toFile($image, $output, $jpeg_quality=80)
236  {
237  // Output result to file
238  switch (get_file_type($output)) {
239  case 'gif' :
240  if (imageGif($image, $output)) return true;
241  break;
242 
243  case 'png' :
244  if (imagePNG($image, $output)) return true;
245  break;
246 
247  case 'jpg' :
248  case 'jpeg':
249  if (imageJPEG($image, $output, $jpeg_quality)) {
250  return TRUE;
251  }
252  break;
253 
254  default:
255  trigger_error('Unable to write unknown image type : '.$output, E_USER_WARNING);
256  break;
257  }
258 
259  trigger_error('Unable to output image : '.$output, E_USER_WARNING);
260  return FALSE;
261 
262  }//end toFile()
263 
264 
265 }//end class
266 ?>