在dedecms织梦系统中,要去掉文章内容中的图片宽度和高度限制,可以修改系统配置文件或使用自定义函数进行处理。
在织梦(DedeCMS)的内容管理中,有时候需要去除文章内容中图片的宽度和高度限制,以实现响应式布局或满足特定设计需求,以下是几种常见的方法:

1、通过修改模板文件
步骤页模板文件(通常是article_article.htm),将调用文章内容的标签{dede:field.body/} 改为{dede:field.body runphp=yes},然后在其中加入正则表达式替换代码,去除图片的宽度和高度属性。
示例代码:
{dede:field.body runphp=yes}
global $cfg_basehost;
$str = @me;
$search = '/(<img.*?)width=(["\'])?(?(2)\2|\s)([^>]+)/is';
$search1 = '/(<img.*?)height=(["\'])?(?(2)\2|\s)([^>]+)/is';
$search2 = '#(<img.*?style=".*?)width:\s{0,}\d+px;([^"]*?.*?>)#i';
$search3 = '#(<img.*?style=".*?)height:\s{0,}\d+px;([^"]*?.*?>)#i';
$content = preg_replace($search,'$1$3',$str);
$content = preg_replace($search1,'$1$3',$content);
$content = preg_replace($search2,'$1$2',$content);
$content = preg_replace($search3,'$1$2',$content);
@me = str_replace('/uploads/allimg/', $cfg_basehost.'/uploads/allimg/', $content);
{/dede:field.body}
2、通过修改系统文件
步骤:打开include/arc.archives.class.php 文件,找到设置全局环境变量的部分,在其下方加入代码,使用正则表达式替换掉图片的宽度和高度属性。

示例代码:
$this>Fields['body'] = preg_replace("/style=\"width\:(.*)\"/","",$this>Fields['body']);
$this>Fields['body'] = preg_replace("/style=\"height\:(.*)\"/","",$this>Fields['body']);
3、通过 JavaScript 动态调整
步骤:在前台文章页模板中加入 JavaScript 代码,当页面加载完成后,遍历所有图片元素,去除其宽度和高度属性。
示例代码:
<script language='javascript'>
document.addEventListener("DOMContentLoaded", function() {
var images = document.getElementsByTagName('img');
for (var i = 0; i < images.length; i++) {
images[i].removeAttribute('width');
images[i].removeAttribute('height');
images[i].removeAttribute('style');
}
});
</script>
4、通过 CSS 控制图片大小

步骤:在样式表中添加 CSS 规则,控制图片的最大宽度,使其不超过容器的宽度。
示例代码:
img {
maxwidth: 100%;
height: auto;
}
去除织梦文章内容中图片的宽度和高度限制可以通过多种方法实现,包括修改模板文件、系统文件、使用 JavaScript 动态调整以及 CSS 控制,每种方法都有其适用场景和优缺点,开发者可以根据具体需求选择合适的方案。