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
- Installing and using under Windows
- Installing and Using class_dicom.php on Linux
- Send all DICOM files in a directory
- How to Write Your Own PACS in PHP
- How to Convert Multi-Frame Cine Loops to AVI or MP4 Format Using PHP
- How to batch convert DICOM to JPEG using IrfanView
- How to sort a large directory of DICOM files.
- DICOM Sample Images
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
Hi Dean,
Seems to be good work.
Gonna try it out!
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
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
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.
i run the code and received the output:
dicom_convert Object ( [file] => xr_chest.dcm [jpg_file] => xr_chest.dcm.jpg [tn_file] => xr_chest.dcm_tn.jpg [jpg_quality] => 100 [tn_size] => 125 )
now how to show the image or store this image on any location in jpg formate.
Hello,
when i open the test :
TEST:
Array
(
)
Name:
HOW I FIX PLS???
Sounds like either the Offis DICOM Toolkit isn’t installed or you need to point the TOOLKIT_DIR define to the correct location.
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.
Thanks for showing this off. It’s awesome.
Works well on CentOS 6.4. Thanks!
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: mundocobol@hotmail.com
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.
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.
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.
Hi Dean,
Have a look at https://github.com/ivmartel/dwv
Pure HTML5 and Javascript.
That’s an interesting viewer project. https://github.com/chafey/cornerstone/wiki/ImageLoader is another good project.
Works great thanks!
Hi
this variable is missing $compress_cmd
I’ve said it before… great work! Below is the DICOM Class rewritten in C# for .Net Developers. I only did not rewrite the Convert From JPEG and XML as well as DICOM TO Movie. I don’t use these ones. I tried to follow your methods, with minor changes. I also added some support to grow the library into supporting JPEG2000 via the use of GDCM or some other tool. Hope you all like!
This is awesome. Thanks!
When i am running this jpg_to_dcm.php i am getting below error.Anyone please help
#!/usr/bin/php New file is ‘C:/dcmtk/bin/xml2dcm.exe’ is not recognized as an internal or external command, operable program or batch file.
Take a look at this: https://deanvaughan.org/wordpress/2012/07/using-class_dicom-php-in-windows/
Dean – have you tried creating a multi-frame Secondary Capture DICOM file, from multiple JPGs? It appears that your code will create a single frame, but wondered if you had tried to create a file with SOPClassUID of (for example) 1.2.840.10008.5.1.4.1.1.7.4?
I have not. I don’t believe the toolkit I rely on has the ability to do this.
Hi Dean – Great work and thanks for sharing!
At present I am trying to read RTSTRUCT dcm files containing contour data (to date with images I have had no issues using class_dicom). What I have found is that there seems to be a limit to the length of contour data that can be read and anything that is longer than this is just ignored (not even an empty slot in the array). So what I end up with is different length arrays for 3006,0050 (ContourData) and 3006,0046 (NumberOfContourPoints), for example. I have tried using print_r($d->tags) as well as $d->get_tag (‘####’,’####’)); and get the same result. There seems to be a limit of about 3,725 characters for any ContourData string (about 130 co-ordinates using 8 digit numbers). Are you aware of any such limits? Is there any way around this that you know of?
Thanks for any input!
Have you tried using the DCMTK to directly extract the header data? From the command line dcm2xml should convert the header into XML for you.
Thanks for getting back to me Dean. No I haven’t done that yet, I’ll take a look at it. I was hoping that there may be a size limiter switch in the toolkit that I could just remove, but projects like this one never seem that simple for some reason.
I made a change to class_dicom:
$dump_cmd = BIN_DCMDUMP . ” -M +L +Qn $file”; => $dump_cmd = BIN_DCMDUMP . ” +M +L +Qn $file”;
This has produced the desired output but now I will have to check to see if there are any knock-on effects in terms of speed and memory, particularly with bitmap data, or whether they can be lived with. At least moving in the right direction. Cheers!
This is great work. But not working with tranfer syntax .90. Any solution
http://sourceforge.net/projects/gdcm/ Grass Roots DICOM will work with JPEG2k.
how to make a worklist query / retrieve with this library?
i want to print dicom file in php ,,so i download given class..but i cannot convert it to jpeg..please help me
I wonder to konw if this project you wrote is supported for Chinese ,because when I get_tags(); if the PatientName is Chinese,it will be Chinese garbled, are there any way to slove this problem?
Thanks Dean for this excellent works! Currently, I am building a DICOM viewer, I want to parse the tags and display it based on data dictionary set by DICOM. For example: 0010,0010 is “Patient’s Name”. Is the lookup function is in DCMTK? Or should I map it manually. Thanks.
Not that I’m aware of, you will need to map manually.
Thank you!
Hello,
is it possible to create dicom encapsulated pdf?
Hello,
There is no means in the class for an encapsulated PDF. However, I believe the DCMTK has some command line programs that will allow you to do this.
Hello, why I can’t get array of tags even one tag?
I’m using this block of code:
$dt = dicom_tag();
$dt->file = ‘files/uploads/13a773e4c8ddc5ef271c2c5077948fde.dcm’;
$tags = $dt->load_tags();
$patient_id = $dt->get_tag(‘0010’, ‘0020’);
but in $patient_id is empty string….could you help me please))
If you print_r($dt->tags()) do you see the tag in the array?
Is the path to your file correct in relation to where your script is being ran? What happens if you provide an absolute path instead?
Nice work.I have one query.I get a error on window server.This function is blocked by group policy.I think popen function is blocked by the server.I need help.please give me a solution
You would need to speak to the server administrator to have the functionality allowed.
Hi Dean,
I need help.Class_dicom works fine on localhost but doesn’t work on server.
i am using go-daddy server(Linux platform).
On localhost i tried on both window and Linux it works.
____________________________________________________________________________________________________
Output on server:-
Lib Files Directory:-/home/megasoftweb/public_html/test/MEDIC_SOFT/dcmtk/bin
Server Used:-Linux
File used to read dicom file:-/home/megasoftweb/public_html/test/MEDIC_SOFT/dcmtk/bin/dcmdump
executing following command on file using PHP funtion popen
/home/megasoftweb/public_html/test/MEDIC_SOFT/dcmtk/bin/dcmdump -M +L +Qn dean.dcm 2>&1
Data From File:-
Single Tag After Reading Dicom file
____________________________________________________________________________________________________
Output on Localhost:-
Lib Files Directory:-C:/xampp/htdocs/dicom/dcmtk/bin
Server Used:-Linux
File used to read dicom file:-C:/xampp/htdocs/dicom/dcmtk/bin/dcmdump
executing following command on file using PHP funtion popen
C:/xampp/htdocs/dicom/dcmtk/bin/dcmdump -M +L +Qn dean.dcm 2>&1
Data From File:- # Dicom-File-Format # Dicom-Meta-Information-Header # Used TransferSyntax: Little Endian Explicit (0002,0000) UL 210 # 4, 1 FileMetaInformationGroupLength (0002,0001) OB 00\01 # 2, 1 FileMetaInformationVersion (0002,0002) UI =ComputedRadiographyImageStorage # 26, 1 MediaStorageSOPClassUID (0002,0003) UI [1.2.826.0.1.3680043.2.1074.90089.1.71.10001008.1] # 48, 1 MediaStorageSOPInstanceUID (0002,0010) UI =JPEGBaseline # 22, 1 TransferSyntaxUID (0002,0012) UI [1.2.276.0.7230010.3.0.3.6.2] # 28, 1 ImplementationClassUID (0002,0013) SH [OFFIS_DCMTK_362] # 16, 1 ImplementationVersionName (0002,0016) AE [SXR_DEMO] # 8, 1 SourceApplicationEntityTitle # Dicom-Data-Set # Used TransferSyntax: JPEG Baseline (0008,0016) UI =ComputedRadiographyImageStorage # 26, 1 SOPClassUID (0008,0018) UI [1.2.826.0.1.3680043.2.1074.90089.1.71.10001008.1] # 48, 1 SOPInstanceUID (0008,0020) DA [20110310] # 8, 1 StudyDate (0008,0030) TM [104637] # 6, 1 StudyTime (0008,0050) SH [demofry80666] # 12, 1 AccessionNumber (0008,0060) CS [CR] # 2, 1 Modality (0008,0064) CS [DI] # 2, 1 ConversionType (0008,0070) LO [iCRco, Inc.] # 12, 1 Manufacturer (0008,0080) LO [DEANLAND, AR] # 12, 1 InstitutionName (0008,0090) PN (no value available) # 0, 0 ReferringPhysicianName (0008,1010) SH [SXR] # 4, 1 StationName (0008,1030) LO (no value available) # 0, 0 StudyDescription (0008,1090) LO [CR1] # 4, 1 ManufacturerModelName (0010,0010) PN [VAUGHAN DEAN] # 12, 1 PatientName (0010,0020) LO [345345] # 6, 1 PatientID (0010,0030) DA [19800301] # 8, 1 PatientBirthDate (0010,0040) CS [M] # 2, 1 PatientSex (0018,0015) CS (no value available) # 0, 0 BodyPartExamined (0018,1004) LO [123] # 4, 1 PlateID (0018,1164) DS [0.357750\0.357750] # 18, 2 ImagerPixelSpacing (0018,1400) LO [[APO:(0,-1,-1)]~] # 16, 1 AcquisitionDeviceProcessingDescription (0018,5101) CS [Invalid] # 8, 1 ViewPosition (0020,000d) UI [1.2.826.0.1.3680043.2.1074.90089.10003.100.19318.3121114637] # 60, 1 StudyInstanceUID (0020,000e) UI [1.2.826.0.1.3680043.2.1074.90089.10003100.19318.1] # 50, 1 SeriesInstanceUID (0020,0010) SH [10003100] # 8, 1 StudyID (0020,0011) IS [1001] # 4, 1 SeriesNumber (0020,0013) IS [1] # 2, 1 InstanceNumber (0020,0020) CS (no value available) # 0, 0 PatientOrientation (0028,0002) US 1 # 2, 1 SamplesPerPixel (0028,0004) CS [MONOCHROME2] # 12, 1 PhotometricInterpretation (0028,0010) US 2008 # 2, 1 Rows (0028,0011) US 2444 # 2, 1 Columns (0028,0034) IS [1\1] # 4, 2 PixelAspectRatio (0028,0100) US 8 # 2, 1 BitsAllocated (0028,0101) US 8 # 2, 1 BitsStored (0028,0102) US 7 # 2, 1 HighBit (0028,0103) US 0 # 2, 1 PixelRepresentation (0028,1050) DS [128.000000] # 10, 1 WindowCenter (0028,1051) DS [252.000000] # 10, 1 WindowWidth (0028,2110) CS [01] # 2, 1 LossyImageCompression (0038,0500) LO [HI] # 2, 1 PatientState (7fe0,0010) OB (PixelSequence #=2) # u/l, 1 PixelData (fffe,e000) pi (no value available) # 0, 1 Item (fffe,e000) pi (not loaded) # 883958, 1 Item (fffe,e0dd) na (SequenceDelimitationItem) # 0, 0 SequenceDelimitationItem
Single Tag After Reading Dicom file:-VAUGHAN DEAN
____________________________________________________________________________________________________
please help me.
Thank you
It’s difficult to say what is going on. It could be the server doesn’t allow you to execute programs, but it is most likely something to do with absolute vs relative paths to the DICOM file.
Hi Thanks for replying.I tried both absolute vs relative paths to the DICOM file but result is same.
Can yo tell me any server on which it works.Or any other way to read Dicom file?
Thank you
Any chance your server doesn’t allow you to popen()?