Exemple #1
0
 /**
  * Create a fake image
  *
  * @param int $owner_guid     GUID of the owner
  * @param int $container_guid GUID of the container album
  * @param hjAlbumImage $type
  */
 public static function createImage($owner_guid = ELGG_ENTITIES_ANY_VALUE, $container_guid = ELGG_ENTITIES_ANY_VALUE, $type = null)
 {
     if (!$type) {
         $type = 'jpg';
     }
     $image = new hjAlbumImage();
     $image->owner_guid = $owner_guid;
     $image->container_guid = $container_guid;
     $image->setFilename("tests/testfile.{$type}");
     $file = self::getTestFile($type);
     $image->open('write');
     $image->write($file);
     $image->close();
     $image->save();
     return $image;
 }
/**
 * Generate icons for an entity
 *
 * @param hjAlbumImage $entity      Entity
 * @param ElggFile     $filehandler Valid filehandler on Elgg filestore to grab the file from | can be null if $entity is instance of ElggFile
 * @param array        $coords      Coordinates for cropping
 * @return boolean
 */
function generate_entity_icons($entity, $filehandler = null, $coords = null)
{
    $icon_sizes = elgg_get_config('icon_sizes');
    $gallery_icon_sizes = elgg_get_config('gallery_icon_sizes');
    $icon_sizes = array_merge($icon_sizes, $gallery_icon_sizes);
    if (!$filehandler && $entity instanceof ElggFile) {
        $filehandler = $entity;
    }
    if (!$filehandler) {
        return false;
    }
    $prefix = "icons/" . $entity->getGUID();
    foreach ($icon_sizes as $size => $values) {
        $w = elgg_extract('w', $values, 200);
        $h = elgg_extract('h', $values, 200);
        $square = elgg_extract('square', $values, true);
        $upscale = elgg_extract('upscale', $values, false);
        $filepath = $filehandler->getFilenameOnFilestore();
        if (is_array($coords) && in_array($size, array('topbar', 'tiny', 'small', 'medium', 'large'))) {
            $x1 = elgg_extract('x1', $coords, 0);
            $y1 = elgg_extract('y1', $coords, 0);
            $x2 = elgg_extract('x2', $coords, 0);
            $y2 = elgg_extract('y2', $coords, 0);
            $thumb_resized = get_resized_image_from_existing_file($filepath, $w, $h, $square, $x1, $y1, $x2, $y2, $upscale);
        } else {
            if (!is_array($coords)) {
                $thumb_resized = get_resized_image_from_existing_file($filepath, $w, $h, $square, 0, 0, 0, 0, $upscale);
            } else {
                continue;
            }
        }
        if ($thumb_resized) {
            $thumb = new ElggFile();
            $thumb->owner_guid = $entity->owner_guid;
            $thumb->setMimeType('image/jpeg');
            $thumb->setFilename($prefix . "{$size}.jpg");
            $thumb->open("write");
            $thumb->write($thumb_resized);
            $thumb->close();
            $icontime = true;
        }
    }
    if ($icontime) {
        $entity->icontime = time();
        return true;
    }
    return false;
}