Skip to content

Instantly share code, notes, and snippets.

@jessemartinez
Created March 4, 2021 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessemartinez/167465e25cfe72e811e09915c1dacd85 to your computer and use it in GitHub Desktop.
Save jessemartinez/167465e25cfe72e811e09915c1dacd85 to your computer and use it in GitHub Desktop.
used in Jasper Reports to pull in an image from a CSpace instance (v4.x and earlier)
<?php
# assumes this image will be a jpg...
    header("Content-Type: image/jpg");
# typically use the reader user cspace account
    $serverPrefix = "http://login:password@somehost.foo:8180/cspace-services/blobs/";
// sets the default image size to be retrieved
    $defaultFileSize = "thumbnail";
# default generic image filename. this is retrieved when there a real image can't be found
# could be created with imagemagick ahead of time with this command:
# convert -size 750x700 xc:grey -stroke black -draw "line 0,0 750,700" -draw "line 750,0 0,700" ok.jpg
    $defaultFileName = './ok.jpg';
// parse the URL 'format' param into a string we recognize
    function getImageFormatSizeName($formatSize){
        $myImageSize = $GLOBALS['defaultFileSize'];
# look for variations of known strings
        switch($formatSize){
            case "medium":
            case "Medium":
                $myImageSize = "Medium";
                break;
            case "large":
            case "original":
            case "originaljpeg":
            case "OriginalJpeg":
                $myImageSize = "OriginalJpeg";
                break;
            case "thumbnail":
            case "Thumbnail":
            default:
                $myImageSize = "Thumbnail";
                break;
        }
        return $myImageSize;
    }
// fetches the image based on the blob CSID and the format string
    function getImage($csid, $formatSizeName){
        $myURL = $GLOBALS['serverPrefix'] . $csid . "/derivatives/" . $formatSizeName . "/content";
        $fp = fopen($myURL, 'rb');
        if($fp === false) {
            $fp = fopen($GLOBALS['defaultFileName'], 'rb');
        }
        return $fp;
    }
// checks the validity of the URL params and either fetches the image,
// or fetches the default generic image
    if (isset($_GET['csid'])) {
        $csid = preg_replace('/[^0-9a-fA-F-]/', '', $_GET['csid']);
        $formatSize = $GLOBALS['defaultFileSize'];
        if (isset($_GET['format'])){
            $formatSize = $_GET['format'];
        }
        $formatSizeName = getImageFormatSizeName($formatSize);
        $fp = getImage($csid, $formatSizeName);
    } else {
        $fp = fopen($GLOBALS['defaultFileName'], 'rb');
    }
# can't get to work
    #header("Content-Length: " . filesize($fp));
// push out the image
    fpassthru($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment