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

Description

Get size of specified image file

Parameters

ColumnTypeDefaultDescription
$ref int "" Resource ID
$path string "" File path
$extension string "jpg" File extension
$forcefromfile bool false Get info from file instead of database cache

Return

array|bool File size info. Returns false if not available

Location

include/resource_functions.php lines 6404 to 6578

Definition

 
function get_original_imagesize($ref="",$path=""$extension="jpg"$forcefromfile=false)
    {
    
$fileinfo=array();
    if(
$ref=="" || $path==""){return false;}
    global 
$imagemagick_path$imagemagick_calculate_sizes;

    if(!
file_exists($path))
        {
        return 
false;
        }

    
$file=$path;

    
// check for valid image
    
if (function_exists('mime_content_type'))
        {
        
$mime_content_type mime_content_type($file);
        }
    else
        {
        
$mime_content_type get_mime_type($file);
        }

    
$o_size=ps_query("SELECT " columns_in("resource_dimensions") . " FROM resource_dimensions WHERE resource=?",array("i",$ref));
    if(!empty(
$o_size))
        {
        if(
count($o_size)>1)
            {
            
# delete all the records and start fresh. This is a band-aid should there be multiple records as a result of using api_search
            
ps_query("DELETE FROM resource_dimensions WHERE resource=?",array("i",$ref));
            
$o_size=false;
            
$forcefromfile=true;
            }
        else
            {
            
$o_size=$o_size[0];
            }
        }
    else
        {
        
$o_size=false;
        }

    if(
$o_size!==false && !$forcefromfile && $o_size['file_size'] > 0)
        {
        
$fileinfo[0]=$o_size['file_size'];
        
$fileinfo[1]=$o_size['width'];
        
$fileinfo[2]=$o_size['height'];
        return 
$fileinfo;
        }

    
$filesize=filesize_unlimited($file);

    
$is_image strpos($mime_content_type'image/') !== false;
    
# imagemagick_calculate_sizes is normally turned off
    
if ($is_image && isset($imagemagick_path) && $imagemagick_calculate_sizes)
        {
        
# Use ImageMagick to calculate the size
        
$prefix '';
        
# Camera RAW images need prefix
        
if (preg_match('/^(dng|nef|x3f|cr2|crw|mrw|orf|raf|dcr)$/i'$extension$rawext)) { $prefix $rawext[0] .':'; }

        
# Locate imagemagick.
        
$identify_fullpath get_utility_path("im-identify");
        if (
$identify_fullpath==false) {exit("Could not find ImageMagick 'identify' utility at location '$imagemagick_path'.");}
        
# Get image's dimensions.
        
$identcommand $identify_fullpath ' -format %wx%h 'escapeshellarg($prefix $file) .'[0]';
        
$identoutput=run_command($identcommand);
        
preg_match('/^([0-9]+)x([0-9]+)$/ims',$identoutput,$smatches);
        if (
count($smatches) < 3) {
            return 
false;
        }
        @list(,
$sw,$sh) = $smatches;
        if ((
$sw!='') && ($sh!=''))
            {
            if(!
$o_size)
                {
                
ps_query("insert into resource_dimensions (resource, width, height, file_size) values(?, ?, ?, ?)",array("i",$ref,"i",$sw,"i",$sh,"i",(int)$filesize));
                }
            else
                {
                
ps_query("update resource_dimensions set width=?, height=?, file_size=? where resource=?'",array("i",$sw,"i",$sh,"i",(int)$filesize,"i",$ref));
                }
            }
        }
    else
        {
        
# check if this is a raw file.
        
$rawfile false;
        if (
preg_match('/^(dng|nef|x3f|cr2|crw|mrw|orf|raf|dcr)$/i'$extension$rawext)){$rawfile=true;}

        
# Use GD to calculate the size
        
$GLOBALS["use_error_exception"] = true;
            try
                {
                list(
$sw,$sh) = getimagesize($file);
                }
            catch (
Exception $e)
                {
                
$returned_error $e->getMessage();
                
debug("get_original_imagesize: Unable to get image size for file: $file  -  $returned_error");
                }
        unset(
$GLOBALS["use_error_exception"]);
        if ((isset(
$sw) && isset($sh)) && !$rawfile)
            {
            if(!
$o_size)
                {
                
ps_query("insert into resource_dimensions (resource, width, height, file_size) values(?, ?, ?, ?)",array("i",$ref,"i",$sw,"i",$sh,"i",(int)$filesize));
                }
            else
                {
                
ps_query("update resource_dimensions set width=?, height=?, file_size=? where resource=?",array("i",$sw,"i",$sh,"i",(int)$filesize,"i",$ref));
                }
            }
        else
            {
            
# Assume size cannot be calculated.
            
$sw="?";$sh="?";

            global 
$ffmpeg_supported_extensions;
            if (
in_array(strtolower($extension), $ffmpeg_supported_extensions) && function_exists('json_decode'))
                {
                
$file=get_resource_path($ref,true,"",false,$extension);
                
$ffprobe_array=get_video_info($file);

                
# Different versions of ffprobe store the dimensions in different parts of the json output. Test both.
                
if (!empty($ffprobe_array['width'] )) { $sw intval($ffprobe_array['width']);  }
                if (!empty(
$ffprobe_array['height'])) { $sh intval($ffprobe_array['height']); }
                if (isset(
$ffprobe_array['streams']) && is_array($ffprobe_array['streams']))
                    {
                    foreach( 
$ffprobe_array['streams'] as $stream )
                        {
                        if (!empty(
$stream['codec_type']) && $stream['codec_type'] === 'video')
                            {
                            
$sw intval($stream['width']);
                            
$sh intval($stream['height']);
                            break;
                            }
                        }
                    }
                }
            if (
$sw!=='?' && $sh!=='?')
                {
                
# Size could be calculated after all
                
if(!$o_size)
                    {
                    
ps_query("INSERT INTO resource_dimensions (resource, width, height, file_size) VALUES (?, ?, ?, ?)",array("i",$ref,"i",$sw,"i",$sh,"i",(int)$filesize));
                    }
                else
                    {
                    
ps_query("UPDATE resource_dimensions SET width=?, height=?, file_size=? WHERE resource=?", array("i",$sw,"i",$sh,"i",$filesize,"i",$ref));
                    }
                }
            else
                {
                
# Size cannot be calculated.
                
$sw="?";$sh="?";
                if(!
$o_size)
                    {
                    
# Insert a dummy row to prevent recalculation on every view.
                    
ps_query("INSERT INTO resource_dimensions (resource, width, height, file_size) VALUES (?,'0', '0', ?)",array("i",$ref,"i",$filesize));
                    }
                else
                    {
                    
ps_query("UPDATE resource_dimensions SET width='0', height='0', file_size=? WHERE resource=?",array("i",$filesize,"i",$ref));
                    }
                }
            }
        }

    
$fileinfo[0]=$filesize;
    
$fileinfo[1]=$sw;
    
$fileinfo[2]=$sh;
    return 
$fileinfo;
    }

This article was last updated 4th December 2024 08:35 Europe/London time based on the source file dated 28th November 2024 12:10 Europe/London time.