Example #1
0
 /**
  * Create icons for an entity
  *
  * @param \ElggEntity $entity  Entity
  * @param mixed       $source  ElggFile, or path, or temp storage to be used as a source for icons
  * @param array       $options Additional options
  *                             'icon_sizes'            Additional icon sizes to create
  *                             'icon_filestore_prefix' Prefix of cropped/resizes icon sizes on the filestore
  *                             'coords'                Cropping coords
  * @return \ElggFile[]|false Created icons
  */
 public function create(\ElggEntity $entity, $source = null, array $options = array())
 {
     if (!$entity instanceof \ElggEntity) {
         return false;
     }
     if (!$source) {
         $source = $entity;
     }
     if ($source instanceof \ElggFile) {
         $source = $source->getFilenameOnFilestore();
     }
     if (empty($source) || !file_exists($source)) {
         return false;
     }
     $coords = elgg_extract('coords', $options, false);
     $dir = $this->getIconDirectory($entity, elgg_extract('icon_filestore_prefix', $options));
     $entity->icon_mimetype = \ElggFile::detectMimeType($source, 'image/jpeg');
     $entity->icon_directory = $dir;
     // reset
     unset($entity->icontime);
     foreach (array('x1', 'x2', 'y1', 'y2') as $coord) {
         unset($entity->{$coord});
     }
     $error = false;
     $icons = array();
     $icons_meta = array();
     $icon_sizes = $this->getSizes($entity, elgg_extract('icon_sizes', $options, array()));
     foreach ($icon_sizes as $size => $props) {
         if (!isset($props['croppable'])) {
             $props['croppable'] = in_array($size, $this->config->getCroppableSizes());
         }
         if (is_array($coords) && !isset($coords['master_width'])) {
             $coords['master_width'] = $this->config->get('master_size_length');
             $coords['master_height'] = $this->config->get('master_size_length');
         }
         try {
             $icon = $this->getIconFile($entity, $size);
             $image = new \hypeJunction\Files\Image($source);
             $image->resize($props, $coords);
             $image->save($icon->getFilenameOnFilestore(), $this->config->getIconCompressionOpts());
             $icons[$size] = $icon;
             if (isset($props['metadata_name'])) {
                 $metadata_name = $props['metadata_name'];
                 $icons_meta[$metadata_name] = $icon->getFilename();
             }
         } catch (\Exception $ex) {
             elgg_log($ex->getMessage(), 'ERROR');
             $error = true;
         }
     }
     if ($error) {
         foreach ($icons as $icon) {
             $icon->delete();
         }
         return false;
     }
     if (!$entity instanceof \ElggFile) {
         // store the original icon source file
         $src = $this->getIconFile($entity);
         $srcimg = new \hypeJunction\Files\Image($source);
         $srcimg->save($src->getFilenameOnFilestore(), $this->config->getSrcCompressionOpts());
     }
     if (is_array($coords)) {
         // store cropping coordinates
         foreach ($coords as $coord => $value) {
             $entity->{$coord} = $value;
         }
     }
     foreach ($icons_meta as $name => $value) {
         $entity->{$name} = $value;
     }
     $entity->icontime = time();
     return $icons;
 }