コード例 #1
0
/**
 * Thumbnail generation
 */
function generate_thumbnail(&$file, &$width, &$height, $max_width, $max_height, $cache_tag)
{
    global $config;
    $thumb_cache_tag = $cache_tag . '_' . $max_width . 'x' . $max_height;
    // the names of the existing or to-be created thumbnail- still missing dimensions
    $thumb_name = './cache/' . CACHE_THUMBS . '/' . ($config['hierarchical'] ? substr($cache_tag, 0, 1) . '/' : '') . $cache_tag;
    // did we already create this thumbnail?
    if (THUMB_CACHE_TARGET && html_image_get_cache($thumb_cache_tag, $width, $height)) {
        // get updated filename
        $file = $thumb_name . '_' . $width . 'x' . $height . '.jpg';
        return;
    }
    // thumbnail not created yet
    $scale = min($max_width / $width, $max_height / $height);
    $width = round($width * $scale);
    $height = round($height * $scale);
    // really need to scale?
    $thumb_must_scale = $config['thumbnail_level'] == TUMB_SCALE || $config['thumbnail_level'] == TUMB_REDUCE_ONLY && $scale < 1 || $config['thumbnail_level'] > 1 && $config['thumbnail_level'] < @filesize($file);
    #dump("scaling required: ".(int)$thumb_must_scale." filesize: ".@filesize($file));
    // perform actual scaling
    if ($thumb_must_scale) {
        $phpThumb = new phpThumb();
        // use of truepath was added for php 5.4 apparently- otherwise thumbnail creation would inadvertantly fail
        $phpThumb->sourceFilename = truepath($file);
        $phpThumb->w = $max_width;
        $phpThumb->h = $max_height;
        if ($config['thumbnail_quality']) {
            $phpThumb->q = $config['thumbnail_quality'];
        }
        // set cache filename
        $thumb_name .= '_' . $width . 'x' . $height . '.jpg';
        #dump("thumbname:$thumb_name");
        // check to see if file already exists in cache, and output it with no processing if it does
        if (is_writable(dirname($thumb_name)) || is_writable($thumb_name)) {
            if (@filesize($thumb_name) || $phpThumb->GenerateThumbnail() && $phpThumb->RenderOutput() && file_put_contents($thumb_name, $phpThumb->outputImageData)) {
                $file = $thumb_name;
                if (THUMB_CACHE_TARGET) {
                    html_image_put_cache($thumb_cache_tag, $width . 'x' . $height);
                }
            }
            /*
            			# emergency debugging
            			else
            			{
            				dlog("Fail: $thumb_name");
            				dlog("fs: ".@filesize($thumb_name));
            				dlog($phpThumb->GenerateThumbnail());
            				dlog($phpThumb->RenderOutput());
            				dlog($phpThumb->debugmessages);
            			}
            */
        }
        // free memory
        $phpThumb = null;
    }
}
コード例 #2
0
 // or $phpThumb->setSourceImageResource($gd_image_resource);
 // set parameters (see "URL Parameters" in phpthumb.readme.txt)
 $phpThumb->setParameter('w', $thumbnail_width);
 //$phpThumb->setParameter('h', 100);
 //$phpThumb->setParameter('fltr', 'gam|1.2');
 // set options (see phpThumb.config.php)
 // here you must preface each option with "config_"
 $phpThumb->setParameter('config_output_format', 'jpeg');
 $phpThumb->setParameter('config_imagemagick_path', '/usr/local/bin/convert');
 //$phpThumb->setParameter('config_allow_src_above_docroot', true); // needed if you're working outside DOCUMENT_ROOT, in a temp dir for example
 // generate & output thumbnail
 $output_filename = './thumbnails/' . basename($_FILES['userfile']['name']) . '_' . $thumbnail_width . '.' . $phpThumb->config_output_format;
 if ($phpThumb->GenerateThumbnail()) {
     // this line is VERY important, do not remove it!
     if ($output_filename) {
         if ($capture_raw_data && $phpThumb->RenderOutput()) {
             // RenderOutput renders the thumbnail data to $phpThumb->outputImageData, not to a file or the browser
             mysql_query("INSERT INTO `table` (`thumbnail`) VALUES ('" . mysql_escape_string($phpThumb->outputImageData) . "') WHERE (`id` = '" . $id . "'");
         } elseif ($phpThumb->RenderToFile($output_filename)) {
             // do something on success
             echo 'Successfully rendered:<br><img src="' . $output_filename . '">';
         } else {
             // do something with debug/error messages
             echo 'Failed (size=' . $thumbnail_width . '):<pre>' . implode("\n\n", $phpThumb->debugmessages) . '</pre>';
         }
     } else {
         $phpThumb->OutputThumbnail();
     }
 } else {
     // do something with debug/error messages
     echo 'Failed (size=' . $thumbnail_width . ').<br>';
コード例 #3
0
ファイル: functions.php プロジェクト: kinj1987/evo-league
function generateThumb($imgDir, $picName, $extension, $thumbtext)
{
    require_once '../thumb/phpthumb.class.php';
    require '../variables.php';
    $thumbNames = array();
    $imgUrl = $imgDir . $picName . "." . $extension;
    $thumbDir = "thumbnails/";
    // echo "<p>$imgUrl</p>";
    // create 3 sizes of thumbnail
    $thumbnail_widths = getThumbnailWidths();
    foreach ($thumbnail_widths as $thumbnail_width) {
        // Note: If you want to loop through and create multiple
        //   thumbnails from different image sources, you should
        //   create and dispose an instance of phpThumb() each time
        //   through the loop and not reuse the object.
        $phpThumb = new phpThumb();
        // set data
        $phpThumb->setSourceFilename($imgUrl);
        // or $phpThumb->setSourceData($binary_image_data);
        // or $phpThumb->setSourceImageResource($gd_image_resource);
        // $gd_img = imagecreatefrom($imgPath.$picturename);
        // $phpThumb->setSourceImageResource($gd_img);
        // set parameters (see "URL Parameters" in phpthumb.readme.txt)
        $phpThumb->setParameter('w', $thumbnail_width);
        //$phpThumb->setParameter('h', 100);
        $fParam = 'wmt|Click to open (^Xx^Y)|1|B|FFFFFF||100|0||000000|100|x';
        if (!empty($thumbtext)) {
            $fParamTop = 'wmt| ' . $thumbtext . ' |2|T|000000||100|0||CCCCCC|100|x';
            $phpThumb->setParameter('fltr', $fParamTop);
        }
        $phpThumb->setParameter('fltr', $fParam);
        $phpThumb->setParameter('fltr', 'bord|1');
        // set options (see phpThumb.config.php)
        // here you must preface each option with "config_"
        $phpThumb->setParameter('config_output_format', 'png');
        // $phpThumb->setParameter('config_imagemagick_path', '/usr/local/bin/convert');
        // $phpThumb->setParameter('config_imagemagick_path', '.');
        //$phpThumb->setParameter('config_allow_src_above_docroot', true); // needed if you're working outside DOCUMENT_ROOT, in a temp dir for example
        // generate & output thumbnail
        $output_filename = $imgDir . $thumbDir . $picName . '_' . $thumbnail_width . '.' . $phpThumb->config_output_format;
        if ($phpThumb->GenerateThumbnail()) {
            // this line is VERY important, do not remove it!
            if ($output_filename) {
                if ($capture_raw_data && $phpThumb->RenderOutput()) {
                    // RenderOutput renders the thumbnail data to $phpThumb->outputImageData, not to a file or the browser
                    // mysql_query("INSERT INTO `table` (`thumbnail`) VALUES ('".mysql_escape_string($phpThumb->outputImageData)."') WHERE (`id` = '".$id."'");
                } elseif ($phpThumb->RenderToFile($output_filename)) {
                    $thumbNames[] = array($thumbnail_width, $output_filename);
                } else {
                    // do something with debug/error messages
                    // echo 'Failed (size=' . $thumbnail_width . '):<pre>' . implode("\n\n", $phpThumb->debugmessages) . '</pre>';
                }
            } else {
                $phpThumb->OutputThumbnail();
            }
        } else {
            // do something with debug/error messages
            //echo '<p>Thumbnail generation failed (size='.$thumbnail_width.').</p>';
            //echo '<div style="background-color:#FFEEDD; font-weight: bold; padding: 10px;">'.$phpThumb->fatalerror.'</div>';
            //echo '<form><textarea rows="10" cols="60" wrap="off">'.htmlentities(implode("\n* ", $phpThumb->debugmessages)).'</textarea></form><hr>';
        }
        // remember to unset the object each time through the loop
        unset($phpThumb);
    }
    return $thumbNames;
}