Beispiel #1
0
 public static function resize($strFileName = "", $intWidth = 100, $intHeight = 75, $intTemplate = RESIZE_EXACT, $intQuality = 75, $blnOverwrite = FALSE, $strName = "", $blnToScreen = FALSE, $blnGrayscale = FALSE)
 {
     $strReturn = $strName;
     if (empty($strReturn)) {
         if (!$blnOverwrite) {
             //*** Generate a random name.
             $strExtension = self::getExtension(basename($strFileName));
             $strReturn = dirname($strFileName) . "/{$intWidth}x{$intHeight}_" . basename($strFileName, $strExtension) . "_" . strtotime("now") . "{$strExtension}";
         } else {
             $strReturn = $strFileName;
         }
     }
     $objEditor = new ImageEditor(basename($strFileName), dirname($strFileName) . "/");
     switch ($intTemplate) {
         case RESIZE_CROP:
             //*** Don't blow the image up if it is smaller then the destination.
             if ($intWidth > $objEditor->getWidth() && $intHeight > $objEditor->getHeight()) {
                 //*** Skip the resize.
             } else {
                 $destX = ($objEditor->getWidth() - $intWidth) / 2;
                 $destY = ($objEditor->getHeight() - $intHeight) / 2;
                 $objEditor->crop(round($destX), round($destY), $intWidth, $intHeight);
             }
             break;
         case RESIZE_FIT_CROP:
             //*** Resize the image to fit into the boundary box.
             //*** Portrait source.
             $destWidth = $intWidth;
             $destHeight = $destWidth / $objEditor->getWidth() * $objEditor->getHeight();
             if ($destHeight < $intHeight) {
                 //*** Landscape source.
                 $destHeight = $intHeight;
                 $destWidth = $destHeight / $objEditor->getHeight() * $objEditor->getWidth();
             }
             //*** Don't blow the image up if it is smaller then the destination.
             if (round($destWidth) > $objEditor->getWidth() && round($destHeight) > $objEditor->getHeight()) {
                 //*** Skipt the resize.
                 $destX = ($objEditor->getWidth() - $intWidth) / 2;
                 $destY = ($objEditor->getHeight() - $intHeight) / 2;
                 $objEditor->crop(round($destX), round($destY), $intWidth, $intHeight);
             } else {
                 $objEditor->resize(round($destWidth), round($destHeight));
                 $destX = ($objEditor->getWidth() - $intWidth) / 2;
                 $destY = ($objEditor->getHeight() - $intHeight) / 2;
                 $objEditor->crop(round($destX), round($destY), $intWidth, $intHeight);
             }
             break;
         case RESIZE_DISTORT:
             //*** Don't blow the image up if it is smaller then the destination.
             if ($intWidth > $objEditor->getWidth() && $intHeight > $objEditor->getHeight()) {
                 //*** Skipt the resize.
             } else {
                 $objEditor->resize($intWidth, $intHeight);
             }
             break;
         case RESIZE_EXACT:
             $destWidth = $intWidth;
             $destHeight = $destWidth / $objEditor->getWidth() * $objEditor->getHeight();
             if ($destHeight > $intHeight) {
                 //*** Landscape source.
                 $destHeight = $intHeight;
                 $destWidth = $destHeight / $objEditor->getHeight() * $objEditor->getWidth();
             }
             //*** Don't blow the image up if it is smaller then the destination.
             if (round($destWidth) > $objEditor->getWidth() && round($destHeight) > $objEditor->getHeight()) {
                 //*** Skipt the resize.
             } else {
                 $objEditor->resize(round($destWidth), round($destHeight));
             }
             break;
     }
     if ($blnGrayscale) {
         $objEditor->grayscale();
     }
     if (!empty($strReturn)) {
         if ($blnToScreen) {
             //*** Return image.
             $objEditor->outputImage($intQuality);
         } else {
             //*** Write file to disk.
             $objEditor->outputFile(basename($strReturn), dirname($strReturn) . "/", $intQuality);
         }
     }
     return $strReturn;
 }
Beispiel #2
0
function tb_post_thumb($generate = false, $alt_text = '', $resize_width = 0, $resize_height = 0, $crop_x = 0, $crop_y = 0, $entry = null)
{
    global $post;
    if ($entry) {
        $post = $entry;
    }
    $settings = get_option('post_thumbnail_settings');
    if ($resize_width) {
        $settings['resize_width'] = $resize_width;
    }
    if ($resize_height) {
        $settings['resize_height'] = $resize_height;
    }
    // find an image from your domain
    if (preg_match('/<img (.*?)src="https?:\\/\\/(www\\.)?' . str_replace('/', '\\/', $settings['domain_name']) . '\\/(.*?)"/i', $post->post_content, $matches)) {
        // put matches into recognizable vars
        // fix later, assumes document root will match url structure
        $the_image = $settings['base_path'] . '/' . $matches[3];
        // check if image exists on server
        // if doesn't exist, can't do anything so return default image
        if (!file_exists($the_image)) {
            return tb_post_thumb_gen_image(str_replace($settings['full_domain_name'], $settings['base_path'], $settings['default_image']), $settings['default_image'], $alt_text, $generate);
        }
        $dest_path = pathinfo($the_image);
        // dir to save thumbnail to
        $save_dir = $dest_path['dirname'] . "/{$settings['folder_name']}";
        // name to save to
        $filename_suffix = $resize_width . 'x' . $resize_height . '-' . $crop_x . '-' . $crop_y;
        $filename = substr($dest_path['basename'], 0, strrpos($dest_path['basename'], "."));
        if ($settings['append'] == 'true') {
            $rename_to = $filename . $settings['append_text'] . '-' . $filename_suffix . '.' . $dest_path['extension'];
        } else {
            $rename_to = $settings['append_text'] . $filename . '-' . $filename_suffix . '.' . $dest_path['extension'];
        }
        // check if file already exists
        // return location if does
        if (file_exists($save_dir . '/' . $rename_to)) {
            $imagelocation = str_replace($settings['base_path'], $settings['full_domain_name'], $save_dir . '/' . $rename_to);
            return tb_post_thumb_gen_image($save_dir . '/' . $rename_to, $imagelocation, $alt_text, $generate);
        }
        // sticky bit?
        if (!is_dir($save_dir)) {
            mkdir($save_dir, 0777);
        }
        // manipulate thumbnails
        $thumb = new ImageEditor($dest_path['basename'], $dest_path['dirname'] . '/');
        $thumb->resize($settings['resize_width'], $settings['resize_height'], $settings['keep_ratio']);
        if ($settings['crop_exact'] == 'true' || $crop_x != 0 && $crop_y != 0) {
            if ($crop_x != 0 && $crop_y != 0) {
                $settings['resize_width'] = $crop_x;
                $settings['resize_height'] = $crop_y;
            }
            if ($thumb->x > $settings['resize_width'] || $thumb->y > $settings['resize_height']) {
                $thumb->crop((int) (($thumb->x - $settings['resize_width']) / 2), 0, $settings['resize_width'], $settings['resize_height']);
            }
        }
        $thumb->outputFile($save_dir . "/" . $rename_to, "");
        chmod($save_dir . "/" . $rename_to, 0666);
        $imagelocation = str_replace($settings['base_path'], $settings['full_domain_name'], $save_dir . '/' . $rename_to);
        return tb_post_thumb_gen_image($save_dir . '/' . $rename_to, $imagelocation, $alt_text, $generate);
    } else {
        if (!empty($settings['video_regex']) && tb_post_thumb_check_video($settings['video_regex'])) {
            $settings['default_image'] = $settings['video_default'];
        }
        return tb_post_thumb_gen_image(str_replace($settings['full_domain_name'], $settings['base_path'], $settings['default_image']), $settings['default_image'], $alt_text, $generate);
    }
}