CMS  Version 3.9
ImageManager Class Reference

The ImageManager class provides basic Image manipulation functions that are useful when working with Image objects. More...

Public Member Functions

 ImageManager ()
 Creates a new ImageManager. More...
 
 renderThumbnail ($image_id, $size=0, $width=0, $height=0)
 Renders the Image specified by the image_id at the specified size. More...
 
 renderImage ($image_id)
 Renders the specified image at its natural size. More...
 
 resizeAndCropToFit ($image_id, $width, $height)
 
 resizeAndCropToCover ($image_id, $width, $height)
 
 getCachedFilePath ($image_id, $mode, $size)
 
 cacheFilePath ($image_id, $mode, $size, $path)
 
 iconize ($image_id, $size)
 
 crop ($id, $x, $y, $w, $h)
 Crop the image to the specified dimensions. More...
 
 thumbnailLink ($image_id, $size, $iconize=false)
 Returns the URI that can be used to access the specified image at the given size. More...
 
 imageLink ($image_id)
 
 getSize ($image_id, &$width, &$height)
 Determines the size of the specified image. More...
 
 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 axis. More...
 

Static Public Member Functions

static displayImageGallery ($identifier, &$continue)
 Event handler to display an image gallery from the specified identifier. More...
 
static enumerateItems ($items)
 Enumerate the Image Gallery objects. More...
 
static isPNG ($filename)
 Test the contents of the specified file, returning true if a PNG header signature is found. More...
 
static isJPG ($filename)
 Test the contents of the specified file, returning true if a JPEG header signature is found. More...
 
static imageUploadHandler ($field, $image)
 Upload the image file specified in the given field. More...
 
static fixOrientation ($photo)
 Rotates the image to match the correct orientation when orientation information is included in the image file. More...
 
static zipGallery ($gallery_id, $process=null)
 Generate a ZIP archive of all the images in the specified gallery. More...
 
static imageGalleryTabs ($key)
 
static getStyles ()
 
static setDefaults ()
 
static registerTaxonomyClasses ($classes)
 
static upgradeComponent ($version)
 
static deleteUser ($user)
 Respond to fired event DeleteUser. More...
 
static registerSerializationHandler ()
 

Detailed Description

The ImageManager class provides basic Image manipulation functions that are useful when working with Image objects.

Author
andy

Definition at line 69 of file image_manager.inc.

Member Function Documentation

◆ cacheFilePath()

ImageManager::cacheFilePath (   $image_id,
  $mode,
  $size,
  $path 
)

Definition at line 503 of file image_manager.inc.

504  {
505  if (!Settings::getValue("image", "cache_file_paths")) return;
506  Cache::put("image_{$mode}_{$image_id}_{$size}", $path);
507  }
if(! $page) $path
Definition: page.inc:57
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104

◆ crop()

ImageManager::crop (   $id,
  $x,
  $y,
  $w,
  $h 
)

Crop the image to the specified dimensions.

This operation is destructive - it overwrites the source file with the resulting crop.

Parameters
integer$idthe ID of the image to be cropped
integer$xthe left-hand coordinate to crop from
integer$ythe top coordinate to crop from
integer$wthe width to crop to
integer$hthe height to crop to

Definition at line 664 of file image_manager.inc.

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  }
$src
Definition: page.inc:37
$image
Definition: image_form.inc:46
renderImage($image_id)
Renders the specified image at its natural size.
global $config
Definition: import.inc:4

◆ deleteUser()

static ImageManager::deleteUser (   $user)
static

Respond to fired event DeleteUser.

Delete any records in this component that have dependencies on user object.

Parameters
obj$user- class SiteUser or custom user class

Definition at line 1163 of file image_manager.inc.

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  }
$user_id
global $user

◆ displayImageGallery()

static ImageManager::displayImageGallery (   $identifier,
$continue 
)
static

Event handler to display an image gallery from the specified identifier.

Parameters
string$identifierthe identifier to match
boolean$continuereturns whether to continue processing this event

Definition at line 799 of file image_manager.inc.

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  }
$page
Definition: help.inc:39
static findByIdentifier($identifier, $constraint="")
ComponentPageView generates the page content for a component page, substituting page fields,...
static storeRedirectPage()
Store the page from which a user has been redirected when prompted to login or create an account.
Definition: login.inc:493
$identifier
Definition: rss.inc:37

