scvinodkumar Posted February 26, 2009 Share Posted February 26, 2009 hi there, i want to add the width attribute if there is not or change the width value to an img tag that comes from the content. for example, i have the following content <p>Also from the wonderful Tank Theory line is this nice Comrade t-shirt. She’s not all that innocent, not that she really looks it, but if you inspect the shirt closer you’ll see she is hiding something on the back. The shirt is now available in red with black wterbased print, and gold foil accents.</p> <p><a title="Tank Theory Comrade" href="http://www.tanktheory.com/store/standard-t-shirts/product/comrade/" target="_blank"><img class="alignnone size-full wp-image-2642" title="m_509_tank_theory_red" src="http://www.birdfight.com/blogfiles/wp-content/uploads/2009/02/m_509_tank_theory_red.jpg" alt="m_509_tank_theory_red" height="400" width="399"></a></p> or how to resize the image dynamically thanks for you help Quote Link to comment https://forums.phpfreaks.com/topic/147067-adding-or-change-img-width-value-dynamically/ Share on other sites More sharing options...
MadTechie Posted February 26, 2009 Share Posted February 26, 2009 list($width, $height) = getimagesize("img.jpg"); echo "$width, $height"; Quote Link to comment https://forums.phpfreaks.com/topic/147067-adding-or-change-img-width-value-dynamically/#findComment-772083 Share on other sites More sharing options...
scvinodkumar Posted February 26, 2009 Author Share Posted February 26, 2009 thanks for your reply, but i want to resize the image from the content dynamically Quote Link to comment https://forums.phpfreaks.com/topic/147067-adding-or-change-img-width-value-dynamically/#findComment-772090 Share on other sites More sharing options...
The Little Guy Posted February 26, 2009 Share Posted February 26, 2009 and you would, you just take his code and do this: $img = 'img.jpg'; list($width, $height) = getimagesize($img); echo '<img src="'.$img.'" width="'.($width/2).'" height="'.($height/2).'" />'; Quote Link to comment https://forums.phpfreaks.com/topic/147067-adding-or-change-img-width-value-dynamically/#findComment-772097 Share on other sites More sharing options...
premiso Posted February 26, 2009 Share Posted February 26, 2009 With the image tag: <?php $string = '<p>Also from the wonderful Tank Theory line is this nice Comrade t-shirt. She’s not all that innocent, not that she really looks it, but if you inspect the shirt closer you’ll see she is hiding something on the back. The shirt is now available in red with black wterbased print, and gold foil accents.</p> <p><a title="Tank Theory Comrade" href="http://www.tanktheory.com/store/standard-t-shirts/product/comrade/" target="_blank"><img class="alignnone size-full wp-image-2642" title="m_509_tank_theory_red" src="http://www.birdfight.com/blogfiles/wp-content/uploads/2009/02/m_509_tank_theory_red.jpg" alt="m_509_tank_theory_red" height="400" width="399"></a></p>'; $string = '<p>testing this other image <img src="blahblah.jpg" height="50">'; if (stristr($string, "<img") !== false) { if (preg_match('~<img.*width="(.*)".+?>~is', $string) > 0) { $string = preg_replace("~<img(.*)width=\"(.*)\"(.+?)>~is", "<img$1width=\"500\"$3>", $string); }else { $string = preg_replace("~<img(.+?)>~is", "<img $1 width=\"500\">", $string); } } echo htmlentities($string); ?> Tested for both (commented out the lower one to test to upper). It worked as expected. As far as if there is a better way to do this, no clue, probably since I am still new to regex. I am working on a better way to do it Quote Link to comment https://forums.phpfreaks.com/topic/147067-adding-or-change-img-width-value-dynamically/#findComment-772136 Share on other sites More sharing options...
scvinodkumar Posted February 26, 2009 Author Share Posted February 26, 2009 wow this is what i want many thanks for you effort. Quote Link to comment https://forums.phpfreaks.com/topic/147067-adding-or-change-img-width-value-dynamically/#findComment-772174 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.