Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Keywords in image file names
Has anyone thought about this issue? For SEO it’s desirable to have a keyword or two in a image file name, but currently Textpattern uses a simple running number for uploaded images.
Here is an addition to .htaccess file used in Textpattern’s root that makes it possible to put custom keywords (with latin alphabet) to existing image file names without “physically” altering the file name. This assumes images are inside images
folder.
RewriteRule ^([0-9a-z-]+)-([0-9]+).(apng|png|gif|webp|avif|jpg|jpeg|jfif|pjpeg|svg|bmp|tif|tiff)$ ./images/$2.$3?slug=$1 [L]
Key points:
- $2 = running number
- $3 = file extension
- $1 = keywords
An example:
images/1.png
would become tomato-1.png
and/or cabbage-1.png
Maybe someone finds this idea interesting and develops it further. ;-) For example a dedicated input field in Textpattern’s Edit image page could added, which would then store the keyword(s) to a database table.
Offline
Re: Keywords in image file names
I am in awe of what you are able to do in .htaccess
So to clarify. I upload an image called cat_on_bike.png. It goes into the TxP DB as 45.png , But your .htaccess
rule renders it to indexing robots as the name i used when it was uploaded. Cool
…. texted postive
Offline
Re: Keywords in image file names
- you can already add custom fields to the image edit panel with one of Jakob‘s extensions, jcr_image_custom
- as you know you can alter the
file name
for an image on the same panel - (thinking out loud, absolutely untested) you could then build a shortcode to combine the fields above to insert in your articles, and then have some redirect in your htaccess to the
id.ext
on the file system.
example: the shortcode would output <img src=/images/keyword-filename-id.jpg alt=this-or-that>
in your HTML, and you then redirect to /images/id.jpg
Where is that emoji for a solar powered submarine when you need it ?
Offline
Re: Keywords in image file names
It’s working fine on my local MAMP server (PC) with a little amendment:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([0-9a-z-]+)-([0-9]+).(apng|png|gif|webp|avif|jpg|jpeg|jfif|pjpeg|svg|bmp|tif|tiff)$ $2.$3 [L]
</IfModule>
Note because the rule is located into the /images
folder we don’t need to specify it.
Now, the image tag can be used as this:
<txp:site_url />images/<txp:image_info type="name" />-<txp:image_info type="id" /><txp:image_info type="ext" />
And https://mydomain.tdl/images/my-image-name-ID.ext
is rendering from the front-end part. 👌
Last edited by Pat64 (2023-01-07 06:52:40)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Keywords in image file names
Hehe, here’s a thread from 2011 on the same topic using the same basic approach (hat tip to Julián and Jukka).
TXP Builders – finely-crafted code, design and txp
Offline
Re: Keywords in image file names
This solves all cases with or without file name, even with its extension or display the current format (ID) if blank:
<img loading="lazy" itemprop="url contentUrl" src="<txp:site_url />images/<txp:evaluate query test='<txp:image_info type="name" />'><txp:image_info type="name" trim="/(.*[^\.])(\.)(.*)?/" replace='$1' />-<txp:else /></txp:evaluate><txp:image_info type="id" /><txp:image_info type="ext" />" alt="-">
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline