CMS  Version 3.9
image_manager.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
40 {
42  {
43  }
44 
45 
46  function export()
47  {
48  $xml = "\n<ImageMap>";
49  $xml .= SerializationManager::serialize(ImageGallery, "ORDER BY gallery_id");
50  $xml .= SerializationManager::serialize(ImageRecord, "ORDER BY image_id");
51  $xml .= "</ImageMap>";
52 
53  return $xml;
54  }
55 
56  function import($doc, $tx)
57  {
60  }
61 }
62 
70 {
74  function ImageManager()
75  {
76  }
77 
90  function renderThumbnail($image_id, $size = 0, $width = 0, $height = 0)
91  {
92  global $config;
93 
94  if ($size > 0)
95  {
96  $path = $this->getCachedFilePath($image_id, "thumbnail", $size);
97  if ($path)
98  {
100  return;
101  }
102  }
103 
105 
106  if ($image->isSVG())
107  {
108  return $this->renderImage($image_id);
109  }
110 
111  $gallery = $image->Gallery();
112 
113  trace("renderThumbnail:: image = {$image->image_id} and gallery = {$gallery->gallery_id}", 3);
114  //TODO: Fix this
115  //if (!checkRole($gallery->read_access))
116  //{
117  // throw new FakoliException("Access Denied");
118  //}
119 
120  if (!$size && !$width && !$height) $size = $config['thumbnail_size'];
121 
122  $suffix = $size ? "$size" : ($width ? "{$width}w" : ($height ? "{$height}h" : ""));
123 
124  $nameParts = explode(".",$image->image_file);
125  $ext = ".".strtolower($nameParts[count($nameParts) - 1]);
126 
127  $imageFile = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . $image->image_file;
128  $cacheDir = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . "thumbnail-cache";
129  $cacheFile = $cacheDir . DIRECTORY_SEPARATOR . $image->image_id . "_" . $suffix . $ext;
130  trace("renderThumbnail:: imageFile {$imageFile} and cacheDir {$cacheDir} and cacheFile {$cacheFile}", 3);
131 
132  if (!file_exists($cacheFile) || (filemtime($cacheFile) < filemtime($imageFile)))
133  {
134  switch($ext)
135  {
136  case ".jpg":
137  $src = imagecreatefromjpeg($imageFile);
138  break;
139 
140  case ".jpeg":
141  $src = imagecreatefromjpeg($imageFile);
142  break;
143 
144  case ".png":
145  $src = imagecreatefrompng($imageFile);
146  break;
147 
148  case ".gif":
149  $src = imagecreatefromgif($imageFile);
150  break;
151  }
152 
153  // If the thumbnail hasn't been generated yet, or is out-of-date, create it.
154  $fullWidth = imagesx($src);
155  $fullHeight = imagesy($src);
156 
157  if ($size)
158  {
159  if ($fullWidth > $fullHeight)
160  {
161  $newWidth = $size;
162  $newHeight = intval(($fullHeight * $size) / $fullWidth);
163 
164  }
165  else
166  {
167  $newWidth = intval(($fullWidth * $size) / $fullHeight);
168  $newHeight = $size;
169  }
170  }
171  else if ($width)
172  {
173  $newWidth = $width;
174  $newHeight = intval(($fullHeight * $width) / $fullWidth);
175  }
176  else if ($height)
177  {
178  $newHeight = $height;
179  $newWidth = intval(($fullWidth * $height) / $fullHeight);
180  }
181  else
182  {
183  $newWidth = $fullWidth;
184  $newHeight = $fullHeight;
185  }
186 
187  $dst = imagecreatetruecolor($newWidth, $newHeight);
188  imagealphablending($dst, false);
189  imagesavealpha($dst, true);
190 
191  $trans = imagecolorallocatealpha($dst, 255, 255, 255, 127);
192  $white = imagecolorallocate($dst, 255, 255, 255);
193 
194  imagecolortransparent($dst, $trans);
195 
196  imagefilledrectangle($dst, 0, 0, $newWidth, $newHeight, $white);
197  imagefilledrectangle($dst, 0, 0, $newWidth, $newHeight, $trans);
198 
199  imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $fullWidth, $fullHeight);
200 
201  if (!file_exists($cacheDir))
202  {
203  trace("renderThumbnail:: creating cachedir", 3);
204  mkdir($cacheDir);
205  }
206 
207  if (file_exists($cacheFile))
208  {
209  // If a previous copy of the file already exists, remove it
210  trace("renderThumbnail:: unlinking cachefile", 3);
211  unlink($cacheFile);
212  }
213 
214  switch($ext)
215  {
216  case ".jpg":
217 
218  imagejpeg($dst, $cacheFile, 85);
219  break;
220 
221  case ".jpeg":
222 
223  imagejpeg($dst, $cacheFile, 85);
224  break;
225 
226  case ".png":
227 
228  imagepng($dst, $cacheFile);
229  break;
230 
231  case ".gif":
232  imagegif($dst, $cacheFile);
233  break;
234  }
235 
236  imagedestroy($dst);
237  imagedestroy($src);
238  }
239 
240  if ($size > 0)
241  {
242  $this->cacheFilePath($image_id, "thumbnail", $size, $cacheFile);
243  }
244 
245  Fakoli::sendFile($cacheFile);
246  }
247 
254  {
256  $gallery = $image->Gallery();
257  $imageFile = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . $image->image_file;
258 
259  Fakoli::sendFile($imageFile);
260  }
261 
263  {
264  global $config;
265 
267 
268  if ($image->isSVG())
269  {
270  return $this->renderImage($image_id);
271  }
272 
273  $gallery = $image->Gallery();
274 
275  trace("resizeAndCropToFit:: image = {$image->image_id} and gallery = {$gallery->gallery_id}", 3);
276  //TODO: Fix this
277  //if (!checkRole($gallery->read_access))
278  //{
279  // throw new FakoliException("Access Denied");
280  //}
281 
282  if (!$width || !$height) $size = $config['thumbnail_size'];
283 
284  $suffix = "cropTo_{$width}x{$height}";
285 
286  $nameParts = explode(".",$image->image_file);
287  $ext = ".".strtolower($nameParts[count($nameParts) - 1]);
288 
289  $imageFile = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . $image->image_file;
290  $cacheDir = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . "thumbnail-cache";
291  $cacheFile = $cacheDir . DIRECTORY_SEPARATOR . $image->image_id . "_" . $suffix . "." . $gallery->getThumbnailFormat();
292  trace("renderThumbnail:: imageFile {$imageFile} and cacheDir {$cacheDir} and cacheFile {$cacheFile}", 3);
293 
294  if (!file_exists($cacheFile) || (filemtime($cacheFile) < filemtime($imageFile)))
295  {
296  switch($ext)
297  {
298  case ".jpg":
299  $src = imagecreatefromjpeg($imageFile);
300  break;
301 
302  case ".jpeg":
303  $src = imagecreatefromjpeg($imageFile);
304  break;
305 
306  case ".png":
307  $src = imagecreatefrompng($imageFile);
308  break;
309 
310  case ".gif":
311  $src = imagecreatefromgif($imageFile);
312  break;
313  }
314 
315  // If the thumbnail hasn't been generated yet, or is out-of-date, create it.
316  $fullWidth = imagesx($src);
317  $fullHeight = imagesy($src);
318 
319  $aspect = $width / $height;
320 
321  $srcHeight = $fullWidth / $aspect;
322  $offset = ($fullHeight - $srcHeight) / 2;
323 
324  $dst = imagecreatetruecolor($width, $height);
325  imagealphablending($dst, false);
326  imagesavealpha($dst, true);
327 
328  $trans = imagecolorallocatealpha($dst, 255, 255, 255, 127);
329  imagecolortransparent($dst, $trans);
330 
331  imagefilledrectangle($dst, 0, 0, $width, $height, $trans);
332 
333  imagecopyresampled($dst, $src, 0, 0, 0, $offset, $width, $height, $fullWidth, $srcHeight);
334 
335  if (!file_exists($cacheDir))
336  {
337  trace("renderThumbnail:: creating cachedir", 3);
338  mkdir($cacheDir);
339  }
340 
341  if (file_exists($cacheFile))
342  {
343  // If a previous copy of the file already exists, remove it
344  trace("renderThumbnail:: unlinking cachefile", 3);
345  unlink($cacheFile);
346  }
347 
348  switch($gallery->getThumbnailFormat())
349  {
350  case "jpg":
351 
352  imagejpeg($dst, $cacheFile, 85);
353  break;
354 
355  case "png":
356  default:
357  imagepng($dst, $cacheFile);
358  }
359 
360  imagedestroy($dst);
361  imagedestroy($src);
362  }
363 
364  Fakoli::sendFile($cacheFile);
365  }
366 
367 
368 
370  {
371  global $config;
372 
374 
375  if ($image->isSVG())
376  {
377  return $this->renderImage($image_id);
378  }
379 
380  $gallery = $image->Gallery();
381 
382  trace("resizeAndCropToCover:: image = {$image->image_id} and gallery = {$gallery->gallery_id}", 3);
383  //TODO: Fix this
384  //if (!checkRole($gallery->read_access))
385  //{
386  // throw new FakoliException("Access Denied");
387  //}
388 
389  if (!$width || !$height)
390  {
391  $size = $config['thumbnail_size'];
392  }
393  else
394  {
395  $size = max($width, $height);
396  }
397 
398  $suffix = "cover_{$width}x{$height}";
399 
400  $nameParts = explode(".",$image->image_file);
401  $ext = ".".strtolower($nameParts[count($nameParts) - 1]);
402 
403  $imageFile = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . $image->image_file;
404  $cacheDir = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . "thumbnail-cache";
405  $cacheFile = $cacheDir . DIRECTORY_SEPARATOR . $image->image_id . "_" . $suffix . "." . $gallery->getThumbnailFormat();
406  trace("renderThumbnail:: imageFile {$imageFile} and cacheDir {$cacheDir} and cacheFile {$cacheFile}", 3);
407 
408  if (!file_exists($cacheFile) || (filemtime($cacheFile) < filemtime($imageFile)))
409  {
410  switch($ext)
411  {
412  case ".jpg":
413  $src = imagecreatefromjpeg($imageFile);
414  break;
415 
416  case ".jpeg":
417  $src = imagecreatefromjpeg($imageFile);
418  break;
419 
420  case ".png":
421  $src = imagecreatefrompng($imageFile);
422  break;
423 
424  case ".gif":
425  $src = imagecreatefromgif($imageFile);
426  break;
427  }
428 
429  // If the thumbnail hasn't been generated yet, or is out-of-date, create it.
430  $fullWidth = imagesx($src);
431  $fullHeight = imagesy($src);
432 
433 
434  if ($size)
435  {
436  if ($fullWidth > $fullHeight)
437  {
438  $newHeight = $size;
439  $newWidth = intval(($fullWidth * $size) / $fullHeight);
440  $offsetX = ($size - $newWidth) / 2;
441  $offsetY = 0;
442  }
443  else
444  {
445  $newWidth = $size;
446  $newHeight = intval(($fullHeight * $size) / $fullWidth);
447  $offsetY = ($size - $newHeight) / 2;
448  $offsetX = 0;
449  }
450  }
451 
452  $dst = imagecreatetruecolor($width, $height);
453  imagealphablending($dst, false);
454  imagesavealpha($dst, true);
455 
456  $trans = imagecolorallocatealpha($dst, 255, 255, 255, 127);
457  imagecolortransparent($dst, $trans);
458 
459  imagefilledrectangle($dst, 0, 0, $width, $height, $trans);
460 
461  imagecopyresampled($dst, $src, $offsetX, $offsetY, 0, 0, $newWidth, $newHeight, $fullWidth, $fullHeight);
462 
463  if (!file_exists($cacheDir))
464  {
465  trace("renderThumbnail:: creating cachedir", 3);
466  mkdir($cacheDir);
467  }
468 
469  if (file_exists($cacheFile))
470  {
471  // If a previous copy of the file already exists, remove it
472  trace("renderThumbnail:: unlinking cachefile", 3);
473  unlink($cacheFile);
474  }
475 
476  switch($gallery->getThumbnailFormat())
477  {
478  case "jpg":
479 
480  imagejpeg($dst, $cacheFile, 85);
481  break;
482 
483  case "png":
484  default:
485  imagepng($dst, $cacheFile);
486  }
487 
488  imagedestroy($dst);
489  imagedestroy($src);
490  }
491 
492  Fakoli::sendFile($cacheFile);
493  }
494 
496  {
497  if (!Settings::getValue("image", "cache_file_paths")) return null;
498 
499  $path = Cache::get("image_{$mode}_{$image_id}_{$size}");
500  return $path;
501  }
502 
504  {
505  if (!Settings::getValue("image", "cache_file_paths")) return;
506  Cache::put("image_{$mode}_{$image_id}_{$size}", $path);
507  }
508 
510  {
511  global $config;
512 
513  $path = $this->getCachedFilePath($image_id, "icon", $size);
514  if ($path)
515  {
517  return;
518  }
519 
521 
522  if ($image->isSVG())
523  {
524  return $this->renderImage($image_id);
525  }
526 
527  $gallery = $image->Gallery();
528 
529  trace("iconize:: image = {$image->image_id} and gallery = {$gallery->gallery_id}", 3);
530  //TODO: Fix this
531  //if (!checkRole($gallery->read_access))
532  //{
533  // throw new FakoliException("Access Denied");
534  //}
535 
536  if (!$size) $size = $config['thumbnail_size'];
537 
538  $suffix = "icon_$size";
539 
540  $nameParts = explode(".",$image->image_file);
541  $ext = ".".strtolower($nameParts[count($nameParts) - 1]);
542 
543  $imageFile = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . $image->image_file;
544  $cacheDir = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . "thumbnail-cache";
545  $cacheFile = $cacheDir . DIRECTORY_SEPARATOR . $image->image_id . "_" . $suffix . "." . $gallery->getThumbnailFormat();
546  trace("renderThumbnail:: imageFile {$imageFile} and cacheDir {$cacheDir} and cacheFile {$cacheFile}", 3);
547 
548  if (!file_exists($cacheFile) || (filemtime($cacheFile) < filemtime($imageFile)))
549  {
550  switch($ext)
551  {
552  case ".jpg":
553  $src = imagecreatefromjpeg($imageFile);
554  break;
555 
556  case ".jpeg":
557  $src = imagecreatefromjpeg($imageFile);
558  break;
559 
560  case ".png":
561  $src = imagecreatefrompng($imageFile);
562  break;
563 
564  case ".gif":
565  $src = imagecreatefromgif($imageFile);
566  break;
567  }
568 
569  // If the thumbnail hasn't been generated yet, or is out-of-date, create it.
570  $fullWidth = imagesx($src);
571  $fullHeight = imagesy($src);
572 
573  if ($size)
574  {
575  if ($fullWidth > $fullHeight)
576  {
577  $newWidth = $size;
578  $newHeight = intval(($fullHeight * $size) / $fullWidth);
579 
580  }
581  else
582  {
583  $newWidth = intval(($fullWidth * $size) / $fullHeight);
584  $newHeight = $size;
585  }
586  }
587  else if ($width)
588  {
589  $newWidth = $width;
590  $newHeight = intval(($fullHeight * $width) / $fullWidth);
591  }
592  else if ($height)
593  {
594  $newHeight = $height;
595  $newWidth = intval(($fullWidth * $height) / $fullHeight);
596  }
597  else
598  {
599  $newWidth = $fullWidth;
600  $newHeight = $fullHeight;
601  }
602 
603  $offsetX = ($size - $newWidth) / 2;
604  $offsetY = ($size - $newHeight) / 2;
605 
606  $dst = imagecreatetruecolor($size, $size);
607 
608 
609  $trans = imagecolorallocatealpha($dst, 255, 255, 255, 127);
610  $white = imagecolorallocate($dst, 255, 255, 255);
611 
612  imagecolortransparent($dst, $trans);
613 
614  imagefilledrectangle($dst, 0, 0, $size, $size, $white);
615  imagefilledrectangle($dst, 0, 0, $size, $size, $trans);
616 
617 
618  imagecopyresampled($dst, $src, $offsetX, $offsetY, 0, 0, $newWidth, $newHeight, $fullWidth, $fullHeight);
619 
620  if (!file_exists($cacheDir))
621  {
622  trace("iconize:: creating cachedir", 3);
623  mkdir($cacheDir);
624  }
625 
626  if (file_exists($cacheFile))
627  {
628  // If a previous copy of the file already exists, remove it
629  trace("iconize:: unlinking cachefile", 3);
630  unlink($cacheFile);
631  }
632 
633  trace("Writing $cacheFile", 3);
634 
635  switch($gallery->getThumbnailFormat())
636  {
637  case "jpg":
638 
639  imagejpeg($dst, $cacheFile, 85);
640  break;
641 
642  case "png":
643  default:
644  imagepng($dst, $cacheFile);
645  }
646 
647  imagedestroy($dst);
648  imagedestroy($src);
649  }
650 
651  $this->cacheFilePath($image_id, "icon", $size, $cacheFile);
652  Fakoli::sendFile($cacheFile);
653  }
654 
664  function crop($id, $x, $y, $w, $h)
665  {
666  global $config;
667 
668  trace("Cropping image $id, starting at ($x, $y) dimensions ($w, $h)", 3);
669 
670  $image = new ImageRecord($id);
671 
672  if ($image->isSVG())
673  {
674  return $this->renderImage($id);
675  }
676 
677  $gallery = $image->Gallery();
678 
679  $imageFile = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . $image->image_file;
680 
681  $nameParts = explode(".",$image->image_file);
682  $ext = ".".strtolower($nameParts[1]);
683 
684  if (!file_exists($cacheFile) || (filemtime($cacheFile) < filemtime($imageFile)))
685  {
686  switch($ext)
687  {
688  case ".jpg":
689  case ".jpeg":
690  $src = imagecreatefromjpeg($imageFile);
691  break;
692 
693  case ".png":
694  $src = imagecreatefrompng($imageFile);
695  break;
696 
697  case ".gif":
698  $src = imagecreatefromgif($imageFile);
699  break;
700  }
701  }
702 
703  $dst = ImageCreateTrueColor( $w, $h );
704 
705  imagecopy($dst, $src, 0, 0, $x, $y, $w, $h);
706 
707  switch($ext)
708  {
709  case ".jpg":
710  case ".jpeg":
711  imagejpeg($dst, $imageFile, 85);
712  break;
713 
714  case ".gif":
715  $src = imagegif($imageFile);
716  break;
717 
718  case "png":
719  default:
720  imagepng($dst, $imageFile);
721  }
722 
723  imagedestroy($dst);
724  imagedestroy($src);
725  }
726 
734  function thumbnailLink($image_id, $size, $iconize = false)
735  {
736  $handler = $iconize ? "iconize" : "thumbnail";
737  return "/action/image/{$handler}?image_id=$image_id&size=$size";
738  }
739 
741  {
742  return "/action/image/show?image_id=$image_id";
743  }
744 
752  {
754  $gallery = $image->Gallery();
755 
756  //TODO: Fix this
757  //if (!checkRole($gallery->read_access))
758  //{
759  // throw new FakoliException("Access Denied");
760  //}
761 
762  $imageFile = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . $image->image_file;
763 
764  $img = imageCreateFromString(file_get_contents($imageFile));
765  $width = imagesx($img);
766  $height = imagesy($img);
767  imageDestroy($img);
768  }
769 
779  {
780  $this->getSize($image_id, $width, $height);
781 
782  if ($width > $height)
783  {
784  $height = $height * $size / $width;
785  $width = $size;
786  }
787  else
788  {
789  $width = $width * $size / $height;
790  $height = $size;
791  }
792  }
793 
799  static function displayImageGallery($identifier, &$continue)
800  {
801  try
802  {
803  $gallery = Query::create(ImageGallery, "WHERE identifier=:i")
804  ->bind(":i", $identifier)
805  ->executeSingle();
806 
807  $page = ComponentPage::findByIdentifier("image_gallery", "WHERE enabled=1");
808  $_GET["gallery_id"] = $gallery->gallery_id;
809 
810  $pageView = new ComponentPageView($page);
811 
812  $page_role = $page->role;
813 
814  if (!checkRole($page->role))
815  {
817  redirect("/login");
818  }
819 
820  echo $pageView->drawView();
821 
822  $continue = false;
823  }
824  catch(DataNotFoundException $e)
825  {
826 
827  }
828 
829  return $identifier;
830  }
831 
836  static function enumerateItems($items)
837  {
838  $galleries = Query::create(ImageGallery, "ORDER BY gallery_name")->execute();
839 
840  $items["Image Galleries"] = $galleries;
841  return $items;
842  }
843 
849  static function isPNG($filename)
850  {
851  // check if the file exists
852  if (!file_exists($filename))
853  {
854  trace("Cannot find $filename", 2);
855  return false;
856  }
857 
858  // define the array of first 8 png bytes
859  $png_header = array(0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A);
860 
861  // open file for reading
862  $f = fopen($filename, 'r');
863 
864  // read first 8 bytes from the file and close the resource
865  $header = fread($f, 8);
866  fclose($f);
867 
868  // convert the string to an array
869  $chars = preg_split('//', $header, -1, PREG_SPLIT_NO_EMPTY);
870 
871  // convert each charater to its ascii value
872  $chars = array_map('ord', $chars);
873 
874  // return true if there are no differences or false otherwise
875  return (count(array_diff($png_header, $chars)) === 0);
876  }
877 
886  static function isJPG($filename)
887  {
888  // check if the file exists
889  if (!file_exists($filename))
890  {
891  trace("Cannot find $filename", 2);
892  return false;
893  }
894 
895  // define the array of first 3 JPEG bytes
896  $png_header = array(0xFF, 0xD8, 0xFF);
897 
898  // open file for reading
899  $f = fopen($filename, 'r');
900 
901  // read first 3 bytes from the file and close the resource
902  $header = fread($f, 3);
903  fclose($f);
904 
905  // convert the string to an array
906  $chars = preg_split('//', $header, -1, PREG_SPLIT_NO_EMPTY);
907 
908  // convert each charater to its ascii value
909  $chars = array_map('ord', $chars);
910 
911  // return true if there are no differences or false otherwise
912  return (count(array_diff($png_header, $chars)) === 0);
913  }
914 
921  static function imageUploadHandler($field, $image)
922  {
923  global $config;
924 
925  trace("imageUploadHandler() called for $field", 3);
926 
927  if (!$_FILES[$field])
928  {
929  trace("No upload record for $field", 3);
930  return false;
931  }
932  if ($_FILES[$field]["name"]=="")
933  {
934  trace("Upload name is empty", 3);
935  return false;
936  }
937 
938  $gallery = $image->Gallery();
939 
940  $dir = $gallery->getGalleryDirectory();
941  if (!file_exists($dir))
942  {
943  mkdir($dir);
944  }
945 
946  /* Copy across the uploaded file */
947 
948  trace("Upload Base: {$dir}", 3);
949 
950  $filename = $_FILES[$field]['name'];
951 
952  $nameParts = explode(".",$filename);
953  $ext = ".".strtolower($nameParts[1]);
954 
955  if ($ext == ".jpeg")
956  {
957  $ext = ".jpg";
958  $filename = strtolower($nameParts[0]) . $ext;
959  }
960 
961  // $ext = strtolower(substr($filename, -4));
962 
963  trace("Image format: $ext", 3);
964 
965  if ($ext != ".jpg" && $ext != ".png" && $ext != ".svg")
966  {
967  throw new FakoliException("Unsupported image format");
968  }
969 
970  if ($gallery->randomize_filenames)
971  {
972  $filename = plainGUID() . $ext;
973  }
974 
975  $target = $dir . DIRECTORY_SEPARATOR . $filename;
976 
977  trace ("Uploading file to $target", 3);
978 
979  if (file_exists($target))
980  {
981  // If a previous copy of the file already exists, remove it
982  unlink($target);
983  }
984 
985  move_uploaded_file($_FILES[$field]["tmp_name"], $target);
986 
987  if ($gallery->fix_orientation && $ext == ".jpg")
988  {
990  if ($rotated != $target)
991  {
992  $target = $rotated;
993  $filename = basename($rotated);
994  $image->set("image_type", "image/png");
995  }
996  }
997  else
998  {
999  chmod($target, 0755);
1000  }
1001 
1002  $image->set("image_file", $filename);
1003  return true;
1004  }
1005 
1014  static function fixOrientation($photo)
1015  {
1016  trace("Checking Photo Orientation", 3);
1017 
1018  $exif = read_exif_data($photo);
1019 
1020  switch($exif['Orientation'])
1021  {
1022  case 6:
1023  $angle = -90;
1024  break;
1025 
1026  case 3:
1027  $angle = 180;
1028  break;
1029 
1030  case 8:
1031  $angle = 90;
1032 
1033  default:
1034 
1035  // No transform
1036  trace("No rotation required", 3);
1037  return $photo;
1038  }
1039 
1040  $source = imagecreatefromjpeg($photo);
1041  $rotated = imagerotate($source, $angle, 0);
1042 
1043  trace("Rotating $angle degrees", 3);
1044 
1045  $photo = preg_replace("/\.jpg\$/", ".png", $photo);
1046 
1047  trace("Saving to $photo");
1048 
1049  imagepng($rotated, $photo);
1050  return $photo;
1051  }
1052 
1053 
1058  static function zipGallery($gallery_id, $process = null)
1059  {
1060  global $config;
1061 
1062  Fakoli::assertRole("admin");
1064 
1065  if ($process)
1066  {
1067  $process->setProgress("Running", "Scanning files", 0);
1068  }
1069 
1070  $files = $gallery->Images("ORDER BY image_file");
1071 
1072  $galleryFileName = $gallery->identifier ? $gallery->format("{identifier}.zip") : $gallery->format("gallery_{gallery_id}.zip");
1073  $galleryDir = $gallery->getGalleryDirectory();
1074  $galleryZipFile = $config['uploadbase'] . DIRECTORY_SEPARATOR . $galleryFileName;
1075  if (file_exists($galleryZipFile))
1076  {
1077  unlink($galleryZipFile);
1078  }
1079 
1080 
1081  $zip = new ZipArchive();
1082 
1083  // open archive
1084  if ($zip->open($galleryZipFile, ZIPARCHIVE::CREATE) !== TRUE)
1085  {
1086  if ($process)
1087  {
1088  $process->setProgress("Error", "Could not create ZIP archive");
1089  throw new FakoliException("Could not create ZIP archive");
1090  }
1091  }
1092 
1093  $max = count($files);
1094  $c = 0;
1095 
1096  foreach($files as $file)
1097  {
1098  $fullPath = $galleryDir . DIRECTORY_SEPARATOR . $file->image_file;
1099  $zip->addFile($fullPath, $file->image_file);
1100 
1101  if ($process)
1102  {
1103  $process->setProgress("Running", "Added $c of $max ".pluralize("file", $max), number_format(100 * $c / $max, 0));
1104  }
1105  }
1106 
1107  $zip->close();
1108 
1109  if ($process)
1110  {
1111  $process->setProgress("Completed", "Zip archive created", 100);
1112  }
1113  }
1114 
1115  static function imageGalleryTabs($key)
1116  {
1117  $tabs = array(
1118  "Definition" => "image_gallery_form",
1119  "Thumbnails" => "images",
1120  "Detailed List" => "image_list"
1121  );
1122 
1123  $qs = ($key) ? "gallery_id=$key" : "";
1124  return new TabBar("tabs", $tabs, $qs);
1125  }
1126 
1127  static function getStyles()
1128  {
1129  $styles .= "<link href=\"/fakoli/css/tabs.css\" rel=\"stylesheet\"/>\n";
1130 
1131  return $styles;
1132  }
1133 
1134  static function setDefaults()
1135  {
1136  trace("ImageManager::setDefaults", 3);
1137 
1138  Settings::setDefaultValue("image", "thumbnail_size", 75, "Number", "");
1139  Settings::setDefaultValue("image", "thumbnail_format", "png", "String", "Use PNG for high quality thumbnails (default) or JPG for fast, low bandwidth thumbnails. JPG files will have lower image quality and do not support transparency", "", "png\njpg");
1140  Settings::setDefaultValue("image", "cache_file_paths", false, "Boolean", "Cache image file paths in memory for increased performance");
1141  Settings::setDefaultValue("image", "retina_mode", false, "Boolean", "Double the resolution of images in image grids to provide better quality on high-DPI displays");
1142  }
1143 
1145  {
1146  $classes[] = ImageRecord;
1147  return $classes;
1148  }
1149 
1150  static function upgradeComponent($version)
1151  {
1152  $mgr = new ImageUpgradeManager();
1153  $mgr->upgrade($version);
1154  }
1155 
1163  static function deleteUser($user)
1164  {
1165  $pk = $user->getPrimaryKey();
1166  $user_id = $user->$pk;
1167 
1168  trace("Component image is deleting objects dependent on user_id {$user_id}", 3);
1169 
1170  $gallery = new ImageGallery();
1171  $gallery->delete("WHERE owner_id={$user_id}");
1172 
1173  return $user;
1174  }
1175 
1177  {
1178  SerializationManager::registerHandler("image_galleries", "Image Galleries &amp; Image Records (files need to be transferred manually)", new ImageSerializationHandler());
1179  return true;
1180  }
1181 }
1182 
1184 {
1186  var $indexedImages = array();
1187 
1189  {
1190  $this->gallery = $gallery;
1191 
1192  $images = $this->gallery->Images();
1193  if(count($images))
1194  {
1195  $this->indexedImages = reindexList($images, "image_file");
1196  }
1197  else
1198  {
1199  $this->indexedImages = array();
1200  }
1201  }
1202 
1203  function rescan()
1204  {
1205  global $user;
1206 
1207  $base = sanitizePath($this->gallery->getGalleryDirectory());
1208 
1209  $files = scandir($base);
1210  foreach($files as $file)
1211  {
1212  if ($file == "." || $file == "..") continue;
1213 
1214  if ($this->indexedImages[$file]) continue;
1215 
1216  $image = new ImageRecord();
1217  $image->title = $file;
1218  $image->image_file = $file;
1219  $image->ALT_tag = "";
1220  $image->published = true;
1221  $image->gallery_id = $this->gallery->gallery_id;
1222  $image->owner_id= $user->get($user->getPrimaryKey());
1223  $image->save();
1224  }
1225  }
1226 }?>
$user_id
$tabs
$handler
Definition: event_form.inc:62
$page
Definition: help.inc:39
if(! $page) $path
Definition: page.inc:57
$src
Definition: page.inc:37
$image_id
Definition: image_form.inc:41
$gallery_id
Definition: image_form.inc:42
$image
Definition: image_form.inc:46
$images
Definition: images.inc:46
$dir
Definition: delete.inc:44
$file
Definition: delete.inc:47
$base
Definition: delete.inc:45
$f
Definition: download.inc:46
$size
Definition: download.inc:47
static findByIdentifier($identifier, $constraint="")
ComponentPageView generates the page content for a component page, substituting page fields,...
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
static sendFile($resource)
Sends the contents of the specified file to the client.
Definition: core.inc:780
static assertRole($role, $redirect="", $message="")
Assert that the user has one of the specified roles.
Definition: core.inc:297
The ImageManager class provides basic Image manipulation functions that are useful when working with ...
renderImage($image_id)
Renders the specified image at its natural size.
renderThumbnail($image_id, $size=0, $width=0, $height=0)
Renders the Image specified by the image_id at the specified size.
static isJPG($filename)
Test the contents of the specified file, returning true if a JPEG header signature is found.
static zipGallery($gallery_id, $process=null)
Generate a ZIP archive of all the images in the specified gallery.
getSize($image_id, &$width, &$height)
Determines the size of the specified image.
static getStyles()
thumbnailLink($image_id, $size, $iconize=false)
Returns the URI that can be used to access the specified image at the given size.
static displayImageGallery($identifier, &$continue)
Event handler to display an image gallery from the specified identifier.
static registerSerializationHandler()
iconize($image_id, $size)
crop($id, $x, $y, $w, $h)
Crop the image to the specified dimensions.
static imageGalleryTabs($key)
resizeAndCropToCover($image_id, $width, $height)
static imageUploadHandler($field, $image)
Upload the image file specified in the given field.
ImageManager()
Creates a new ImageManager.
static isPNG($filename)
Test the contents of the specified file, returning true if a PNG header signature is found.
getScaledSize($image_id, $size, &$width, &$height)
Calculates the width and height of an image after it is scaled to the specified size along its major ...
static upgradeComponent($version)
static registerTaxonomyClasses($classes)
getCachedFilePath($image_id, $mode, $size)
static deleteUser($user)
Respond to fired event DeleteUser.
static enumerateItems($items)
Enumerate the Image Gallery objects.
cacheFilePath($image_id, $mode, $size, $path)
static fixOrientation($photo)
Rotates the image to match the correct orientation when orientation information is included in the im...
imageLink($image_id)
resizeAndCropToFit($image_id, $width, $height)
static setDefaults()
static storeRedirectPage()
Store the page from which a user has been redirected when prompted to login or create an account.
Definition: login.inc:493
static serialize($class, $constraint="")
Serializes the specified DataItems to XML.
registerHandler($component, $title, $handler)
Registers a serialization handler for a component.
static unserialize($class, $doc, $tx, $save=true)
Instantiates DataItems from the supplied XML document and stores them in the database.
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174
global $user
$height
Definition: cover.inc:38
$width
Definition: cover.inc:37
if(!checkRole("admin")) $c
$mode
global $config
Definition: import.inc:4
$max
$styles
$galleries
$identifier
Definition: rss.inc:37
$process
Definition: run.php:54
$chart max
if(!Settings::getValue("debug", "enable_trace_file_downloads")) $filename
Definition: trace.inc:42