◆ enumerateItems()

static ImageManager::enumerateItems (   $items)
static

Enumerate the Image Gallery objects.

Parameters
$itemscollection that the ImageGallery items are returned in

Definition at line 836 of file image_manager.inc.

837  {
838  $galleries = Query::create(ImageGallery, "ORDER BY gallery_name")->execute();
839 
840  $items["Image Galleries"] = $galleries;
841  return $items;
842  }
$galleries

◆ fixOrientation()

static ImageManager::fixOrientation (   $photo)
static

Rotates the image to match the correct orientation when orientation information is included in the image file.

Currently this only works for JPEGs. The target file is converted to a PNG in the rotation process so that the transformation is lossless.

Parameters
string$photopath to the photo that is to be re-oriented
Returns
the path to the reoriented photo

Definition at line 1014 of file image_manager.inc.

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  }

◆ getCachedFilePath()

ImageManager::getCachedFilePath (   $image_id,
  $mode,
  $size 
)

Definition at line 495 of file image_manager.inc.

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  }

◆ getScaledSize()

ImageManager::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 axis.

Parameters
$image_idthe ID of the image.
$sizethe required length of the major axis
$widthreturns the width of the scaled image
$heightreturns the height of the scaled image

Definition at line 778 of file image_manager.inc.

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  }
$image_id
Definition: image_form.inc:41
$size
Definition: download.inc:47
getSize($image_id, &$width, &$height)
Determines the size of the specified image.
$height
Definition: cover.inc:38
$width
Definition: cover.inc:37

◆ getSize()

ImageManager::getSize (   $image_id,
$width,
$height 
)

Determines the size of the specified image.

Parameters
$image_idthe ID of the image to be queried.
$widthreturns the width of the image in pixels
$heightreturns the height of the image in pixels

Definition at line 751 of file image_manager.inc.

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  }

◆ getStyles()

static ImageManager::getStyles ( )
static

Definition at line 1127 of file image_manager.inc.

1128  {
1129  $styles .= "<link href=\"/fakoli/css/tabs.css\" rel=\"stylesheet\"/>\n";
1130 
1131  return $styles;
1132  }
$styles

◆ iconize()

ImageManager::iconize (   $image_id,
  $size 
)

Definition at line 509 of file image_manager.inc.

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  }
static sendFile($resource)
Sends the contents of the specified file to the client.
Definition: core.inc:780
getCachedFilePath($image_id, $mode, $size)
cacheFilePath($image_id, $mode, $size, $path)

◆ imageGalleryTabs()

static ImageManager::imageGalleryTabs (   $key)
static

Definition at line 1115 of file image_manager.inc.

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  }
$tabs

◆ imageLink()

ImageManager::imageLink (   $image_id)

Definition at line 740 of file image_manager.inc.

741  {
742  return "/action/image/show?image_id=$image_id";
743  }

◆ ImageManager()

ImageManager::ImageManager ( )

Creates a new ImageManager.

Definition at line 74 of file image_manager.inc.

75  {
76  }

◆ imageUploadHandler()

static ImageManager::imageUploadHandler (   $field,
  $image 
)
static

Upload the image file specified in the given field.

Parameters
string$fieldthe field name of the image file
$imagethe Image object the field is being uploaded to.
Returns
boolean true if the upload worked, false if it failed.

Definition at line 921 of file image_manager.inc.

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  }
$dir
Definition: delete.inc:44
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
static fixOrientation($photo)
Rotates the image to match the correct orientation when orientation information is included in the im...
if(!Settings::getValue("debug", "enable_trace_file_downloads")) $filename
Definition: trace.inc:42

◆ isJPG()

static ImageManager::isJPG (   $filename)
static

Test the contents of the specified file, returning true if a JPEG header signature is found.

This is very rough and ready - only use it if you are pretty sure that the file you are being passed is an image.

Parameters
string$filefull path to the file
Returns
boolean true if the file is a JPG

Definition at line 886 of file image_manager.inc.

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  }
$f
Definition: download.inc:46

