RegEx - change width and height in an image

ok …
i have x number of img tags in some text … i want to change in all img tags (maybe one, maybe 10 or more of such tags in some HTML text) widht and height to some value. I want to set widht to specific value and have height changed automaticallly to constrain proportions …

Problem is that I don’t know whether image is in portrait or landscape mode and I don’t know it’s size or whether tag is going like this

<img src=“kkkk” height=“100” width=“100” … >
<img height=“100” width=“100” src=“kkkk” … >
<img width=“100” src=“kkkk” height=“100” … >
or something else

so you get the point … can someone please help me with this one?

crnaovca, efficiency requires reliability, which requires precision and carefulness, always, even more in programming, and even one step more in using RegEx (Regular Expressions). So please check your text before posting, and don’t make errors like “widht”, and even worse, don’t repeat it…

In all browsers I think, if you set ONE of the 2 dimensions (width OR height), then the browser will adjust the other dimension so to keep the ratio unaltered and render the image correctly. So in your case, you just REMOVE all the “height=yyy”, and replace all the “width=xxx” with your own chosen “width=aaa”.

Now to do it, this depends on your HTML editor, and the number of items you have. I personally use Notepad++, in which you can easily make a RegEx that will do this on ALL “width=xxx” or “height=yyy”, no matter the "<IMG " or else; then you can do it item by item, just clicking on each item either “Replace” (if you have visually checked it is actually an IMG) or “Find text” (if this is NOT an IMG); this is better than just “Find”, yet not as well as a truly efficient editor, so it will work IMO for a few dozens items, no more.

Please tell which editor(s) you are using, so some others may help further.

Versailles, Tue 11 May 2010 18:12:15 +0200

Ok, it was an error in typing, I know that, and if I had error like that, I would correct it easily.
English is not my main language … but I aknowledge your remark …

Yes … it COULD be done in that way, to remove height and change width … but that is not a good solution. Images that have size set in that way depends on browser capabilities to do that resize. Not ALL browser do that, and not all of them do it in correct way. You have lot of examples when images are resized, but their quality is extremly bad.
I wanted to use regex in my process, but I have problem that i described above … some images are in landscape, some in portrait, some are not standard size … I tried regex buddy, but cannot find a “rule” how to retrieve those values and how to write it down … I can do it manually with search function in programming language, but that is not solution that would be adequate for this.

This shouldn’t get involved at all here. Just pick the width, the height, compute 1st the existing ratio, 2nd use it to compute the new height.

Thx for making me know RegexBuddy, it seems very friendly and powerful. Also, when you have found how to retrieve the existing width and height, compute the new height, and insert the new width and height in their due place in each IMG tag, then could you remember to come back and explain how you did and with which tools (RegexBuddy, [URL=“”]Notepad++ or any other ones). TIA,

Versailles, Wed 12 May 2010 19:50:25 +0200

no comment …
my question was pretty straight forward … and i would like answer to that question … not other solution … this is not my first project …
i KNOW i can do it through php or asp with simple find or search in string …
but i want to use regex … and I don’t want lesson from english, i want lesson from regex, i don’t want to hear other solutions … i know why I want THIS solution …
i DON’T know how to make regex code to do what I described above … to cover all situations I have … if it is possible - help, if not, please let someone else answer …

what you are giving me are beginners answers and I know that, I need professional in regex …

so to repeat my question …

I have some text. In it I have some X number of pictures. Each of it could have width and height set or doesn’t and I don’ know where are they situated inside IMG tag …

I want to retrieve values of width and height for all pictures with php using regex … how to do that …

I think it is entirely possible to use regex to do this, for at least some if it.

But the regex would be extremely “ugly”, the code to manipulate the data would be extremely convoluted, and all-in-all it would be a solution that’s much more complicated than it needs to be. IMHO the alernatives are not “beginner” as much as they are “more efficient”.

Personally, I would use DOM to work with the attributes.

But if you absolutely must use regex, I’m interested in knowing why.

Ok, so this is answer to my question … I understood from one book I started to read, that with regex it should be easier and more precise than with my first thought of it.
Problem is with CMS I have to extend, so I thought that with regex i could do this task with less code and to even try to reduce server load … it is heavy traffic site (300 000 users per month by google ad planner (570 000 by google analytics) so I want to try different things to reduce server load, and I never used regex … why not to try …

Regex would be easy enough if the tag format was consistent. But where it might be

<img src=“kkkk” height=“100” width=“100” … >
<img height=“100” width=“100” src=“kkkk” … >
<img width=“100” src=“kkkk” height=“100” … >
or something else

It’s going to get real messy real quick trying to make sure every variation will work.

Maybe it would help if you put all the image tags into the same convention?

problem is that 20 different people are using administration of CMS and they are not tech savy … (i think that is the phrase)
So since images and web is manual process, wyswyg editors just help a little, they do it as they do it, clumsy, and I must compensate … So i am trying to find best solution …

ok then … it stays as it is … :smiley:

I don’t know as I’d leave it as it is. Only that using regex would be messy and inefficient. But using DOM should work OK IMHO.

It wouldn’t matter if they did

height=125 width=525
width=‘550’ height=‘225’
height=“600” alt=“logo” width=“400”

the image’s getAttribute would still be the same