/**
 * Prepare a media item for showing
 *
 * @param  array    Array of image metadata
 * @param  string   URL for maintenance tasks
 * @return bool
 *
 */
function serendipity_prepareMedia(&$file, $url = '')
{
    global $serendipity;
    static $full_perm = null;
    if ($full_perm === null) {
        $full_perm = serendipity_checkPermission('adminImagesMaintainOthers');
    }
    $sThumbSource = serendipity_getThumbNailPath($file['path'], $file['name'], $file['extension'], $file['thumbnail_name']);
    $file['full_thumb'] = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $sThumbSource;
    $file['full_thumbHTTP'] = $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $sThumbSource;
    if ($file['hotlink']) {
        $file['full_file'] = $file['path'];
        $file['show_thumb'] = $file['path'];
        if (!isset($file['imgsrc'])) {
            $file['imgsrc'] = $file['show_thumb'];
        }
    } else {
        $file['full_file'] = $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $file['path'] . $file['name'] . '.' . $file['extension'];
        $file['show_thumb'] = $file['full_thumbHTTP'];
        if (!isset($file['imgsrc'])) {
            $file['imgsrc'] = $serendipity['uploadHTTPPath'] . $file['path'] . $file['name'] . (!empty($file['thumbnail_name']) ? '.' . $file['thumbnail_name'] : '') . '.' . $file['extension'];
        }
    }
    // Detect PDF thumbs
    if (file_exists($file['full_thumb'] . '.png')) {
        $file['full_thumb'] .= '.png';
        $file['full_thumbHTTP'] .= '.png';
        $file['show_thumb'] .= '.png';
        $sThumbSource .= '.png';
    }
    if (empty($file['realname'])) {
        $file['realname'] = $file['name'] . '.' . $file['extension'];
    }
    $file['diskname'] = $file['name'] . '.' . $file['extension'];
    $file['links'] = array('imagelinkurl' => $file['full_file']);
    $file['dim'] = @getimagesize($file['full_thumb'], $file['thumb_header']);
    $file['dim_orig'] = @getimagesize($serendipity['serendipityPath'] . $file['full_file'], $file['header']);
    $file['is_image'] = serendipity_isImage($file);
    if ($file['is_image']) {
        $file['mediatype'] = 'image';
    } elseif (0 === strpos(strtolower($file['displaymime']), 'video/') || 0 === strpos(strtolower($file['displaymime']), 'application/x-shockwave')) {
        $file['mediatype'] = 'video';
    } elseif (0 === strpos(strtolower($file['displaymime']), 'audio/') || 0 === strpos(strtolower($file['displaymime']), 'application/vnd.rn-') || 0 === strpos(strtolower($file['displaymime']), 'application/ogg')) {
        $file['mediatype'] = 'audio';
    } elseif (0 === strpos(strtolower($file['displaymime']), 'text/')) {
        $file['mediatype'] = 'document';
    } elseif (preg_match('@application/(pdf|rtf|msword|msexcel|excel|x-excel|mspowerpoint|postscript|vnd\\.ms*|powerpoint)@i', $file['displaymime'])) {
        $file['mediatype'] = 'document';
    } elseif (preg_match('@application/(java-archive|zip|gzip|arj|x-bzip|x-bzip2|x-compressed|x-gzip|x-stuffit)@i', $file['displaymime'])) {
        $file['mediatype'] = 'archive';
    } else {
        $file['mediatype'] = 'binary';
    }
    $file['realfile'] = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file['path'] . $file['name'] . '.' . $file['extension'];
    if ($full_perm || $serendipity['authorid'] == $file['authorid'] || $file['authorid'] == '0') {
        $file['is_editable'] = true;
    } else {
        $file['is_editable'] = false;
    }
    /* If it is an image, and the thumbnail exists */
    if ($file['is_image'] && file_exists($file['full_thumb'])) {
        $file['thumbWidth'] = $file['dim'][0];
        $file['thumbHeight'] = $file['dim'][1];
        $file['preview'] .= '<img src="' . $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $sThumbSource . '" border="0" title="' . $file['path'] . $file['name'] . '" alt="' . $file['realname'] . '" />';
        if ($url) {
            $file['preview_url'] = $url . '&amp;serendipity[image]=' . $file['id'];
            $file['preview'] = '<a href="' . $file['preview_url'] . '">' . $file['preview'] . '</a>';
        }
    } elseif ($file['is_image'] && $file['hotlink']) {
        $sizes = serendipity_calculate_aspect_size($file['dimensions_width'], $file['dimensions_height'], $serendipity['thumbSize'], $serendipity['thumbConstraint']);
        $file['thumbWidth'] = $sizes[0];
        $file['thumbHeight'] = $sizes[1];
        $file['preview'] .= '<img src="' . $file['path'] . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" border="0" title="' . $file['path'] . '" alt="' . $file['realname'] . '" />';
        if ($url) {
            $file['preview_url'] = $url . '&amp;serendipity[image]=' . $file['id'];
            $file['preview'] = '<a href="' . $file['preview_url'] . '">' . $file['preview'] . '</a>';
        }
        /* If it's not an image, or the thumbnail does not exist */
    } else {
        $mimeicon = serendipity_getTemplateFile('admin/img/mime_' . preg_replace('@[^a-z0-9\\-\\_]@i', '-', $file['mime']) . '.png');
        if (!$mimeicon) {
            $mimeicon = serendipity_getTemplateFile('admin/img/mime_unknown.png');
        }
        $file['preview'] .= '<img src="' . $mimeicon . '" title="' . $file['path'] . $file['name'] . ' (' . $file['mime'] . ')" alt="' . $file['mime'] . '" /><br /><span style="font-weight: bold; font-size: 8pt">- ' . ($file['hotlink'] ? MEDIA_HOTLINKED : $file['mime']) . ' -</span>';
        if ($url) {
            $file['preview_url'] = $url . '&amp;serendipity[image]=' . $file['id'];
            $file['preview'] .= '<br /><a href="' . $file['preview_url'] . '">' . $file['name'] . '.' . $file['extension'] . '</a>';
        }
    }
    $file['popupWidth'] = $file['is_image'] ? $file['dimensions_width'] + 20 : 600;
    $file['popupHeight'] = $file['is_image'] ? $file['dimensions_height'] + 20 : 500;
    if ($file['hotlink']) {
        $file['nice_hotlink'] = wordwrap($file['path'], 45, '<br />', 1);
    }
    $file['nice_size'] = number_format(round($file['size'] / 1024, 2), NUMBER_FORMAT_DECIMALS, NUMBER_FORMAT_DECPOINT, NUMBER_FORMAT_THOUSANDS);
    return true;
}
 function resizeThumb($sizes, $target)
 {
     global $serendipity;
     // Thumbsize: 75
     // A: 100x300
     // B: 300x100
     // s9y A: 25x75
     // s9y B: 75x25
     // Max-Height: 0
     // Max-Width : 75
     // s9y A:
     // s9y B:
     // Max-Height: 0
     // Max-Height: 75
     // s9y A: 25x75
     // s9y B: 225x75
     $fdim = @serendipity_getimagesize($target, '', '');
     if (!isset($serendipity['thumbConstraint'])) {
         // Original code, for older versions of s9y
         $s9ysizes = serendipity_calculate_aspect_size($fdim[0], $fdim[1], $serendipity['thumbSize']);
         if ($fdim[0] >= $fdim[1]) {
             $orientation = 'Landscape';
         } else {
             $orientation = 'Portrait';
         }
         if ($sizes['width'] == 0) {
             if ($orientation == 'Landscape') {
                 $_newsizes = serendipity_calculate_aspect_size($fdim[0], $fdim[1], null, $sizes['height']);
             } else {
                 $_newsizes = serendipity_calculate_aspect_size($fdim[0], $fdim[1], $sizes['height'], null);
             }
             $newsizes = array('width' => $_newsizes[0], 'height' => $_newsizes[1]);
         } elseif ($sizes['height'] == 0) {
             if ($orientation == 'Landscape') {
                 $_newsizes = serendipity_calculate_aspect_size($fdim[0], $fdim[1], $sizes['width'], null);
             } else {
                 $_newsizes = serendipity_calculate_aspect_size($fdim[0], $fdim[1], null, $sizes['width']);
             }
             $newsizes = array('width' => $_newsizes[0], 'height' => $_newsizes[1]);
         } else {
             $newsizes = array(0 => $sizes['width'], 1 => $sizes['height']);
         }
     } else {
         // Newer s9y version that understands how to constrain images properly
         $s9ysizes = serendipity_calculate_aspect_size($fdim[0], $fdim[1], $serendipity['thumbSize'], $serendipity['thumbConstraint']);
         $orientation = 'size';
         if ($sizes['width'] == 0) {
             $_newsizes = serendipity_calculate_aspect_size($fdim[0], $fdim[1], $sizes['height'], 'height');
         } elseif ($sizes['height'] == 0) {
             $_newsizes = serendipity_calculate_aspect_size($fdim[0], $fdim[1], $sizes['width'], 'width');
         } else {
             $_newsizes = array(0 => $sizes['width'], 1 => $sizes['height']);
         }
         $newsizes = array('width' => $_newsizes[0], 'height' => $_newsizes[1]);
     }
     echo '<span class="msg_notice"><span class="icon-attention-circled"></span> Resizing thumb of ' . $orientation . ' ' . $fdim[0] . 'x' . $fdim[1] . ' to ' . $_newsizes[0] . 'x' . $_newsizes[1] . ' instead of ' . $s9ysizes[0] . 'x' . $s9ysizes[1] . "...</span>\n";
     $dirname = dirname($target) . '/';
     $dirname = str_replace($serendipity['serendipityPath'] . $serendipity['uploadPath'], '', $dirname);
     $serendipity['imagemagick_nobang'] = true;
     serendipity_makeThumbnail(basename($target), $dirname, $newsizes, $serendipity['thumbSuffix']);
     $serendipity['imagemagick_nobang'] = false;
 }