コード例 #1
0
function scrgen_generate_post_screenshot($post_id)
{
    // Workaround bug: http://premium.wpmudev.org/forums/topic/snapshot-faltal-error-on-get_home_path
    if (!function_exists('get_home_path')) {
        require_once dirname(__FILE__) . '/../../../wp-admin/includes/file.php';
    }
    $url = get_permalink($post_id);
    $url_segs = parse_url($url);
    $phantomjs = scrgen_phantomjs();
    if (empty($phantomjs)) {
        error_log('The phantomjs binary was not found. Make sure it is in your PHP\'s PATH or set the PHANTOMJS constant to its path.');
        return;
    }
    $relative_path = 'wp-content/screenshots/' . $post_id . '.jpg';
    $image_path = get_home_path() . $relative_path;
    $image_url = get_home_url() . '/' . $relative_path;
    $job_id = uniqid();
    $job_path = "/tmp/{$job_id}.js";
    $tmp_path = "/tmp/{$job_id}.jpg";
    $width = scrgen_setting('width');
    $height = scrgen_setting('height');
    $enable_cropping = scrgen_setting('enable_cropping');
    $crop_left = scrgen_setting('crop_left');
    $crop_top = scrgen_setting('crop_top');
    $crop_width = scrgen_setting('crop_width');
    $crop_height = scrgen_setting('crop_height');
    $url = strip_tags($url);
    $url = str_replace(';', '', $url);
    $url = str_replace('"', '', $url);
    $url = str_replace('\'', '/', $url);
    $url = str_replace('<?', '', $url);
    $url = str_replace('<?', '', $url);
    $url = str_replace('\\077', ' ', $url);
    $url = escapeshellcmd($url);
    $src = "\n\n  var page = require('webpage').create();\n\n  page.viewportSize = { width: {$width}, height: {$height} };\n\n  ";
    if ($enable_cropping) {
        $src .= "page.clipRect = { top: {$crop_top}, left: {$crop_left}, width: {$crop_width}, height: {$crop_height} };";
    }
    $src .= "\n\n  page.open('{$url}', function () {\n      page.render('{$tmp_path}');\n      phantom.exit();\n  });\n\n\n  ";
    file_put_contents($job_path, $src);
    $exec = $phantomjs . ' ' . $job_path;
    $escaped_command = escapeshellcmd($exec);
    exec($escaped_command);
    unlink($job_path);
    if (is_file($tmp_path)) {
        rename($tmp_path, $image_path);
        do_action('scrgen_post_screenshot_generated', $post_id, $image_url);
    }
}
コード例 #2
0
function scrgen_options_page()
{
    ?>
  <form action='options.php' method='post'>
   
    <h2>Screenshot Generator</h2>

    <p>
      Screenshot Generator takes automatic screenshots of posts when they are updated.<br />
      These are saved to <code>wp-content/screenshots</code>.
    </p>

    <?php 
    if (scrgen_phantomjs() == false) {
        ?>
    <div style="background: #faa; color: #700; padding: 10px 15px; border: 1px solid #c00; margin-right: 20px;">
      The PhantomJS binary (<code>phantomjs</code>) could not be found. Screenshot Generator uses PhantomJS to generate screenshots.<br />
      Please see the <a href="https://github.com/lassebunk/screenshot-generator#installation" target="_blank">installation instructions</a> for information on how to fix this.
    </div>
    <?php 
    }
    ?>
   
    <?php 
    if (isset($_REQUEST['msg']) && $_REQUEST['msg'] == 'regenerating') {
        ?>
    <div style="background: #ada; color: #050; padding: 10px 15px; border: 1px solid #090; margin-right: 20px;">
      Screenshots are now being regenerated in the background, and will be available when done.<br >
      You are welcome to navigate away from this page.
    </div>
    <?php 
    }
    ?>

    <?php 
    settings_fields('scrgen-settings');
    do_settings_sections('scrgen-settings');
    submit_button();
    ?>
   
  </form>
  <?php 
}