Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2012-03-12 22:00:48

kvnmcwebn
Member
From: Ireland
Registered: 2007-01-27
Posts: 724
Website

Re: Image name instead ID in html

Hi this is really great.
I have a question about nested folders inside the images folder.
I’m using fpx image import to batch upload and automatically generate categories.
So the folder structure is like this:

images > import > category
Is there a way i can modify the images folder htaccess file so that it works with the nested folders.
Thanks a mill


its a bad hen that wont scratch itself.
photogallery

Offline

#14 2012-03-12 23:54:04

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: Image name instead ID in html

I thought fpx image import moved the files to the images directory.


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#15 2012-03-13 02:06:28

kvnmcwebn
Member
From: Ireland
Registered: 2007-01-27
Posts: 724
Website

Re: Image name instead ID in html

hi matt. you’re right… sorry. not sure what the problem is then… I will take another look at.


its a bad hen that wont scratch itself.
photogallery

Offline

#16 2012-03-13 23:42:31

kvnmcwebn
Member
From: Ireland
Registered: 2007-01-27
Posts: 724
Website

Re: Image name instead ID in html

ah awesome working now. I’m just outputting a simple list of images like this:

<txp:images category="sligo" break="" wraptag="ul" sort="id asc"  >
<li class="imagelist" >  
<img title="<txp:image_info type="caption" />" alt="<txp:image_info type="alt" />" 
src="<txp:site_url />images/<txp:image_info type="id" />/<txp:image_info type="name" />" /></li>
<li class="imageinfo"><txp:image_info type="caption" /></li>
</txp:images>

That is in an article. Now that I look at it I guess it should be more like this.

<txp:images category="sligo" break="li" wraptag="ul" sort="id asc" form="imagelist" />

Even that seems like it shouldn’t be in an article…

Anyway I’m going to switch the main gallery of my photo blog over to this now. It is a modified version of the widescreen template so it uses a custom field instead of the article image field. The custom field has the same image id/function as an article image would. I know it’s a long shot but is there a way i could get away with still using the custom field instead of an article image within the images tag. There are hundreds of articles to change over so I thought i would ask first.

Here is the existing form:

<li><a href="<txp:permlink />" title="<txp:title />">
<img src="<txp:site_url />
images/<txp:custom_field name="Image-ID" />t.jpg" 
alt="Thumbnail of <txp:title />" /></a>

thanks

Last edited by kvnmcwebn (2012-03-13 23:46:45)


its a bad hen that wont scratch itself.
photogallery

Offline

#17 2012-05-04 15:01:30

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Image name instead ID in html

This is an improved (I think!) version of the .htaccess trick that works well with smd_thumbnail (which creates subfolders to store images for each thumbnail profile).

I recommend to put this in an .htaccess file inside the your images folder. This way, by not putting it on your general .htaccess (the one at the root of your website), you avoid the rules being run on every request.

RewriteEngine On

# Uncomment one of the following lines in GoDaddy (depending from where you are serving images)
#RewriteBase /images/
#RewriteBase /static/images/

# Matches any URL of the kind /images/123/string.jpg and rewrites it internally to /images/123.jpg
RewriteRule ^([0-9]+)/(.+)(png|gif|jpg|bmp)$ $1.$3 [NC,L]

# Similarly to the above one, but this one matches those URLS that are in a subfolder like: /images/square/123/string.jpg 
# and rewrites them to /images/square/123.jpg
RewriteRule ^([^/]+)/([0-9]+)/(.+)(png|gif|jpg|bmp)$ $1/$2.$4 [NC,L]

I couldn’t find a way to merge the two rewrite rules into one, maybe someone clever than me could take it to that next level…


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#18 2014-07-15 13:36:14

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

Re: Image name instead ID in html

Hi ;)

Very nice and useful tip! Thanks lot.

It seems that some bots (Google) try to smell into all Directories included in URLs. To avoid access to /images/1/ folder here is some changes:

# Protect this directory from direct access
ErrorDocument 401 default
# Enable rewrite
RewriteEngine On
# Set the current base URL
RewriteBase /images/
# Matches any URL of the kind /images/123/string.jpg and rewrites it internally to /images/123.jpg
RewriteRule ^([0-9]+)/(.+)(png|gif|jpg|bmp)$ $1.$3 [NC,L]
# Similarly to the above one, but this one matches those URLS that are in a subfolder like: /images/square/123/string.jpg 
# and rewrites them to /images/square/123.jpg
RewriteRule ^([^/]+)/([0-9]+)/(.+)(png|gif|jpg|bmp)$ $1/$2.$4 [NC,L]
# Restaure TXP 404 page for all other access
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#19 2015-02-25 16:37:53

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Image name instead ID in html

