Php 在Wordpress提要中调整img标记的大小
我想更新WordPress中的内容,使其适合feedburner。它需要找到每个img标签,看看是否设置了宽度和高度。如果它们大于600,则应减少到600。如果未设置,则宽度应设置为600。我曾想用一些代码来做这件事,但我有点卡住了,如果能帮我修复它,我将不胜感激 问题:Php 在Wordpress提要中调整img标记的大小,php,wordpress,domdocument,feedburner,Php,Wordpress,Domdocument,Feedburner,我想更新WordPress中的内容,使其适合feedburner。它需要找到每个img标签,看看是否设置了宽度和高度。如果它们大于600,则应减少到600。如果未设置,则宽度应设置为600。我曾想用一些代码来做这件事,但我有点卡住了,如果能帮我修复它,我将不胜感激 问题: 它有用吗 如果width为null,它如何查找?在何种情况下添加它 <?php function feedburner_img_resize($the_content) { // Create a new istance
<?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');
?>
在function.php中添加以下函数:
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;
}
在需要缩图的地方,写下以下内容,为您更改右侧缩略图的大小:
<img src="<?php echo aq_resize(first_img(),180,130,true)?>
考虑使用javascript吗?我需要使用php对其进行更改,以更新add\u filter…我对wordpress不太熟悉,但您可以发出ajax请求,然后将其传递到php并添加\u filter()