DICOM PHP Class

class_dicom.php is a PHP class that lets you work with DICOM files within PHP programs. class_dicom.php enables you to retrieve tags, write tags, convert to JPEG, make thumbnails, convert JPEG to DICOM, compress DICOM, uncompress DICOM, receive DICOM files over the network, and send DICOM files over the network. It even comes with several example programs so you can get started writing your own code right away!

This PHP DICOM class has good support for running under Windows and Linux.

As of 2012-06-11 class_dicom.php can convert multi-frame DICOM files into normal video files such as .mp4, .mpg, or .avi. This functionality is driven by ffmpeg and is easily extendable and modifiable. Think of it as DICOM to AVI or DICOM to MPG.

class_dicom.php uses the excellent Offis DICOM Toolkit for it’s nitty gritty DICOM work. Your system will need a reasonably recent version of the DICOM Toolkit in order to use class_dicom.php.

Downloading

You can download class_dicom.php directly from this site or from GitHub.

Tutorials

Specifics

class_dicom.php is made up three classes that handle tags, conversions, and network.

class dicom_tag

  • load_tags(): Loads all of the tags from a DICOM file into an associative array. Example: examples/get_tags.php
  • get_tag(): Returns the value of the group and element you specify. Example: examples/get_tags.php
  • write_tags(): Writes the tags contained in an array you specify to a DICOM file. Example: examples/write_tags.php

class dicom_convert

  • dcm_to_jpg(): DICOM to JPG. Converts a DICOM file into a JPEG file. Example: examples/dcm_to_jpg.php
  • dcm_to_tn(): DICOM to JPG thumbnail. Converts a DICOM file into a JPEG thumb nail. Example: examples/dcm_to_jpg.php
  • jpg_to_dcm(): JPG to DICOM. Takes the demographic info in an array you specify and a JPEG file, produces a DICOM file. Example: examples/jpg_to_dcm.php
  • compress(): Performs a lossless JPEG compression on a DICOM file. Example: examples/compress.php
  • uncompress(): Uncompresses a compressed DICOM file. Example: examples/uncompress.php
  • multiframe_to_video():  Convert a DICOM multi-frame file into a video (.mp4, .avi, .mpg, ect)

class dicom_net

  • store_server(): Starts a DICOM receive network service. Example: examples/store_server.php
  • echoscu(): Sends a DICOM ping (echoscu).
  • send_dcm(): Sends a DICOM file to the host you specify. Example: examples/send_dcm.php

Examples of Examples

These examples and many more are included in the download packages above.

#
# Prints out the DICOM tags in a file specified on the command line
#

require_once('../class_dicom.php');

$file = (isset($argv[1]) ? $argv[1] : '');

if(!$file) {
  print "USAGE: ./get_tags.php \n";
  exit;
}

if(!file_exists($file)) {
  print "$file: does not exist\n";
  exit;
}

$d = new dicom_tag;
$d->file = $file;
$d->load_tags();

print_r($d->tags); // PRINT ALL OF THE TAGS

$name = $d->get_tag('0010', '0010'); // GET A SPECIFIC TAG
print "Name: $name\n";

#
# Creates a jpeg and jpeg thumbnail of a DICOM file
#

require_once('../class_dicom.php');

$file = (isset($argv[1]) ? $argv[1] : '');

if(!$file) {
  print "USAGE: ./dcm_to_jpg.php \n";
  exit;
}

if(!file_exists($file)) {
  print "$file: does not exist\n";
  exit;
}

$d = new dicom_convert;
$d->file = $file;
$d->dcm_to_tn(); // CONVERT TO JPEG AND A JPEG THUMBNAIL
system("ls -lsh $file*"); // List the files we just created

18 comments

  1. Ubuntu 12.04TLS works like a charm.
    Simple to install, instructions were right on.
    Have not yet sent studies to it but will be very soon.
    Great work!!!!
    thank you

  2. Hi seems to be a great work thank you for that
    I am working on a simple dicom viewer via PHP connecting to RIS database , in your wrapper
    DICOM PHP Clas can you create a new function like dcm to jpg($read_dir,$temp_dir)
    read_dir is the datastorage of dicom files , tem_dir is a directory that i can read and write *.jpg
    and displaying on my web page and next delete temp_dir when am finish…this is the scenario
    Thank you Vagelis from Greece

    1. Hi,

      The class won’t do that as is, you could pretty easily write a function to loop through a directory, convert any DICOM files it finds to jpeg, and move those jpegs into another directory.

  3. Thanks for creating this. I used this class to help me sort through and organize about 1TB of images.

    Here’s the code I used.

    <?php
    
    require_once('class_dicom.php');
    
    define('STORAGE', 'X:/sorted');
    
    get_directory("X:/unsorted");
    
    function process_file($file) {
    
      if(!is_dcm($file)) {
        print("Not a DICOM file: $file\n");
        unlink($file);
        return(0);
      }
    
      $d = new dicom_tag;
      $d->file = $file;
      $d->load_tags();
    
      $name = $d->get_tag('0010', '0010');
      $id = $d->get_tag('0010', '0020');
      $modality = $d->get_tag('0008', '0060');
      $appt_date = $d->get_tag('0008', '0020');
      $sop_id = $d->get_tag('0002', '0003');
    
      $year = date('Y', strtotime($appt_date));
      $month = date('m', strtotime($appt_date));
      $day = date('d', strtotime($appt_date));
    
      $storage = STORAGE . "/$year";
      if(!file_exists($storage)) {
        mkdir($storage);
      }
      $storage = $storage . "/$month";
      if(!file_exists($storage)) {
        mkdir($storage);
      }
      $storage = $storage . "/$day";
      if(!file_exists($storage)) {
        mkdir($storage);
      }
    
      $name = str_replace('^', '_', $name);
      $arr_replace = array('^', "'", '"', '`', '/', '\\', '?', ':', ';');
      foreach($arr_replace as $replace) {
        $name = str_replace($replace, '', $name);
        $id = str_replace($replace, '', $id);
      }
    
      $storage = $storage . "/$name" . "_$id";
      if(!file_exists($storage)) {
        mkdir($storage);
      }
    
      $new_file = $modality . "_" . $sop_id . ".dcm";
    
      if(file_exists("$storage/$new_file")) {
        $new_file = $modality . "_" . $sop_id . "_" . rand(1, 1000) . ".dcm";
      }
    
    //  print "$storage/$new_file\n";
    
      if(!rename($file, "$storage/$new_file")) {
        print "Failed $file -> $storage/$new_file";
        exit;
      }
      print ".";
    
    //  print "$name - $storage\n";
      //exit;
    }
    
    function is_dir_empty($dir) {
      if (!is_readable($dir)) return NULL;
      return (count(scandir($dir)) == 2);
    }
    
    function get_directory($dir, $level = 0) {
      $ignore = array( 'cgi-bin', '.', '..' );
      $dh = @opendir($dir);
      while( false !== ( $file = readdir($dh))){
        if( !in_array( $file, $ignore ) ){
          if(is_dir("$dir/$file")) {
            echo "\n$file\n";
            get_directory("$dir/$file", ($level+1));
          }
          else {
            //echo "$spaces $file\n";
            process_file("$dir/$file");
          }
        }
      }
    
      closedir( $dh );
    
      if(is_dir_empty($dir) && $dir != "X:/unsorted") {
        //print "\n-= Removing $dir =-\n";
        rmdir($dir);
    
      }
    
    }
    
    ?>
    
  4. Hello Dean, I’m reading and using your tutorial in the last days. I would like to congratulate you for the work. I’m having some difficulties and would like to take some questions. When I run the file store_server.php the browser does not return response if this running successfully or not. How can I test if I am getting a device Dicom? Another doubt is that I see in the tutorial you talk about the file import.php but found this file to download. If you have more details on how to mount a Dicom server I thank you very much. My email is: [email protected]

    1. Hello,

      store_server.php is meant to be run from the command line. If it is returning immediately you may not have the OFFIS DCMTK installed or your class_dicom.php is not pointing to the location of the DCMTK.

  5. hi DEAN.

    we are impement web-based pacs viewer and view dcm image easily with convert dcm to jpg, but can we view dcm images directly without convert to jpg in php.

    1. I don’t know of a web browser that can natively view DICOM images. I do know there have been some commercial products to view DICOM files via Flash or HTML5, so that’s possible.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>