◆ isPNG()

static ImageManager::isPNG (   $filename)
static

Test the contents of the specified file, returning true if a PNG header signature is found.

Parameters
string$filenamefull path to the file
Returns
boolean true if the file is a PNG

Definition at line 849 of file image_manager.inc.

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  }

◆ registerSerializationHandler()

static ImageManager::registerSerializationHandler ( )
static

Definition at line 1176 of file image_manager.inc.

1177  {
1178  SerializationManager::registerHandler("image_galleries", "Image Galleries &amp; Image Records (files need to be transferred manually)", new ImageSerializationHandler());
1179  return true;
1180  }
registerHandler($component, $title, $handler)
Registers a serialization handler for a component.

◆ registerTaxonomyClasses()

static ImageManager::registerTaxonomyClasses (   $classes)
static

Definition at line 1144 of file image_manager.inc.

1145  {
1146  $classes[] = ImageRecord;
1147  return $classes;
1148  }

◆ renderImage()

ImageManager::renderImage (   $image_id)

Renders the specified image at its natural size.

The image file is output with the correct HTTP headers to ensure correct interpretation by the browser.

Parameters
$image_idthe ID of the image to be rendered.

Definition at line 253 of file image_manager.inc.

254  {
256  $gallery = $image->Gallery();
257  $imageFile = $gallery->getGalleryDirectory() . DIRECTORY_SEPARATOR . $image->image_file;
258 
259  Fakoli::sendFile($imageFile);
260  }

◆ renderThumbnail()

ImageManager::renderThumbnail (   $image_id,
  $size = 0,
  $width = 0,
  $height = 0 
)

Renders the Image specified by the image_id at the specified size.

Note that the size is the length of the major axis, and aspect ratio is maintained. The generated image is cached after rendering to speed subsequent accesses. The image file is output with the correct HTTP headers to ensure correct interpretation by the browser.

Parameters
$image_idthe ID of the image to render
$sizethe size of the major axis, in pixels
$widththe specific width of the rendered thumbnail (if $size is 0)
$heightthe specific height of the rendered thumbnail (if $size and $width are 0)

Definition at line 90 of file image_manager.inc.

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  }

◆ resizeAndCropToCover()

ImageManager::resizeAndCropToCover (   $image_id,
  $width,
  $height 
)

Definition at line 369 of file image_manager.inc.

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  }
$chart max

◆ resizeAndCropToFit()

ImageManager::resizeAndCropToFit (   $image_id,
  $width,
  $height 
)

Definition at line 262 of file image_manager.inc.

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  }

◆ setDefaults()

static ImageManager::setDefaults ( )
static

Definition at line 1134 of file image_manager.inc.

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  }
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

◆ thumbnailLink()

ImageManager::thumbnailLink (   $image_id,
  $size,
  $iconize = false 
)

Returns the URI that can be used to access the specified image at the given size.

Parameters
$image_idthe ID of the image
$sizethe size of the major axis in pixels
$iconizeif set to true, the image is squared of with the edge length being the specified size.
Returns
string URI that can be used in an tag to view this image.

Definition at line 734 of file image_manager.inc.

735  {
736  $handler = $iconize ? "iconize" : "thumbnail";
737  return "/action/image/{$handler}?image_id=$image_id&size=$size";
738  }
$handler
Definition: event_form.inc:62

◆ upgradeComponent()

static ImageManager::upgradeComponent (   $version)
static

Definition at line 1150 of file image_manager.inc.

1151  {
1152  $mgr = new ImageUpgradeManager();
1153  $mgr->upgrade($version);
1154  }

◆ zipGallery()

static ImageManager::zipGallery (   $gallery_id,
  $process = null 
)
static

Generate a ZIP archive of all the images in the specified gallery.

Parameters
integer$gallery_idthe ID of the gallery to be zipped

Definition at line 1058 of file image_manager.inc.

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  }
$gallery_id
Definition: image_form.inc:42
$file
Definition: delete.inc:47
static assertRole($role, $redirect="", $message="")
Assert that the user has one of the specified roles.
Definition: core.inc:297
if(!checkRole("admin")) $c
$max
$process
Definition: run.php:54

The documentation for this class was generated from the following file: