Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource 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?

Return

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

Location

include/resource_functions.php lines 9413 to 9499

Definition

 
function get_resource_preview(array $resource,array $sizes = [], int $access = -1bool $watermark false)
    {
    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))
                {
                continue;
                }

            
// Check that file actually exists
            
$img_file get_resource_path(
                
$resource['ref'],
                
true,
                
$size,
                
false,
                
$resource['preview_extension'],
                
true,
                
1,
                
$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,1,$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 14th July 2024 19:05 Europe/London time based on the source file dated 10th July 2024 11:30 Europe/London time.