Skip to content

Instantly share code, notes, and snippets.

@FuzzicalLogic
Created March 16, 2015 13:01
Show Gist options
  • Save FuzzicalLogic/b8ff93dc71a58b9dfc5a to your computer and use it in GitHub Desktop.
Save FuzzicalLogic/b8ff93dc71a58b9dfc5a to your computer and use it in GitHub Desktop.
Sample MODX Revolution Plugin for Binary Resources
<?php
if ($modx->event->name == 'OnLoadWebDocument'
&& $modx->resource->get('type') == 'document') {
$mime = $modx->resource->get('contentType');
$isBinary = $modx->getObject('modContentType', array('mime_type' => $mime))->get('binary');
if ($isBinary == 1) {
$bytes = base64_decode($modx->resource->get('content'));
}
// Inline Disposition is handled differently from Attachment
$rArray = $modx->resource->toArray();
$disposition = $rArray['content_dispo'];
if ($disposiiton == 0) {
// This must be handled explicitly (from here, that is)
header('Content-Type: '. $mime);
// Image-specific Handling
if ($bytes && strpos($mime, 'image/') == 0) {
$img = imagecreatefromstring($bytes);
if ($img) {
// Other Images: Add MIME type and handling
switch($mime) {
case 'image/png':
imagesavealpha($img, true);
imagepng($img, NULL, 0, NULL);
break;
}
imagedestroy($img);
// Stop MODX from mangling the output
@session_write_close();
exit;
}
}
elseif ($bytes) {
// ... typically, we'll just send back the binary data
}
}
elseif ($bytes) {
// ... typically, we'll just send back the binary data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment