REQ:Help Modding WordPress post thumb code.

Status
Not open for further replies.

Lee.

Banned
Jan 20, 2007
212
2
0
USA
Hi guys. This makes thumbs for posts in WorPress, but I would like to be able to write to a specified directory instead of the same directory the image is in.
I thought I could change
Code:
$save_dir = $dest_path['dirname']."/{$settings['folder_name']}";
but it's not working out. Any help would be appreciated.
Code:
<?php
require_once('post-thumb-image-editor.php');

$data = array(    'domain_name' => '',
                'default_image' => '',
                'full_domain_name' => '',
                'base_path' => '',
                'folder_name' => '.pthumbs',
                'append' => 'false',
                'append_text' => '.',
                'resize_width' => '60',
                'resize_height' => '60',
                'keep_ratio' => 'true',
                'crop_exact' => 'true',
                'video_default' => '',
                'video_regex' => ''
                );
                
                
add_option('post_thumbnail_settings',$data,'Post Thumbnail Options');

function tb_post_thumb($generate=false, $alt_text='', $resize_width = 0, $resize_height = 0, $crop_x = 0, $crop_y = 0) {
    
    global $post;
    $settings = get_option('post_thumbnail_settings');
    
    // 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
        if ($settings['append'] == 'true') {
            $filename = substr($dest_path['basename'], 0, strrpos($dest_path['basename'], "."));
            $rename_to = $filename.$settings['append_text'].'.'.$dest_path['extension'];
        }
        else $rename_to = $settings['append_text'].$dest_path['basename'];

        // 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,02775);
        // 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, "");
        $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
                                        );
    }
}

function tb_post_thumb_gen_image($server_image,$site_image,$alt='',$fullhtml) {
    if ($fullhtml) {
        list($width, $height, $type, $attr) = getimagesize($server_image);
        return '<img src="'.$site_image.'" '.$attr.' alt="'.$alt.'" />';
    } else {
        return $site_image;
    }
}

function tb_post_thumb_check_video($regex) {
    global $post;
    if (preg_match('/'.$regex.'/i',$post->post_content,$matches)) return true;
    else return false;
}

function tb_post_thumb_options() {
    if (function_exists('add_options_page')) {
        add_options_page('Post Thumbnails', 'Post Thumbs', 8, basename(__FILE__), 'tb_post_thumb_options_subpanel');
    }
}

function tb_post_thumb_options_subpanel() {
      if (isset($_POST['info_update']) == 'Update') {
          if ($_POST['crop_exact'] == 'on') $_POST['crop_exact'] = 'true';
          else $_POST['crop_exact'] = 'false';
          if ($_POST['append'] == 'on') $_POST['append'] = 'true';
          else $_POST['append'] = 'false';
          if ($_POST['keep_ratio'] == 'on') $_POST['keep_ratio'] = 'true';
          else $_POST['keep_ratio'] = 'false';
          if (get_magic_quotes_gpc()) $_POST['video_regex'] = stripslashes($_POST['video_regex']);
          $new_options = array(    'domain_name' => $_POST['domain_name'],
                                'default_image' => $_POST['default_image'],
                                'full_domain_name' => $_POST['full_domain_name'],
                                'base_path' => $_POST['base_path'],
                                'folder_name' => $_POST['folder_name'],
                                'append' => $_POST['append'],
                                'append_text' => $_POST['append_text'],
                                'resize_width' => $_POST['width'],
                                'resize_height' => $_POST['height'],
                                'keep_ratio' => $_POST['keep_ratio'],
                                'crop_exact' => $_POST['crop_exact'],
                                'video_regex' => $_POST['video_regex'],
                                'video_default' => $_POST['video_default'],
                            );
        if (!is_numeric($new_options['resize_width'])) {
            $update_error = "Resize width must be a number";
            $new_options['resize_width'] = '60';
        } else if (!is_numeric($new_options['resize_height'])) {
            $update_error = "Resize height must be a number";
            $new_options['resize_height'] = '60';
        }
        update_option('post_thumbnail_settings',$new_options);
        ?>
 


Lee:

Could changing the line in your array help?

'folder_name' => '.pthumbs'
 
Changing that line to what? As it is now .pthumbs is onlt the name of the folder in the directory which MUST be the same as the images. I would like to be able to pull images from anywhere but the generated thumb can go into any folder I choose NOT the same folder where the images are found.

I think I'll try to change the expression to a catch-all and see it that works...
 
  • Like
Reactions: nachoninja
Status
Not open for further replies.