Post Reply 
Replace image with specific dimension
Sep. 29, 2009, 06:09 PM
Post: #1
Replace image with specific dimension
Hi!

I'm trying to make a filter to replace images with a specific dimension from any website.
Say, a website has an image with 40x50px, the filter has to replace it and all other images won't be touched.

Sounds easy, but struggling with it for hours.

This is one of the versions I tried:

Name = "Images: Replace by own - test 2"
Active = TRUE
Multi = TRUE
URL = "$TYPE(htm)"
Limit = 40000
Match = "<img \1 ( ( * (height="50")\2 * ) & ( * (width="40")\3 * ) ) \4 >"
Replace = "<img \1 src="http://192.168.0.15/playground/proxy/replace_40_50.jpg" \2 \3 \4 >"

This is my test html:

<p>start</p>
<img src="image.jpg" width="100" height="70" />
<p>bla bla bla</p>
<p>bla bla bla</p>
<img src="example.jpg" width="40" height="50" />
<p>end</p>

If the filter would work correctly, the last image should be replaced.

This is the filter result:

<p>start</p>
<img src="http://192.168.0.15/playground/proxy/replace_100_150.jpg" height="50" width="40" height="50" / >
<p>end</p>

As you can see, the first img-tag and the p-tags in between has been removed.

Any idea what I'm doing wrong?

Thank you so much!

Cheers,
Fabian
Add Thank You Quote this message in a reply
Sep. 29, 2009, 10:55 PM (This post was last modified: Sep. 29, 2009 10:57 PM by JJoe.)
Post: #2
RE: Replace image with specific dimension
The Proxomitron matches from the first ??? to the first ??? that the bounds limit allows.

The unrestrained "*" allowed the match to consume too much.
The "\1" will always be empty.
Two "*" are not needed.
"\4" will fill with "height="50"" when "width="40"" comes first,
Lots of spaces...
( I think Wink )

Replacing "*" with "[^>]++" and removing the trailing wildcards keeps the match inside the tag.

Code:
[Patterns]
Name = "Images: Replace by own - test 2a"
Active = TRUE
Multi = TRUE
URL = "$TYPE(htm)"
Limit = 32766
Match = "<img "
        "( ( [^>]++ (height="50")\2 ) & ( [^>]++ (width="40")\3 ) )"
        " \4 >"
Replace = "<img "
          "src="http://192.168.0.15/playground/proxy/replace_40_50.jpg""
          " \2 \3 \4 >"

Test window

Code:
<p>start</p>
<img src="image.jpg" width="100" height="70" />
<p>bla bla bla</p>
<p>bla bla bla</p>
<img src="http://192.168.0.15/playground/proxy/replace_40_50.jpg" height="50" width="40" height="50" / >
<p>end</p>

Had you set a Bounds Match of <img*>.
The Bounds Match finds its match.
The Match must match the same thing.
The Proxomitron would have restricted the Match to one tag.

Code:
[Patterns]
Name = "Images: Replace by own - test 3"
Active = TRUE
Multi = TRUE
URL = "$TYPE(htm)"
Bounds = "<img*>"
Limit = 32766
Match = "<img "
        "( ( * (height="50")\2 ) & ( * (width="40")\3 ) )"
        " \4 >"
Replace = "<img \1 src="http://192.168.0.15/playground/proxy/replace_40_50.jpg" \2 \3 \4 >"

Test window

Code:
<p>start</p>
<img src="image.jpg" width="100" height="70" />
<p>bla bla bla</p>
<p>bla bla bla</p>
<img src="http://192.168.0.15/playground/proxy/replace_40_50.jpg" height="50" width="40" height="50" / >
<p>end</p>

But you still have an extra "height"

You could

Code:
[Patterns]
Name = "Images: Replace by own - test 4"
Active = TRUE
Multi = TRUE
URL = "$TYPE(htm)"
Bounds = "<img*>"
Limit = 32766
Match = "*height=$AV(50)"
        "&"
        "*width=$AV(40)"
        "&"
        "\1src=$AV(*)\2"
Replace = "\1"
          "src="http://192.168.0.15/playground/proxy/replace_40_50.jpg""
          "\2"

Test window

Code:
<p>start</p>
<img src="image.jpg" width="100" height="70" />
<p>bla bla bla</p>
<p>bla bla bla</p>
<img af src="http://192.168.0.15/playground/proxy/replace_40_50.jpg" width="40" height="50" />
<p>end</p>

You could also

Code:
[Patterns]
Name = "Images: Replace by own - test 5"
Active = TRUE
Multi = TRUE
URL = "$TYPE(htm)"
Limit = 32766
Match = "<img*>"
        "&&"
        "*height=$AV(50)"
        "&"
        "*width=$AV(40)"
        "&"
        "\1src=$AV(*)\2"
Replace = "\1"
          "src="http://192.168.0.15/playground/proxy/replace_40_50.jpg""
          "\2"

HTH

Having fun?
Add Thank You Quote this message in a reply
Sep. 30, 2009, 07:53 AM
Post: #3
RE: Replace image with specific dimension
Hi JJoe,

Thank you so much for your input and code correction!
I need to go thru it carefully to understand it (I'm a newbie).

It works perfectly!!! Hurray!

Cheers,
Fabian
Add Thank You Quote this message in a reply
Sep. 30, 2009, 09:25 AM
Post: #4
RE: Replace image with specific dimension
Hi,

The filter runs well now!

I have a minor issue which I hope to find again a solution here:
(still reg. the above filter):

This is the filter code I'm using (thanks to JJoe!)

Name = "Images: Replace by own - test 7"
Active = TRUE
Multi = TRUE
URL = "$TYPE(htm)"
Limit = 32766
Match = "<img*>"
"&&("
"(*height=$AV(100)&*width=$AV(150))|"
"(*height=$AV(50)&*width=$AV(40))|"
"(*height=$AV(110)&*width=$AV(301))"
")&"
"(*height=(\w)\2)&(*width=(\w)\3)"
"&"
"\1src=$AV(*)\4"
Replace = "\1"
"src="http://192.168.0.15/playground/proxy/replace_\3_\2.jpg""
"\4"

As you can see, I'm capturing now also the width & height per image with this "(*height=(\w)\2)&(*width=(\w)\3)"

The variables \2 and \3 do contain the values but of course surrounded by double quotes.

How can I remove them or how to capture them without?

Thank you very much!

Cheers,
Fabian
Add Thank You Quote this message in a reply
Sep. 30, 2009, 10:35 AM
Post: #5
RE: Replace image with specific dimension
You can use $AV() to capture the value without quotes.
Code:
(*height=$AV(\2))&(*width=$AV(\3))
Add Thank You Quote this message in a reply
Sep. 30, 2009, 11:44 AM
Post: #6
RE: Replace image with specific dimension
whenever: Thank you so much!!!

It works and my filter is up and running!

Cheers,
Fabian
Add Thank You Quote this message in a reply
Sep. 30, 2009, 03:19 PM
Post: #7
RE: Replace image with specific dimension
Some notes for the future.

Use Multi when filters need to see the filter's output.
Small byte limites are better.
A Bounds Match may help the Proxomitron.


Any landing that you can walk away from... ;-)
Have fun
Add Thank You Quote this message in a reply
Post Reply 


Forum Jump: