Ejemplo n.º 1
0
function attach_thumbnail($media_work, $shim)
{
    // create image file in the vimeo temp directory
    $image_url = $shim->get_thumbnail($media_work->get_value('entry_id'));
    if ($image_url) {
        $tmp_path = VimeoShim::get_temp_dir() . 'tmp_thumbnail_' . $media_work->get_value('id');
        $f = fopen($tmp_path, 'w');
        $contents = get_reason_url_contents($image_url);
        fwrite($f, $contents);
        fclose($f);
        $user = new entity($media_work->get_value('created_by'));
        $username = $user->get_value('name');
        // Create a reason entity out of the temp image file
        if (!empty($tmp_path) && file_exists($tmp_path) && $username) {
            if ($id = create_image_entity($media_work, $username)) {
                reason_include_once('content_managers/image.php3');
                $im = new ImageManager();
                $im->thumbnail_width = REASON_STANDARD_MAX_THUMBNAIL_WIDTH;
                $im->thumbnail_height = REASON_STANDARD_MAX_THUMBNAIL_HEIGHT;
                $im->max_width = REASON_STANDARD_MAX_IMAGE_WIDTH;
                $im->max_height = REASON_STANDARD_MAX_IMAGE_HEIGHT;
                $im->load_by_type(id_of('image'), $id, $media_work->get_value('created_by'));
                $im->handle_standard_image($id, $tmp_path);
                $im->create_default_thumbnail($id);
                $values = array();
                foreach ($im->get_element_names() as $element_name) {
                    $values[$element_name] = $im->get_value($element_name);
                }
                reason_update_entity($id, get_user_id($username), $values, false);
                create_relationship($media_work->get_value('id'), $id, relationship_id_of('av_to_primary_image'));
            }
        }
    } else {
        echo date(DATE_RFC822) . ': No thumbnail url found for media work with id ' . $media_work->get_value('entry_id') . "\n";
    }
}
/**
 * Attempt to attach a thumbnail to the media work.
 *
 * @param $obj object
 * @param $media_work entity
 * @param $netid string
 */
function attach_thumbnail($obj, $media_work, $netid)
{
    // check to see if this had thumbnails generated
    if (property_exists($obj, 'thumbnails')) {
        // First, create temp file
        $tmp_path = ZencoderShim::get_temp_dir() . 'temp_media_image_' . $media_work->get_value('entry_id') . '.jpg';
        $f = fopen($tmp_path, 'w');
        $image_url = $obj->thumbnails[0]->images[0]->url;
        $contents = get_reason_url_contents($image_url);
        fwrite($f, $contents);
        fclose($f);
        // Then, create image entity.  Attach it to the media work, too.
        if (!empty($tmp_path) and file_exists($tmp_path)) {
            // Create a new entity for the image
            if ($id = create_image_entity($media_work, $netid)) {
                $im = new ImageManager();
                //$im->convert_non_web_to = $this->convert_non_web_to;
                $im->thumbnail_width = REASON_STANDARD_MAX_THUMBNAIL_WIDTH;
                $im->thumbnail_height = REASON_STANDARD_MAX_THUMBNAIL_HEIGHT;
                $im->max_width = REASON_STANDARD_MAX_IMAGE_WIDTH;
                $im->max_height = REASON_STANDARD_MAX_IMAGE_HEIGHT;
                $im->load_by_type(id_of('image'), $id, get_user_id($netid));
                $im->handle_standard_image($id, $tmp_path);
                //$im->handle_original_image($id, $image);
                $im->create_default_thumbnail($id);
                $dimensions = $obj->thumbnails[0]->images[0]->dimensions;
                $arr = explode('x', $dimensions);
                $thumb_width = intval($arr[0]);
                $thumb_height = intval($arr[1]);
                if ($thumb_width > $im->max_width || $thumb_height > $im->max_height) {
                    $image_path = PHOTOSTOCK . reason_format_image_filename($id, 'jpg');
                    $original_path = add_name_suffix($image_path, '_orig');
                    @copy($image_path, $original_path);
                    resize_image($image_path, $im->max_width, $im->max_height);
                }
                // Pull the values generated in the content manager
                // and save them to the entity
                $values = array();
                foreach ($im->get_element_names() as $element_name) {
                    $values[$element_name] = $im->get_value($element_name);
                }
                reason_update_entity($id, get_user_id($netid), $values, false);
                // Remove any existing association with an image and replace it with this new one
                delete_relationships(array('entity_a' => $media_work->id(), 'type' => relationship_id_of('av_to_primary_image')));
                create_relationship($media_work->id(), $id, relationship_id_of('av_to_primary_image'));
            } else {
                trigger_error('Failed to create image entity.');
            }
        } else {
            trigger_error('No path to image: ' . $tmp_path);
        }
    }
}