+ /**
+ * Format the attachment placeholder img with the final version.
+ *
+ * @param DOMElement $img
+ * @param MediaFile $media
+ */
+ private function formatAttachment($img, $media)
+ {
+ $dom = $img->ownerDocument;
+ $link = $dom->createElement('a');
+ $link->setAttribute('href', $media->fileurl);
+
+ if ($this->isEmbeddable($media)) {
+ common_log(LOG_INFO, 'QQQQQ');
+ // Fix the the <img> attributes and wrap the link around it...
+ $this->insertImage($img, $media);
+ common_log(LOG_INFO, 'QQQQQ A!');
+ try {
+ $dom->replaceChild($link, $img); //it dies in here?!
+ } catch (Exception $wtf) {
+ common_log(LOG_ERR, 'QQQ WTF? ' . $wtf->getMessage());
+ }
+ common_log(LOG_INFO, 'QQQQQ B!');
+ $link->appendChild($img);
+ common_log(LOG_INFO, 'QQQQQ C!');
+ } else {
+ common_log(LOG_INFO, 'QQQQQ X');
+ // Not an image? Replace it with a text link.
+ $text = $dom->createTextNode($media->shortUrl());
+ $link->appendChild($text);
+ $dom->replaceChild($link, $img);
+ }
+ }
+
+ /**
+ * Is this media file a type we can display inline?
+ *
+ * @param MediaFile $media
+ * @return boolean
+ */
+ private function isEmbeddable($media)
+ {
+ $showable = array('image/png',
+ 'image/gif',
+ 'image/jpeg');
+ return in_array($media->mimetype, $showable);
+ }
+
+ /**
+ * Rewrite and resize a placeholder image element to match the uploaded
+ * file. If the holder is smaller than the file, the file is scaled to fit
+ * with correct aspect ratio (but will be loaded at full resolution).
+ *
+ * @param DOMElement $img
+ * @param MediaFile $media
+ */
+ private function insertImage($img, $media)
+ {
+ $img->setAttribute('src', $media->fileRecord->url);
+
+ $holderWidth = intval($img->getAttribute('width'));
+ $holderHeight = intval($img->getAttribute('height'));
+
+ $path = File::path($media->filename);
+ $imgInfo = getimagesize($path);
+
+ if ($imgInfo) {
+ $origWidth = $imgInfo[0];
+ $origHeight = $imgInfo[1];
+
+ list($width, $height) = $this->sizeBox(
+ $origWidth, $origHeight,
+ $holderWidth, $holderHeight);
+
+ $img->setAttribute('width', $width);
+ $img->setAttribute('height', $height);
+ }
+ }
+
+ /**
+ *
+ * @param int $origWidth
+ * @param int $origHeight
+ * @param int $holderWidth
+ * @param int $holderHeight
+ * @return array($width, $height)
+ */
+ private function sizeBox($origWidth, $origHeight, $holderWidth, $holderHeight)
+ {
+ $holderAspect = $holderWidth / $holderHeight;
+ $origAspect = $origWidth / $origHeight;
+ if ($origAspect >= 1.0) {
+ // wide image
+ if ($origWidth > $holderWidth) {
+ return array($holderWidth, intval($holderWidth / $origAspect));
+ } else {
+ return array($origWidth, $origHeight);
+ }
+ } else {
+ if ($origHeight > $holderHeight) {
+ return array(intval($holderWidth * $origAspect), $holderHeight);
+ } else {
+ return array($origWidth, $origHeight);
+ }
+ }
+ }
+