18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Working with Files and Images<br />

/**<br />

* Overrides getExternalUrl().<br />

*<br />

* Return the HTML URL of a Twitpic image.<br />

*/<br />

function getExternalUrl() {<br />

// Get image_id and size from the URI into an array.<br />

$target = file_uri_target($this->uri);<br />

$options = array_combine(<br />

array('image_id', 'size'),<br />

explode('/', $target)<br />

);<br />

// If no size is specified, default to thumb.<br />

if (empty($options['size'])) {<br />

$options['size'] = 'thumb';<br />

}<br />

// Create the URL<br />

$url = 'http://twitpic.com/show/' . $options['size'] . '/' .<br />

$options['image_id'];<br />

return $url;<br />

}<br />

/**<br />

* Support for fopen(), file_get_contents(),etc.<br />

*/<br />

public function stream_open($uri, $mode, $options, &$opened_path) {<br />

$allowed_modes = array('r', 'rb');<br />

if (!in_array($mode, $allowed_modes)) {<br />

return FALSE;<br />

}<br />

$this->uri = $uri;<br />

$url = $this->getExternalUrl();<br />

$this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($url,<br />

$mode) : @fopen($url, $mode);<br />

return (bool)$this->handle;<br />

}<br />

}<br />

[ 322 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!