<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>图片 &#8211; 李辉 / Grey Li</title>
	<atom:link href="https://greyli.com/tag/%E5%9B%BE%E7%89%87/feed/" rel="self" type="application/rss+xml" />
	<link>https://greyli.com</link>
	<description>一个编程和写作爱好者的在线记事本</description>
	<lastBuildDate>Sat, 15 Nov 2025 10:55:15 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.26</generator>

<image>
	<url>https://greyli.com/wp-content/uploads/2025/03/avatar-500-compressed-144x144.jpg</url>
	<title>图片 &#8211; 李辉 / Grey Li</title>
	<link>https://greyli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>（使用 HTML、JavaScript、Flask 或 Nginx）为丢失的图片显示默认图片</title>
		<link>https://greyli.com/display-default-image-for-missing-image/</link>
		<comments>https://greyli.com/display-default-image-for-missing-image/#respond</comments>
		<pubDate>Sat, 23 Feb 2019 10:58:46 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[图片]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=2307</guid>
		<description><![CDATA[当在 HTML 页面上显示图片时，如果图片不存在，我们通常需要显示一个默认图片。 假设我们的图片路径在 /im [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>
	当在 HTML 页面上显示图片时，如果图片不存在，我们通常需要显示一个默认图片。
</p>
<p>
	假设我们的图片路径在 /imgs 下，默认图片为 /imgs/default.jpg，下面是一些常见的处理方法示例。
</p>
<h2>
	Solution 1: HTML / JavaScript<br />
</h2>
<p>
	最简单的，你可以使用 &lt;img&gt;&nbsp;元素的&nbsp;<code>onerror</code>&nbsp;属性来设置默认图片：
</p>
<pre>
<code>&lt;img src=&quot;/imgs/cat.jpg&quot; onerror=&quot;this.src=&#39;/imgs/default.jpg&#39;&quot;&gt;</code></pre>
<p>
	类似的，你也可以使用 JavaScript（jQuery）监听 img 元素的 error 事件（当图片地址无效时浏览器会触发这个事件）：
</p>
<pre>
<code>$(&#39;img&#39;).on(&quot;error&quot;, function() {
  $(this).attr(&#39;src&#39;, &#39;/imgs/default.jpg&#39;);  // 替换为默认图片
});</code></pre>
<h2>
	Solution 2: Flask<br />
</h2>
<p>
	除此之外，你也可以在服务器端处理，以 Flask 为例，你可以写一个自定义视图函数来加载图片：
</p>
<pre>
<code>import os
from flask import send_from_directory

# ...

@app.route(&#39;/img/&lt;path:filename&gt;&#39;)
def get_image(filename):
    img_path = os.path.join(images_path, filename)  # 获取图片路径

    if not os.path.exists(img_path):  # 判断图片文件是否存在
        return send_from_directory(os.path.join(images_path, &#39;/imgs/default.jpg&#39;))
    return send_from_directory(img_path)</code></pre>
<p>
	在模板里，使用这个视图函数获取图片 URL：
</p>
<pre>
<code>&lt;img src=&quot;{{ url_for(&#39;get_image&#39;, filename=</code>&#39;imgs/&#39;&nbsp;+ <code>image.filename) }}&quot; &gt;</code></pre>
<h2>
	Solution 3: Nginx<br />
</h2>
<p>
	在生产环境下，出于性能的考虑，我们通常会使用 Web 服务器（比如 Nginx / Apache）&nbsp;来服务（serve）静态文件。以 Nginx 为例，你可以使用&nbsp;<a href="http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files"><code>try_files</code></a>&nbsp;指令来设置默认图片：
</p>
<pre>
<code>location /imgs/ {
    try_files $uri /imgs/default.jpg;
}</code></pre>
<p>
	附注：本文改写自我的<a href="https://stackoverflow.com/a/54684256/5511849">SO&nbsp;回答</a>。</p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/display-default-image-for-missing-image/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>让 CKEditor 图片响应式（responsive）</title>
		<link>https://greyli.com/ckeditor-image-responsive/</link>
		<comments>https://greyli.com/ckeditor-image-responsive/#respond</comments>
		<pubDate>Wed, 12 Dec 2018 09:10:39 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[CKEditor]]></category>
		<category><![CDATA[响应式]]></category>
		<category><![CDATA[图片]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=2027</guid>
		<description><![CDATA[通过 CKEditor 上传并插入图片后，CKEditor 的图片部件（widget）会在图片的 &#60;im [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>
	通过 CKEditor 上传并插入图片后，CKEditor 的图片部件（widget）会在图片的 &lt;img&gt; 元素里插入行内样式定义来设置图片的宽高，这会导致响应式布局失效：图片因为被固定了宽高，在窗口缩小后会超出外层元素。生成的 HTML 代码示例如下：
</p>
<pre>
&lt;img alt=&quot;hello&quot; src=&quot;/images/hello.jpg&quot; <strong>style=&quot;height:300px; width:500px&quot;</strong> /&gt;</pre>
<p>
	对于这个问题，有三种解决方法：
</p>
<h2>
	方法 1：设置 CSS<br />
</h2>
<p>
	最简单的解决方法是在 CSS 文件里加入下面的代码：
</p>
<pre>
img {
    max-width: 100%;
    height: auto !important;
}</pre>
<p>
	这会重写 CKEditor 生成的行内 CSS。
</p>
<h2>
	方法 2：使用插件&nbsp;Enhanced Image<br />
</h2>
<p>
	你可以使用加强版的图片部件插件&nbsp;<a href="https://ckeditor.com/cke4/addon/image2">Enhanced Image</a>（Image2）来替代原有的图片部件，同时确保 CKEditor 的版本大于 4.3。
</p>
<p>
	（未测试。）
</p>
<h2>
	方法 3：使用 JavaScript<br />
</h2>
<p>
	如果你使用 Bootstrap，这里还有另一种方法：
</p>
<pre>
CKEDITOR.on(&#39;instanceReady&#39;, function (ev) {
    ev.editor.dataProcessor.htmlFilter.addRules( {
        elements : {
            img: function( el ) {
                // Add bootstrap &quot;img-responsive&quot; class to each inserted image
                el.addClass(&#39;img-responsive&#39;);
        
                // Remove inline &quot;height&quot; and &quot;width&quot; styles and
                // replace them with their attribute counterparts.
                // This ensures that the &#39;img-responsive&#39; class works
                var style = el.attributes.style;
        
                if (style) {
                    // Get the width from the style.
                    var match = /(?:^|\s)width\s*:\s*(\d+)px/i.exec(style),
                        width = match &amp;&amp; match[1];
        
                    // Get the height from the style.
                    match = /(?:^|\s)height\s*:\s*(\d+)px/i.exec(style);
                    var height = match &amp;&amp; match[1];
        
                    // Replace the width
                    if (width) {
                        el.attributes.style = el.attributes.style.replace(/(?:^|\s)width\s*:\s*(\d+)px;?/i, &#39;&#39;);
                        el.attributes.width = width;
                    }
        
                    // Replace the height
                    if (height) {
                        el.attributes.style = el.attributes.style.replace(/(?:^|\s)height\s*:\s*(\d+)px;?/i, &#39;&#39;);
                        el.attributes.height = height;
                    }
                }
        
                // Remove the style tag if it is empty
                if (!el.attributes.style)
                    delete el.attributes.style;
            }
        }
    });
});</pre>
<p>
	（<a href="https://gist.github.com/fabiomaggio/c2f4b84756cb4d82c0ae">出处地址</a>）
</p>
<p>
	需要注意的是，如果你使用 Bootstrap 4，需要把第 6 行的 img-responsive 换成 img-fluid。
</p>
<p>
	（未测试。）</p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/ckeditor-image-responsive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
