Ejemplo n.º 1
0
 /**
  * Create image thumbnails
  * If coordinates are not set, $entity metadata will be used
  *
  * @param ElggEntity $entity  Entity
  * @param int        $x1 Upper left crooping coordinate
  * @param int        $y1 Upper left crooping coordinate
  * @param int        $x2 Lower right cropping coordinate
  * @param int        $y2 Lower right cropping coordinate
  * @return Thumb[]|false
  */
 public function createThumbs(ElggEntity $entity, $x1 = null, $y1 = null, $x2 = null, $y2 = null)
 {
     if (!$this->isImage($entity)) {
         return false;
     }
     $this->clearThumbs($entity);
     $x1 = isset($x1) ? (int) $x1 : (int) $entity->x1;
     $y1 = isset($y1) ? (int) $y1 : (int) $entity->y1;
     $x2 = isset($x2) ? (int) $x2 : (int) $entity->x2;
     $y2 = isset($y2) ? (int) $y2 : (int) $entity->y2;
     $crop_width = $x2 - $x1;
     $crop_height = $y2 - $y1;
     $error = false;
     $thumbs = [];
     $sizes = $this->getThumbSizes($entity);
     foreach ($sizes as $size => $opts) {
         $width = elgg_extract('w', $opts);
         $height = elgg_extract('h', $opts);
         $square = elgg_extract('square', $opts);
         $croppable = elgg_extract('croppable', $opts, $square);
         $mode = elgg_extract('mode', $opts);
         $metadata_name = elgg_extract('metadata_name', $opts);
         if ($metadata_name && $entity->{$metadata_name}) {
             $filestorename = $entity->{$metadata_name};
         } else {
             $directory = $this->getThumbDirectory($entity);
             $filename = $this->getThumbFilename($entity, $size);
             $filestorename = "{$directory}/{$filename}";
         }
         $thumb = new Thumb();
         $thumb->owner_guid = $entity->owner_guid;
         $thumb->setFilename($filestorename);
         if (!$thumb->exists()) {
             $thumb->open('write');
             $thumb->close();
         }
         $thumbs[] = $thumb;
         $params = ['entity' => $entity, 'thumb' => $thumb];
         $options = elgg_trigger_plugin_hook('options', 'imagine', $params, []);
         try {
             ini_set('memory_limit', '256M');
             if ($mode != 'outbound' && $mode != 'inset') {
                 $mode = $square ? 'outbound' : 'inset';
             }
             $box = new Box($width, $height);
             $image = $this->imagine->open($entity->getFilenameOnFilestore());
             if ($croppable && $crop_width > 0 && $crop_height > 0) {
                 $image = $image->crop(new Point($x1, $y1), new Box($crop_width, $crop_height));
             }
             $image = $image->thumbnail($box, $mode);
             $image->save($thumb->getFilenameOnFilestore(), $options);
             unset($image);
             if (!empty($opts['metadata_name'])) {
                 $md_name = $opts['metadata_name'];
                 $entity->{$md_name} = $thumb->getFilename();
             }
         } catch (Exception $ex) {
             elgg_log($ex->getMessage(), 'ERROR');
             $error = true;
         }
     }
     if ($error) {
         foreach ($thumbs as $thumb) {
             $thumb->delete();
         }
         return false;
     }
     $entity->icon_owner_guid = $entity->owner_guid;
     return $thumbs;
 }