Hi all,
I’ve rewritten the regex a bit, to have just one rule to rule them all. :P

	# ------------------------------------------------------------------------------
	# | SEO/human-friendly URLs for images                                         |
	# ------------------------------------------------------------------------------

	# Matches any URL of the kind:

	#     images/123/some-string.jpg 
	#     images/123-some-string.jpg 
	#     images/123t/some-string.jpg 
	#     images/123t-some-string.jpg 
	#     images/small/123/some-string.jpg (in case you are using smd_thumbnail)
	#     images/small/123-some-string.jpg (in case you are using smd_thumbnail)

	# and rewrites it internally to: 

	#    images/123.jpg
	#    images/123t.jpg
	#    images/small/123.jpg

	<IfModule mod_rewrite.c>
		RewriteEngine On
		RewriteRule ^(.+)?(/[0-9]+t?)(?:[-/]).+(png|gif|je?pg|bmp|webp)$ $1$2.$3 [NC,L]
	</IfModule>

	# You can put this rule:
	# a) on the main .htaccess on the root (DocumentRoot) of your website, or
	# b) on an .htaccess file located inside the /images/ folder (better performance)
	# It should work flawlessly on both places.
	# + info: http://forum.textpattern.com/viewtopic.php?pid=247847

Test and report back :)

See also the other half for this trick (on how to implement SEO/human-friendly URLs for images).

Edit: I’ve tighten up the regex a little, to work even when one of the subfolders in the URL might have numbers on its name (which might be the case if you are using it in in tandem with this smd_thumbnail snippet for responsive images)

Last edited by maniqui (2016-03-14 18:22:54)


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#20 2015-02-25 17:41:16

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,243

Re: Image name instead ID in html

Thanks, Julián! Very large-hearted, again :)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#21 2015-02-25 18:08:05

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Image name instead ID in html

You are welcome, uli. :)

I’ll expand this tip, to explain how to craft this SEO/human-friendly URLs, so to take advantage of this .htaccess trick.

It basically involves using tags like txp:image_info to output the small pieces of information to craft the proper URL.
For example:

<txp:images>
  <img src="/images/<txp:image_info type="id" />/<txp:image_info type="name" /><txp:image_info type="ext" />
</txp:images>

Or, if you are using something like the smd_gallery plugin, that would be:

<txp:smd_gallery>
   <img src="/images/{id}/{name}{ext}" />
</txp:smd_gallery>

As you can see, we use the name of the image (editable for each image via Admin > Content > Images) as the source of the SEO/human-friendly string.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#22 2015-09-23 05:55:00

Pyanepsion
New Member
From: Clermont-Ferrand
Registered: 2015-09-22
Posts: 7
Website

Re: Image name instead ID in html

Rewriting is not a good solution. It is as if suddenly your faucet dripped a somewhat muddy trickle and you thought fix the thing by increasing the pressure of the water tower upstream and use filters and disinfectants downstream. Water will be certainly more fit for consumption, but it would have been better to simply repair the leak.

Here, the leak is the coding that replaces the image name with a number. So it is here it will act.


Mais je vous le demande : peut-on encore parler de police sans sérifs ?

Offline

#23 2015-09-23 17:13:25

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

Re: Image name instead ID in html

Pyanepsion, you may be right, but for better or worse the decision to use a number-based naming scheme was taken in the very early days of textpattern before most of us were around. Changing it would require modifications at all levels – in the file system, the admin system, the way of attaching images to an article as well as the image output tags … not to mention the problem of maintaining backwards compatibility…

The one advantage that using an image number has is that the image name can be changed at a later date without breaking existing links. The rewriting method maniqui describes is really only a cosmetic addition as the name part is irrelevant for the rewrite: only the id and ext are used. The name part can be anything, so you can tailor it to your own SEO needs.


TXP Builders – finely-crafted code, design and txp

Offline

#24 2015-09-24 21:07:59

qrayg
Member
From: USA
Registered: 2004-08-27
Posts: 81
Website

Re: Image name instead ID in html

This whole thread is exactly why on my dozen or so well trafficked TXP sites, I do not use or allow the customer to use the Image library. Maintaining these sites after years of customer use and hundreds of number-based image file names would make my life far more complicated than it needs to be. Allowing FTP access to an image folder is far simpler and allows for greater control IMO.

I love TXP, but this is the reason why it’s hard to sell to other developers and customers that want a decent media library solution in their CMS.

I understand it would be a massive change to make the images retain their name on upload and still allow every other aspect of the CMS to work without a hitch, but there has to be another way.

Umbraco uses that number id as a folder rather than renaming the image. Could that possibly be a solution or a path to solution that could work with TXP? I’m ok with that ID being part of the path to the image, I just don’t want TXP renaming my already organized naming scope.

Offline

Board footer

Powered by FluxBB