Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

get_resource_preview()

Description

Get the largest available preview URL for the given resource and the given array of sizes

Parameters

ColumnTypeDefaultDescription
$resource array Array of resource data from get_resource_data() or search results
$sizes array [] Array of size IDs to look through, in order of size. If not provied will use all sizes
$access int -1 Resource access
$watermark bool false Look for watermarked versions?
$page int 1 Page to look for

Return

string | bool URL, or false if no image is found

Location

include/resource_functions.php lines 9525 to 9613

Definition

 
function get_resource_preview(array $resource,array $sizes = [], int $access = -1bool $watermark falseint $page 1)
    {
    global 
$userref$open_access_for_contributor;
    if(empty(
$sizes))
        {
        
$sizes array_reverse(array_column(get_all_image_sizes(),"id"));
        }

    
$preview["url"] = "";
    if (isset(
$resource['thm_url']))
        {
        
// Option to override thumbnail image in search results, e.g. by plugin using process_search_results hook
        
$preview["url"] = $resource['thm_url'];
        
$preview["height"] = $resource["thumb_height"];
        
$preview["width"] = $resource["thumb_width"];
        }
    else
        {
        if((int)
$resource['has_image'] !== RESOURCE_PREVIEWS_NONE)
            {
            
// If configured, try and use a preview from a related resource
            
$pullresource related_resource_pull($resource);
            if(
$pullresource !== false)
                {
                
$resource $pullresource;
                }
            }

        if(
$access == -1)
            {
            
$access get_resource_access($resource);
            }

        
// Work out image to use.
        
if($watermark !== '')
            {
            
$use_watermark check_use_watermark();
            }
        else
            {
            
$use_watermark false;
            }
        
$validimage false;
        foreach(
$sizes as $size)
            {
            if(
resource_has_access_denied_by_RT_size($resource['resource_type'], $size)
               && !(
$open_access_for_contributor && $userref == $resource['created_by']) )
                {
                continue;
                }

            
// Check that file actually exists
            
$img_file get_resource_path(
                
$resource['ref'],
                
true,
                
$size,
                
false,
                
$resource['preview_extension'],
                
true,
                
$page,
                
$use_watermark,
                
$resource['file_modified']
            );
            if(
file_exists($img_file))
                {
                
$preview["path"] = $img_file;
                
$preview["url"] = get_resource_path($resource['ref'],false,$size ,false,$resource['preview_extension'],true,$page,$use_watermark,$resource['file_modified']);
                
$GLOBALS["use_error_exception"] = true;
                try
                    {
                    list(
$preview["width"], $preview["height"]) = getimagesize($img_file);
                    
$validimage true;
                    }
                catch (
Exception $e)
                    {
                    
$returned_error $e->getMessage();
                    
debug("get_resource_preview - getimagesize(): " $returned_error);
                    }
                unset(
$GLOBALS["use_error_exception"]);
                break;
                }
            }
        }
    if(!
$validimage)
        {
        return 
false;
        }
    return 
$preview;
    }

This article was last updated 4th December 2024 08:35 Europe/London time based on the source file dated 28th November 2024 12:10 Europe/London time.