get_page_count()

Parameters

ColumnTypeDefaultDescription
$resource
$alternative -1

Location

include/resource_functions.php lines 5666 to 5726

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($reftrue""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($reftrue""false"pdf", -11false""$alt_ref);
    } else {
        
$file get_resource_path($reftrue""false"pdf", -11false""$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) {
            
# 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 14th March 2025 15:05 Europe/London time based on the source file dated 6th March 2025 14:30 Europe/London time.