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 5507 to 5580

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'];

    if (
$alternative!=-1)
        {
        
$pagecount=ps_value("select page_count value from resource_alt_files where ref=?",array("i",$alternative),"");
        
$resource=get_alternative_file($ref,$alternative);
        }
    else
        {
        
$pagecount=ps_value("select page_count value from resource_dimensions where resource=?", array("i",$ref), "");
        }
    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=ps_value("select ref value from resource_alt_files where resource=? and unoconv=1",array("i",$ref), "");
        
$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)
        {
        
ps_query("update resource_alt_files set page_count=? where ref=?",array("i",$pages,"i",$alternative));
        }
    else
        {
        
ps_query("update resource_dimensions set page_count=? where resource=?",array("i",$pages,"i",$ref));
        }
    return 
$pages;
    }

This article was last updated 3rd October 2022 12:05 Europe/London time based on the source file dated 30th September 2022 14:20 Europe/London time.