/** * Gets the filename of an image relative to the photostock directory. * @param mixed $image the ID of an image entity or an image entity object * @param string $size the desired scaling of the image: thumbnail/thumb/tn, * standard/default, or original/full (which is not always * available) * @return string the image's filename, or NULL if an invalid value was given * for $image */ function reason_get_image_filename($image, $size='standard') { if (is_object($image)) { if (!is_callable(array(&$image, 'get_value'))) { trigger_error('given object does not look like an entity to me: '. var_export($image, true), WARNING); return null; } } else if (is_numeric($image)) { $id = (int) $image; $image = new Entity($id); if (!$image->get_values()) { trigger_error('no entity exists with ID '.$id, WARNING); return null; } } else { trigger_error('must pass an entity or an image entity ID; instead '. 'got '.var_export($image, true), WARNING); return null; } if ($image->get_value('type') != id_of('image')) { trigger_error('given entity is not an image; is actually a '. var_export($image->get_value('type'), true), WARNING); return null; } /* Determine what size image was requested; look up the corresponding image type in DB. Default is to return the standard-sized image type ( i.e. $image->get_value('image_type') ) in the case that new entity fields don't exist yet (thumbnail_image_type and original_image_type), or that these fields are null */ $normalized_size = _normalize_image_size_name($size); $image_type = null; if( $normalized_size == 'tn' ) { $image_type = $image->get_value('thumbnail_image_type'); } elseif( $normalized_size == 'orig' ) { $image_type = $image->get_value('original_image_type'); } if( $image_type == null || $image_type == false) { $image_type = $image->get_value( 'image_type' ); } return reason_format_image_filename($image->id(), $image_type, $size); }
/** * This function should be overloaded in each new ReasonJSON type. It is the * mapping of values from the Reason entities to the JSON object. */ function transform_item($v) { $newArray = array(); $newArray['id'] = $v->get_value('id'); $newArray['name'] = $v->get_value('name'); $newArray['description'] = $v->get_value('description'); $newArray['pubDate'] = $v->get_value('creation_date'); $newArray['lastMod'] = $v->get_value('last_modified'); $newArray['link'] = '//' . REASON_HOST . WEB_PHOTOSTOCK . reason_format_image_filename($v->id(), $v->get_value('image_type'), 'standard'); $newArray['thumbnail'] = $v->get_value('thumbnail_image_type') ? '//' . REASON_HOST . WEB_PHOTOSTOCK . reason_format_image_filename($v->id(), $v->get_value('thumbnail_image_type'), 'thumbnail') : $newArray['link']; $newArray['content'] = $v->get_value('content'); $newArray['keywords'] = $v->get_value('keywords'); return $newArray; }
/** * 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); } } }
function process_form(&$disco) { $images = $this->get_images(); $thumbnail_image = $images[$disco->get_value('thumbnails')]; $tmp_path = WEB_PATH . trim_slashes(WEB_TEMP) . '/temp_media_image_' . uniqid() . '.jpg'; $contents = get_reason_url_contents($thumbnail_image); if (!$contents) { trigger_error('Unable to retrieve file contents for ' . $thumbnail_image . '. The url is invalid or S3 is being slow.'); return; } $f = fopen($tmp_path, 'w'); fwrite($f, $contents); fclose($f); if (!empty($tmp_path) and file_exists($tmp_path)) { // Create a new entity for the image if ($id = $this->create_image_entity()) { $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, $this->user->id()); $im->handle_standard_image($id, $tmp_path); //$im->handle_original_image($id, $image); $im->create_default_thumbnail($id); $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, $this->user->id(), $values, false); // Remove any existing association with an image and replace it with this new one delete_relationships(array('entity_a' => $this->media_work->id(), 'type' => relationship_id_of('av_to_primary_image'))); create_relationship($this->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); } }
function associate_image($media_work, $data) { $tmp_path = KalturaShim::get_temp_dir() . 'temp_media_image_' . $media_work->get_value('entry_id') . '.jpg'; $f = fopen($tmp_path, 'w'); $thumb_opts = array('width' => $data['width'], 'quality' => 100); $image_url = $this->kaltura_shim->get_thumbnail($media_work->get_value('entry_id'), $data['length_in_msecs'] / 2.0 / 1000.0, $thumb_opts); $contents = get_reason_url_contents($image_url); fwrite($f, $contents); fclose($f); if (!empty($tmp_path) and file_exists($tmp_path)) { // Create a new entity for the image if ($id = $this->create_image_entity($media_work, $data)) { $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($data['puser_id'])); $im->handle_standard_image($id, $tmp_path); //$im->handle_original_image($id, $image); $im->create_default_thumbnail($id); if ($data['width'] > $im->max_width || $data['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($data['puser_id']), $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); } }