Resize img tags in a Wordpress feed

79 views Asked by At

I would like to update the_content_rss in WordPress so it would fit feedburner. It would need to find every img tag, see if it has a width and height set. If they are larger than 600 they should be reduced to 600. If they are not set, then width should be set to 600. I thought of using some of the code here to do it, but I got a bit stuck, I would appreciate help with fixing it.

Questions:

  1. Does it work?
  2. How can it find if width is null - and in which case to add it?

    <?php
    function feedburner_img_resize($the_content) {
    // Create a new istance of DOMDocument
    $post = new DOMDocument();
    // Load $the_content as HTML
    $post->loadHTML($the_content);
    // Look up for all the <img> tags.
    $imgs = $post->getElementsByTagName('img');
    
    // Iteration time
    foreach( $imgs as $img ) {    
    
        // if width is smaller than 600 - no need to continue
        $width = $img->getAttribute('width');
        if( $width < 600 ) continue;
    
        $img->removeAttribute('width');
        $img->setAttribute('width', 600);
        $img->removeAttribute('height'); // so the image is not distorted
    };
    
     return $post->saveHTML();
    }
    
    add_filter('the_content_rss', 'feedburner_img_resize');
    ?>
    
1

There are 1 answers

0
MobiDevices On

In function.php add the following function:

function aq_resize($url,$width,$height=null,$crop=null,$single=true){
    $up_info=wp_upload_dir();
    $up_dir=$up_info['basedir'];
    $up_url=$up_info['baseurl'];
    if (strpos($url,home_url()) === false){return false;}
    $rel_path = str_replace( $up_url, '', $url);
    $img_path = $up_dir . $rel_path;
    if (!file_exists($img_path) OR ! getimagesize($img_path)){return false;}
    $info = pathinfo($img_path);
    $ext = $info['extension'];
    list($orig_w,$orig_h) = getimagesize($img_path);
    $dims = image_resize_dimensions($orig_w, $orig_h, $width, $height, $crop);
    $dst_w = $dims['4'];
    $dst_h = $dims['5'];
    $suffix="{$dst_w}x{$dst_h}";
    $dstrel=str_replace('.'.$ext,'',$rel_path);
    $dest="{$up_dir}{$dstrel}-{$suffix}.{$ext}";
    if($width >= $orig_w) {
        if(!$dst_h) :
            $img_url=$url;
            $dst_w=$orig_w;
            $dst_h=$orig_h;

        else :
            if(file_exists($dest) && getimagesize($dest)) {
                $img_url="{$up_url}{$dstrel}-{$suffix}.{$ext}";
            }
            else {
                $resized=resize_image($img_path,$width,$height,$crop);
                $resized_rel=str_replace($up_dir,'',$resized);
                $img_url=$up_url.$resized_rel;
            }
        endif;
    }
    elseif(file_exists($dest) && getimagesize($dest)) {
        $img_url="{$up_url}{$dstrel}-{$suffix}.{$ext}";
    }
    else {
        $resized=resize_image($img_path,$width,$height,$crop);
        $resized_rel=str_replace($up_dir,'',$resized);
        $img_url=$up_url.$resized_rel;
    }

    if($single) {
        $image = $img_url;
    } else {
        $image = array (
            0 => $img_url,
            1 => $dst_w,
            2 => $dst_h
        );
    }
    return $image;
}

In a place where the need conclusion miniatures, write the following, changing the size of the thumbnails on the right for you:

<img src="<?php echo aq_resize(first_img(),180,130,true)?>