Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-01-06 23:31:08

kuopassa
Plugin Author
From: Porvoo, Finland
Registered: 2008-12-03
Posts: 221
Website

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

#2 2023-01-07 00:09:05

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 1,989
Website Mastodon

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

#3 2023-01-07 02:26:30

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 2,797
Website

Re: Keywords in image file names

  1. you can already add custom fields to the image edit panel with one of Jakob‘s extensions, jcr_image_custom
  2. as you know you can alter the file name for an image on the same panel
  3. (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

#4 2023-01-07 06:49:09

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,535
GitHub Twitter

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

#5 2023-01-07 18:12:31

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,428
Website

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

#6 2023-01-08 14:24:10

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,535
GitHub Twitter

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

Board footer

Powered by FluxBB