Collections functions
General functions
Render functions
Theme permission functions
Resource functions

get_page_count()

Parameters

ColumnTypeDefaultDescription
$resource
$alternative -1

Location

include/resource_functions.php lines 5570 to 5645

Definition

 
function get_page_count($resource,$alternative=-1)
    {
    
# gets page count for multipage previews from resource_dimensions table.
    # also handle alternative file multipage previews by switching $resource array if necessary
    # $alternative specifies an actual alternative file
    
$ref=$resource['ref'];
    
$ref_escaped escape_check($ref);
    
$alternative_escaped escape_check($alternative);

    if (
$alternative!=-1)
        {
        
$pagecount=sql_value("select page_count value from resource_alt_files where ref='{$alternative_escaped}'","");
        
$resource=get_alternative_file($ref,$alternative);
        }
    else
        {
        
$pagecount=sql_value("select page_count value from resource_dimensions where resource='{$ref_escaped}'","");
        }
    if (!empty(
$pagecount)) { return $pagecount; }
    
# or, populate this column with exiftool or image magick (for installations with many pdfs already
    # previewed and indexed, this allows pagecount updates on the fly when needed):
    # use exiftool. 
    
if ($resource['file_extension']=="pdf" && $alternative==-1)
        {
        
$file=get_resource_path($ref,true,"",false,"pdf");
        }
    else if (
$alternative==-1)
        {
        
# some unoconv files are not pdfs but this needs to use the auto-alt file
        
$alt_ref=sql_value("select ref value from resource_alt_files where resource='{$ref_escaped}' and unoconv=1","");
        
$file=get_resource_path($ref,true,"",false,"pdf",-1,1,false,"",$alt_ref);
        }
    else
        {
        
$file=get_resource_path($ref,true,"",false,"pdf",-1,1,false,"",$alternative);
        }

    if (
file_exists($file))
        {
        
# locate exiftool
        
$exiftool_fullpath get_utility_path("exiftool");
        if (
$exiftool_fullpath==false)
            {
            
# Try with ImageMagick instead
            
$command get_utility_path("im-identify") . ' -format %n ' escapeshellarg($file);
            
$pages trim(run_command($command));
            }
        else
            {
            
$command $exiftool_fullpath;    
            
$commandescapeshellarg($command) . " -sss -pagecount " escapeshellarg($file);
            
$output=run_command($command);
            
$pages=str_replace("Page Count","",$output);
            
$pages=str_replace(":","",$pages);
            
$pages=trim($pages);
            }
        if (!
is_numeric($pages))
            {
            
$pages 1// default to 1 page if we didn't get anything back
            
}
        }
    else
        {
        
$pages 1;
        }

    if (
$alternative!=-1)
        {
        
sql_query("update resource_alt_files set page_count='$pages' where ref='{$alternative_escaped}'");
        }
    else
        {
        
sql_query("update resource_dimensions set page_count='$pages' where resource='{$ref_escaped}'");
        }
    return 
$pages;
    }

This article was last updated 20th May 2022 08:05 Europe/London time based on the source file dated 16th May 2022 11:00 Europe/London time.