예제 #1
0
 static function CreateThumbnail($src_img, $max_size)
 {
     $ext = trim(strtolower(strrchr($src_img, '.')), '.');
     $extras_dir = WPFB_PLUGIN_ROOT . 'extras/';
     $tmp_img = $src_img . '_thumb.jpg';
     $tmp_del = true;
     switch ($ext) {
         case 'bmp':
             if (@file_exists($extras_dir . 'phpthumb.functions.php') && @file_exists($extras_dir . 'phpthumb.bmp.php')) {
                 @(include_once $extras_dir . 'phpthumb.functions.php');
                 @(include_once $extras_dir . 'phpthumb.bmp.php');
                 if (class_exists('phpthumb_functions') && class_exists('phpthumb_bmp')) {
                     $phpthumb_bmp = new phpthumb_bmp();
                     $im = $phpthumb_bmp->phpthumb_bmpfile2gd($src_img);
                     if ($im) {
                         @imagejpeg($im, $tmp_img, 100);
                     } else {
                         return false;
                     }
                 }
             }
             break;
         default:
             $tmp_img = $src_img;
             $tmp_del = false;
             break;
     }
     $tmp_size = array();
     if (!@file_exists($tmp_img) || @filesize($tmp_img) == 0 || !WPFB_FileUtils::IsValidImage($tmp_img, $tmp_size)) {
         if ($tmp_del && is_file($tmp_img)) {
             @unlink($tmp_img);
         }
         return false;
     }
     if (!function_exists('image_make_intermediate_size')) {
         require_once ABSPATH . 'wp-includes/media.php';
         if (!function_exists('image_make_intermediate_size')) {
             if ($tmp_del && is_file($tmp_img)) {
                 @unlink($tmp_img);
             }
             wp_die('Function image_make_intermediate_size does not exist!');
             return false;
         }
     }
     $dir = dirname($src_img) . '/';
     $thumb = @image_make_intermediate_size($tmp_img, $max_size, $max_size);
     if ((!$thumb || is_wp_error($thumb)) && !empty($tmp_size) && max($tmp_size) <= $max_size) {
         // error occurs when image is smaller than thumb_size. in this case, just copy original
         $name = wp_basename($src_img, ".{$ext}");
         $new_thumb = "{$name}-{$tmp_size[0]}x{$tmp_size[1]}" . strtolower(strrchr($tmp_img, '.'));
         if ($tmp_del) {
             rename($tmp_img, $dir . $new_thumb);
         } else {
             copy($tmp_img, $dir . $new_thumb);
         }
         $thumb = array('file' => $new_thumb);
     }
     if ($tmp_del && is_file($tmp_img)) {
         unlink($tmp_img);
     }
     if (!$thumb) {
         return false;
     }
     rename($dir . $thumb['file'], $fn = $dir . str_ireplace(array('.pdf_thumb', '.tiff_thumb', '.tif_thumb', '.bmp_thumb'), '', $thumb['file']));
     return $fn;
 }
예제 #2
0
 static function CreateThumbnail($src_img, $max_size)
 {
     $ext = trim(strtolower(strrchr($src_img, '.')), '.');
     $extras_dir = WPFB_PLUGIN_ROOT . 'extras/';
     $tmp_img = $src_img . '_thumb.jpg';
     $tmp_del = true;
     switch ($ext) {
         case 'bmp':
             if (@file_exists($extras_dir . 'phpthumb.functions.php') && @file_exists($extras_dir . 'phpthumb.bmp.php')) {
                 @(include_once $extras_dir . 'phpthumb.functions.php');
                 @(include_once $extras_dir . 'phpthumb.bmp.php');
                 if (class_exists('phpthumb_functions') && class_exists('phpthumb_bmp')) {
                     $phpthumb_bmp = new phpthumb_bmp();
                     $im = $phpthumb_bmp->phpthumb_bmpfile2gd($src_img);
                     if ($im) {
                         @imagejpeg($im, $tmp_img, 100);
                     } else {
                         return false;
                     }
                 }
             }
             break;
         default:
             $tmp_img = $src_img;
             $tmp_del = false;
             break;
     }
     $tmp_size = array();
     if (!@file_exists($tmp_img) || @filesize($tmp_img) == 0 || !WPFB_FileUtils::IsValidImage($tmp_img, $tmp_size)) {
         return $tmp_del && is_file($tmp_img) && @unlink($tmp_img) && false;
     }
     if (!function_exists('wp_get_image_editor') && !(include_once ABSPATH . 'wp-includes/media.php') && !function_exists('wp_get_image_editor')) {
         return $tmp_del && is_file($tmp_img) && @unlink($tmp_img) && false;
     }
     // load image
     $editor = wp_get_image_editor($tmp_img);
     if (is_wp_error($editor)) {
         return $tmp_del && is_file($tmp_img) && @unlink($tmp_img) && false;
     }
     // "trim" image whitespaces
     $boundary = self::GetImageBoundary($tmp_img);
     if (array_sum($boundary) > 0 && is_wp_error($editor->crop($boundary[0], $boundary[1], $boundary[2], $boundary[3]))) {
         return $tmp_del && is_file($tmp_img) && @unlink($tmp_img) && false;
     }
     // resize to max thumb size
     if (is_wp_error($editor->resize($max_size, $max_size))) {
         return $tmp_del && is_file($tmp_img) && @unlink($tmp_img) && false;
     }
     // save
     $thumb = $editor->save();
     $dir = dirname($src_img) . '/';
     // error occurs when image is smaller than thumb_size. in this case, just copy original
     if (is_wp_error($thumb) && !empty($tmp_size) && max($tmp_size) <= $max_size) {
         $name = wp_basename($src_img, ".{$ext}");
         $new_thumb = "{$name}-{$tmp_size[0]}x{$tmp_size[1]}" . strtolower(strrchr($tmp_img, '.'));
         if ($tmp_del) {
             rename($tmp_img, $dir . $new_thumb);
         } else {
             copy($tmp_img, $dir . $new_thumb);
         }
         $thumb = array('file' => $new_thumb);
     }
     $tmp_del && is_file($tmp_img) && unlink($tmp_img);
     if (!$thumb) {
         return false;
     }
     $fn = $dir . str_ireplace(array('.pdf_thumb', '.jpg_thumb', '.tiff_thumb', '.tif_thumb', '.bmp_thumb'), '', $thumb['file']);
     // make sure we have a thumb file name like `._[KK..K].thumb.(jpg|png)$`
     $thumb_suffix = '.thumb';
     $lts = strlen($thumb_suffix);
     $p = strrpos($fn, '.');
     if ($p <= $lts || strcmp($thumb_suffix, substr($fn, $p - $lts, $lts)) != 0) {
         // add token to make thumbnail url non-guessable
         $token = '._' . wp_generate_password(12, false, false);
         $fn = substr($fn, 0, $p) . $token . $thumb_suffix . substr($fn, $p);
     }
     rename($dir . $thumb['file'], $fn);
     return $fn;
 }