Cache Images

  • Avatar de Inconnu

    Bonjour,

    J’ai un blog sous blogspot que je souhaite basculer sous wordpress. J’ai trouvé un plugin qui copie les images déposées sur blogspot vers mon site.
    Cela marche bien sauf qu’il ne trouve que les images miniatures et pas les grands formats…

    Exemple, il m’uploade l’image :
    http://1.bp.blogspot.com/_FKD9m7dt0hI/SbWk4hOLAiI/AAAAAAAAAU4/SqrKIkuJU6w/s320/inde+006.jpg
    mais pas celle-ci
    http://1.bp.blogspot.com/_FKD9m7dt0hI/SbWk4hOLAiI/AAAAAAAAAU4/SqrKIkuJU6w/s1600-h/inde+006.jpg

    La seule différence c’est le nom du répertoire s320 (mini) et s1600-h (maxi).
    Je pensais donc faire str_replace(« s320″, »s1600-h »,$lefichier) dans le script mais le blème c’est que je ne sais pas où faire ce changement…

    Voici le script :

    <?php
    /*
    Plugin Name: Cache Images
    Plugin URI: http://wordpress.org/extend/plugins/cache-images/
    Description: Goes through your posts and gives you the option to cache all hotlinked images from a domain locally in your upload folder
    Version: 1.3
    Author: Matt Mullenweg
    Author URI: http://ma.tt/
    WordPress Version Required: 2.2
    */

    function mkdirr($pathname, $mode = 0777) { // Recursive, Hat tip: PHP.net
    //Modif Fred
    // J’ai réussi à modifier le chemin dans lequel copier le fichier mais je n’arrive pas à modifier le chemin du fichier à télécharger…
    $pathname=str_replace(« s320″, »s1600-h »,$pathname);
    //fin modif

    // Check if directory already exists
    if ( is_dir($pathname) || empty($pathname) )
    return true;

    // Ensure a file does not already exist with the same name
    if ( is_file($pathname) )
    return false;

    // Crawl up the directory tree
    $next_pathname = substr( $pathname, 0, strrpos($pathname, DIRECTORY_SEPARATOR) );
    if ( mkdirr($next_pathname, $mode) ) {
    if (!file_exists($pathname))
    return mkdir($pathname, $mode);
    }

    return false;
    }

    function mm_ci_add_pages() {

    add_management_page(‘Cache Remote Images’, ‘Remote Images’, 8, __FILE__,
    ‘mm_ci_manage_page’);
    }

    function mm_ci_manage_page() {
    global $wpdb;
    ?>
    <div class= »wrap »>
    <h2>Remote Image Caching</h2>
    <?php if ( !isset($_POST[‘step’]) ) : ?>
    <p>Here’s how this works:</p>

    1. Click the button below and we’ll scan all of your posts for remote images
    2. Then you’ll be presented with a checklist of domains, check the domains you want to grab cache from
    3. The images will be copied to your upload directory (this must be writable) and the links in your posts will be updated to the new location.

    <form action= » » method= »post »>
    <p class= »submit »>
    <input name= »step » type= »hidden » id= »step » value= »2″>
    <input type= »submit » name= »Submit » value= »Get Started » » />
    </p>
    </form>
    <?php endif; ?>

    <?php if (‘2’ == $_POST[‘step’]) : ?>
    <?php
    $posts = $wpdb->get_results(« SELECT post_content FROM $wpdb->posts WHERE post_content LIKE (‘%<img%’) »);

    if ( !$posts )
    die(‘No posts with images were found.’);

    foreach ($posts as $post) :
    preg_match_all(‘|<img.*?src=[' »](.*?)[' »].*?>|i’, $post->post_content, $matches);

    foreach ($matches[1] as $url) :
    $url = parse_url($url);
    $url[‘host’] = str_replace(‘www.’,  », $url[‘host’]);
    $domains[$url[‘host’]]++;
    endforeach;

    endforeach;
    ?>
    <p>We found some goodies. Check the domains that you want to grab images from:</p>
    <form action= » » method= »post »>

      <?php foreach ($domains as $domain => $num) : ?>

    • <label><input type= »checkbox » name= »domains[] » value= »<?php echo $domain; ?> » /> <?php echo $domain; ?> (<?php echo $num; ?> images found)</label>
    • <?php endforeach; ?>

    <p class= »submit »>
    <input name= »step » type= »hidden » id= »step » value= »3″>
    <input type= »submit » name= »Submit » value= »Cache These Images » » />
    </p>
    </form>
    <?php endif; ?>

    <?php if (‘3’ == $_POST[‘step’]) : ?>
    <?php
    if ( !isset($_POST[‘domains’]) )
    die(« You didn’t check any domains, did you change your mind? »);
    if ( !is_writable( ABSPATH . get_option(‘upload_path’) ) )
    die(‘Your upload folder is not writable’);

    foreach ( $_POST[‘domains’] as $domain ) :
    $posts = $wpdb->get_results(« SELECT post_content FROM $wpdb->posts WHERE post_content LIKE (‘%<img%’) AND post_content LIKE (‘%$domain%’) »);
    ?>
    <h3><?php echo $domain; ?></h3>

      <?php
      foreach ($posts as $post) :
      preg_match_all(‘|<img.*?src=[' »](.*?)[' »].*?>|i’, $post->post_content, $matches);

      foreach ( $matches[1] as $url ) :
      if ( strstr( $url, get_option(‘siteurl’) . ‘/’ . get_option(‘upload_path’) ) )
      continue; // Already local

      $filename = basename ( $url );
      $b = parse_url( $url );
      $dir = ABSPATH . get_option(‘upload_path’) . ‘/’ . $domain . dirname ( $b[‘path’] );

      mkdirr( $dir );

      $f = fopen( $dir . ‘/’ . $filename , ‘w’ );
      $img = file_get_contents( $b[‘scheme’] . ‘://’ . $b[‘host’] . str_replace(‘ ‘, ‘%20’, $b[‘path’]) . $b[‘query’] );

      if ( $img ) {
      fwrite( $f, $img );
      fclose( $f );
      $local = get_option(‘siteurl’) . ‘/’ . get_option(‘upload_path’) . ‘/’ . $domain . dirname ( $b[‘path’] ) . « /$filename »;
      $wpdb->query(« UPDATE $wpdb->posts SET post_content = REPLACE(post_content, ‘$url’, ‘$local’); »);
      echo  »

    • Cached $url
    • « ;
      // Le $url est l’adresse de l’image qui a été copiée et qu’il faudrait modifier mais je ne sais pas où !!

      flush();
      }
      endforeach;
      endforeach;
      ?>

    <?php
    endforeach;
    ?>
    <h3>All done!</h3>
    <?php endif; ?>
    </div>
    <?php
    }

    add_action(‘admin_menu’, ‘mm_ci_add_pages’);

    ?>

  • Avatar de Inconnu

    En créant cette entrée, vous n’avez pas spécifié une adresse de blog ou de raison de publier.

    Ce forum de support est destiné aux blogs hébergés sur WordPress.com. Si votre question se rapporte à un blog WordPress hébergé sur votre site vous trouverez de l’aide ici Communauté Française de WordPress.

    Si vous ne connaissez pas la différence entre WordPress.com et WordPress.org, vous avez de l’information utile à cet endroit .

    Si vous avez oublié d’inclure un lien vers votre blog, vous pouvez répondre et l’inclure ici. Cela aidera les gens à répondre à votre question.

    Ceci est un message automatisé.

  • Le sujet ‘Cache Images’ est fermé aux nouvelles réponses.