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_page_count()

Parameters

ColumnTypeDefaultDescription
$resource
$alternative -1

Location

include/resource_functions.php lines 6206 to 6275

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");
        }
    elseif (
$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");
        
        
$cmdparams = [
            
'[FILE]' => new CommandPlaceholderArg($file'is_valid_rs_path'),
        ];
        if (
$exiftool_fullpath == false) {
            
# Try with ImageMagick instead
            
$command get_utility_path("im-identify") . ' -format %n [FILE]';
            
$pages trim(run_command($commandfalse$cmdparams));
        } else {
            
$command $exiftool_fullpath " -sss -pagecount [FILE]";
            
$output run_command($commandfalse$cmdparams);
            
$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 17th November 2024 15:35 Europe/London time based on the source file dated 15th November 2024 18:30 Europe/London time.