ed
Member
Registered: 10th Sep 03
User status: Offline
|
Is there anyone on here who is handy with regular experssions and PHP?
I'm using FCKEditor on my website, but it outputs images like this:
<img src="http://ecsid.co.uk/images/picture.jpg" width="100" height="50" />
I'm not too keen on setting the size of the image using HTML as browsers tend to do a crappy job of resizing the image, as well as having to download the image at full res.
What I normally do is use a script which dynamically resizes the image with a touch of mod_rewrite like so:
<img src="http://ecsid.co.uk/images/100x50/picture.jpg" />
Because I can't figure out how to mod FCKEditor to do what I want, does anyone know how I could write a little function to convert the former to the latter?
xxx
|
AndyKent
Member
Registered: 3rd Sep 05
User status: Offline
|
So you want a PHP script that takes
<img src="http://ecsid.co.uk/images/picture.jpg" width="100" height="50" />
in and outputs
<img src="http://ecsid.co.uk/images/100x50/picture.jpg" />
right?
Pretty easy. Whenever you come across a set of img tags split the string at the quotation marks. In the string array you just grab items 2, 4 and 6.
4 and 6 will be the width by height, 2 will be the image address. Split the address at the forward slashes and just insert the height at the right place.
Is that what you were after or do you not know PHP at all?
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
I was wanting to use regex rather than splitting it because if for some reason the width and height tags are in different places within the image code the script wouldn't work.
|
Reedy
Member
Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
|
Why dont you just manually change it in the source part of fckeditor?
|
AndyKent
Member
Registered: 3rd Sep 05
User status: Offline
|
If its autogenerated, it should be in the same place in the same order every time surely?
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
But you have the option to insert HTML manually too, so it might not always be in that order.
|
AndyKent
Member
Registered: 3rd Sep 05
User status: Offline
|
Oh I see. I don't know regex at all, so I would just be splitting as said, maybe compare the text strings pulled out before the numbers so you can 'read' which measurement it is even if its in a different order.
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
code:
$url = "<img src="http://ecsid.co.uk/images/picture.jpg" width="100" height="50" />";
$pattern = array('/images\//', 'width=\"[0-9]?\"', 'height=\"[0-9]?\"' );
$replace = array('images\/100x50\/', '', '');
echo preg_replace($pattern, $replace, $url);
Don't blame me if that doesn't work as it hasn't been tested!
|