예제 #1
0
function generate_thumb($path, $prefix, $type = THUMB_SMALL)
{
    global $config;
    global $thumbnail_config;
    // for relative paths assume that they are relative to images directory,
    // otherwise just use the given pat
    if (file_exists($path)) {
        $source_file_name = $path;
    } else {
        $source_file_name = $config['basedir'] . 'images/' . SmartStripSlashes($path);
    }
    // the file might have been deleted and since phpThumb dies in that case
    // try to do something sensible so that the rest of the images can still be seen
    // there is a problem in safe mode - if the script and picture file are owned by
    // different users, then the file can not be read.
    if (!is_readable($source_file_name)) {
        return false;
    }
    $imgdata = @getimagesize($source_file_name);
    if (!$imgdata) {
        // unknown image format, bail out
        return false;
    }
    // attributes of original image
    $orig_width = $imgdata[0];
    $orig_height = $imgdata[1];
    // XXX: food for thought - maybe we can return URL to some kind of error image
    // if this function fails?
    $base_filename = sanitize_filename(basename($path));
    $thumb_config = $thumbnail_config[$type];
    if (1 == $thumb_config['disabled']) {
        return $config['baseurl'] . '/images/' . $path;
    }
    $prefix = $thumb_config['filename_prefix'] . $prefix . "-";
    $thumbpath = $config['basedir'] . 'thumbs/' . $prefix . $base_filename;
    $thumburl = $config['gallery_url'] . 'thumbs/' . $prefix . $base_filename;
    // if thumbnail file already exists and is generated after data for a thumbnail type
    // has been changed, then we assume that the thumbnail is valid.
    $thumbnail_timestamp = @filemtime($thumbpath);
    if (file_exists($thumbpath) && $thumb_config['timestamp'] < $thumbnail_timestamp) {
        return $thumburl;
    }
    // if dimensions of source image are smaller than those of the requested
    // thumbnail, then use the original image as thumbnail
    if ($orig_width <= $thumb_config['size'] && $orig_height <= $thumb_config['size']) {
        copy($source_file_name, $thumbpath);
        return $thumburl;
    }
    // no existing thumbnail found or thumbnail config has changed,
    // generate new thumbnail file
    list($width, $height, $thumb_type, $attr) = @getimagesize($thumbpath);
    require_once $config['basedir'] . 'lib/phpthumb/phpthumb.class.php';
    $phpThumb = new phpThumb();
    // set data
    $phpThumb->setSourceFileName($source_file_name);
    if ($imgdata[0] > $imgdata[1]) {
        $phpThumb->w = $thumb_config['size'];
    } else {
        $phpThumb->h = $thumb_config['size'];
    }
    $phpThumb->q = $config['compression'];
    // set zoom crop flag to get image squared off
    if ($type == THUMB_SMALL && $config['square_thumbs']) {
        $phpThumb->zc = 1;
        $phpThumb->h = $thumb_config['size'];
        $phpThumb->w = $thumb_config['size'];
    }
    $phpThumb->config_use_exif_thumbnail_for_speed = false;
    // Set image height instead of width if not using square thumbs
    if ($type == THUMB_SMALL && !$config['square_thumbs']) {
        $phpThumb->h = $thumb_config['size'];
        $phpThumb->w = '';
    }
    if ($type == THUMB_NAV) {
        $phpThumb->zc = 1;
        $phpThumb->h = $thumb_config['size'];
        $phpThumb->w = $thumb_config['size'];
    }
    // set options (see phpThumb.config.php)
    // here you must preface each option with "config_"
    // Set error handling (optional)
    $phpThumb->config_error_die_on_error = false;
    // generate & output thumbnail
    if ($phpThumb->GenerateThumbnail()) {
        $phpThumb->RenderToFile($thumbpath);
    } else {
        // do something with debug/error messages
        die('Failed: ' . implode("\n", $phpThumb->debugmessages));
    }
    return $thumburl;
}
예제 #2
0
function generate_thumb($path, $prefix, $type = THUMB_SMALL)
{
    global $config, $thumbnail_config;
    $thumb_config = $thumbnail_config[$type];
    // For relative paths assume that they are relative to 'plog-content/images/' directory,
    // otherwise just use the given path
    if (file_exists($path)) {
        $source_file_name = $path;
        if ($type == THUMB_THEME) {
            $cache_path = 'themes/';
        } else {
            $cache_path = 'uploads/';
        }
    } else {
        $source_file_name = $config['basedir'] . 'plog-content/images/' . SmartStripSlashes($path);
        $cache_path = dirname(SmartStripSlashes($path)) . '/' . $thumb_config['type'] . '/';
    }
    // The file might have been deleted and since phpThumb dies in that case
    // try to do something sensible so that the rest of the images can still be seen
    // There is a problem in safe mode - if the script and picture file are owned by
    // different users, then the file cannot be read.
    if (!is_readable($source_file_name)) {
        return false;
    }
    $imgdata = @getimagesize($source_file_name);
    if (!$imgdata) {
        // Unknown image format, bail out
        // Do we want to have video support in the Plogger core?
        //return 'plog-graphics/thumb-video.gif';
        return false;
    }
    // Attributes of original image
    $orig_width = $imgdata[0];
    $orig_height = $imgdata[1];
    // XXX: food for thought - maybe we can return URL to some kind of error image
    // if this function fails?
    $base_filename = sanitize_filename(basename($path));
    if ($thumb_config['disabled']) {
        return $config['gallery_url'] . 'plog-content/images/' . $path;
    }
    $prefix = $prefix . '-';
    $thumbpath = $config['basedir'] . 'plog-content/thumbs/' . $cache_path . $prefix . $base_filename;
    $thumburl = $config['gallery_url'] . 'plog-content/thumbs/' . $cache_path . $prefix . $base_filename;
    // If thumbnail file already exists and is generated after data for a thumbnail type
    // has been changed, then we assume that the thumbnail is valid.
    if (file_exists($thumbpath)) {
        $thumbnail_timestamp = @filemtime($thumbpath);
        if ($thumb_config['timestamp'] < $thumbnail_timestamp) {
            return $thumburl;
        }
    }
    // Create the same directory structure as the image under plog-content/thumbs/
    include_once PLOGGER_DIR . 'plog-admin/plog-admin-functions.php';
    if (!makeDirs(dirname($thumbpath))) {
        return sprintf(plog_tr('Error creating path %s'), dirname($thumbpath));
    }
    // If dimensions of source image are smaller than those of the requested
    // thumbnail, then use the original image as thumbnail unless fullsize images are disabled
    if ($orig_width <= $thumb_config['size'] && $orig_height <= $thumb_config['size']) {
        // if fullsize image access is disabled, copy the file to the thumbs folder
        if ($config['allow_fullpic'] == 0) {
            copy($source_file_name, $thumbpath);
            return $thumburl;
            // otherwise return the original file path
        } else {
            return $config['gallery_url'] . 'plog-content/images/' . $path;
        }
    }
    // No existing thumbnail found or thumbnail config has changed,
    // generate new thumbnail file
    require_once PLOGGER_DIR . 'plog-includes/lib/phpthumb/phpthumb.class.php';
    $phpThumb = new phpThumb();
    // Set data
    $phpThumb->setSourceFileName($source_file_name);
    switch ($thumb_config['resize_option']) {
        // Resize to width
        case 0:
            $phpThumb->w = $thumb_config['size'];
            break;
            // Resize to height
        // Resize to height
        case 1:
            $phpThumb->h = $thumb_config['size'];
            break;
            // Use square thumbnails
        // Use square thumbnails
        case 3:
            $phpThumb->zc = 1;
            $phpThumb->h = $thumb_config['size'];
            $phpThumb->w = $thumb_config['size'];
            break;
            // Resize to longest side
        // Resize to longest side
        case 2:
        default:
            if ($imgdata[0] > $imgdata[1]) {
                $phpThumb->w = $thumb_config['size'];
            } else {
                $phpThumb->h = $thumb_config['size'];
            }
    }
    $phpThumb->q = $config['compression'];
    if ($type == THUMB_NAV) {
        $phpThumb->zc = 1;
        $phpThumb->h = $thumb_config['size'];
        $phpThumb->w = $thumb_config['size'];
    }
    if ($type == THUMB_THEME) {
        $phpThumb->w = $thumb_config['size'];
    }
    // Set options (see phpThumb.config.php)
    // here you must preface each option with "config_"
    // Disable ImageMagick - set to false for localhost testing
    // ImageMagick seems to cause some issues on localhost using FF or Chrome
    $phpThumb->config_prefer_imagemagick = false;
    // We want to use the original image for thumbnail creation, not the EXIF stored thumbnail
    $phpThumb->config_use_exif_thumbnail_for_speed = false;
    // Set error handling (optional)
    $phpThumb->config_error_die_on_error = false;
    // If safe_mode enabled, open the permissions first
    if (is_safe_mode()) {
        $thumb_path = dirname($thumbpath) . '/';
        chmod_ftp($thumb_path, 0777);
    }
    // Generate & output thumbnail
    if ($phpThumb->GenerateThumbnail()) {
        $phpThumb->RenderToFile($thumbpath);
    } else {
        // do something with debug/error messages
        die('Failed: ' . implode("\n", $phpThumb->debugmessages));
    }
    @chmod($thumbpath, PLOGGER_CHMOD_FILE);
    // If safe_mode enabled, close the permissions back down to the default
    if (is_safe_mode()) {
        chmod_ftp($thumb_path);
    }
    return $thumburl;
}