drisate Posted December 16, 2008 Share Posted December 16, 2008 hey guys i need a code that finds every images inside a posted message and that checks if the size is bigger then 430 px if it is put 430. I guess we would need to search for a paterne then extract the info and replace if needed ... There can be only 2 paternes 1. $message='blablablabla <img src="http://..." border"0" alt="" />blablablabla blablablabla'; and 2. $message='blablablabla <img src="http://..." border"0" alt="" width="">blablablabla blablablabla'; For paterne 1 we need to get the image size from the image it self using the image adress then add a width inside the string. For paterne 2 we need to check the width and change the value if needed I have abselutly no idea how to do that ... Quote Link to comment https://forums.phpfreaks.com/topic/137227-check-image-size-in-string/ Share on other sites More sharing options...
.josh Posted December 16, 2008 Share Posted December 16, 2008 is the user entering in the image tag info, like the width? Quote Link to comment https://forums.phpfreaks.com/topic/137227-check-image-size-in-string/#findComment-716921 Share on other sites More sharing options...
.josh Posted December 16, 2008 Share Posted December 16, 2008 $message='blablablabla <img src="somepic.jpg" border"0" alt="" />blablablabla blablablabla'; preg_match('/(<img.*?src="(.*?)".*?>)/',$message,$match); $picinfo = getimagesize($match[2]); $picinfo[0] = ($picinfo[0] > 430)? 430 : $picinfo[0]; $replacement = 'img src="'.$match[2].'" width="'.$picinfo[0].'" height="'.$picinfo[1].'" border="0" alt="" /'; $message = preg_replace($match[1], $replacement, $message); This code will pull the image tag from the message, find out the width and height of the image, and create a new image tag with width no more than 430. But width for the replacement image tag is based off of the getimagesize of the pic, not some width in the original $message string. Code is going to have to be changed up a bit if the width in the original $message has to be preserved (like, it's based off user input or something). Like for instance, user put in 100 as width. You want max of 430, but getimagesize says it's 200. That script will set it to 200, not 100. You didn't specify whether you wanted it to be based off original image or actual pic size (hence the question in the prev post). Also, code works for either of those formats. Quote Link to comment https://forums.phpfreaks.com/topic/137227-check-image-size-in-string/#findComment-716973 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.