maxudaskin Posted March 31, 2008 Share Posted March 31, 2008 Ok, before you think, oh wow, this guy doesn't know anything, I do know how to do it, but, I am stuck on a method for a code I made. <?php /* FORUM ARRAYS */ $find = array("[img]", "[/img]"); $replace = array("<img src=\"","\" />"); ?> <?php echo str_replace($find,$replace,nl2br($textsql['text'])); ?> What I want it to do is after it changes [ img ] and [ /img ] to <img src=\" and \" /> for it to check the image size, probably with <?php $getimg = getimagesize("somepic.jpg"); list($width, $height, $type, $size) = $getimg; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/ Share on other sites More sharing options...
maxudaskin Posted March 31, 2008 Author Share Posted March 31, 2008 Any Idea's what so ever? Even a partial idea will help. Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-505228 Share on other sites More sharing options...
stopblackholes Posted March 31, 2008 Share Posted March 31, 2008 $getimg = getimagesize("somepic.jpg"); $width = $size[0]; $height = $size[1]; echo 'width: '.$width.'<br />'; echo 'height: '.$height.'<br />'; getimagesize puts the data in a array, Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-505249 Share on other sites More sharing options...
maxudaskin Posted March 31, 2008 Author Share Posted March 31, 2008 But how can I isolate the URL? Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-505250 Share on other sites More sharing options...
stopblackholes Posted March 31, 2008 Share Posted March 31, 2008 What Are You Trying to Do? Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-505261 Share on other sites More sharing options...
maxudaskin Posted March 31, 2008 Author Share Posted March 31, 2008 Given: $text = "[ img ]http://www.url.com/image.jpg[ /img ]" Wanted: $var = "http://www.url.com/image.jpg"; I want the url Isolated.. but the url is dynamic. Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-505270 Share on other sites More sharing options...
maxudaskin Posted March 31, 2008 Author Share Posted March 31, 2008 Is it possible to isolate the URL? Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-505284 Share on other sites More sharing options...
stopblackholes Posted March 31, 2008 Share Posted March 31, 2008 just use the str_replace function to remove all the junk you dont want or a ereg, preg_match . <?php $text = "[img=http://www.url.com/image.jpg]" $find = array("[img=", "]"); $replace = ""; $new = str_replace($find, $replace, $text ); echo $new; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-505287 Share on other sites More sharing options...
maxudaskin Posted March 31, 2008 Author Share Posted March 31, 2008 But the text will also contain other stuff... Here is a real example... GLAD to see we have our new forum! Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-505290 Share on other sites More sharing options...
stopblackholes Posted March 31, 2008 Share Posted March 31, 2008 haha well you had me confused title of thread said resize image. well there are many different ways to do it i suppose but heres a way with preg_match_all <?php $text = "GLAD to see we have our new forum! [img=http://www.url.com/image.jpg] GLAD to see we have our new forum! "; preg_match_all('/\[img\](.*?)\[\/img\]/', $text, $get, PREG_PATTERN_ORDER); //complete tag and url echo $get[0][0]; echo "<br><br>"; //url only echo $get[1][0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-505329 Share on other sites More sharing options...
maxudaskin Posted March 31, 2008 Author Share Posted March 31, 2008 What will happen if there are two images? Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-506024 Share on other sites More sharing options...
discomatt Posted March 31, 2008 Share Posted March 31, 2008 Why don't you test it? You could always use preg_replace_callback like so <?php // This is a very loose regex for example purposes only. // For production use, it should be santizied and cleaned // unless the source of input is from a trusted source $regex = '%\[img\]([^\[]++)\[/img\]%i'; $subject = 'This is some text and this: [img=img.jpg] will be converted to proper code'; $result = preg_replace_callback( $regex, create_function('$matches', ' if (!$img = getimagesize($matches[1]) ) return FALSE; if (!$img[0] || !$img[1]) return FALSE; return "<img src=\"$matches[1]\" width=\"$img[0]\" height=\"$img[1]\" />"; '), $subject ); echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-506043 Share on other sites More sharing options...
discomatt Posted March 31, 2008 Share Posted March 31, 2008 In hindsight, this function is fairly safe. If the content within the [ img ] and [ /img ] isnt an image file, it returns nothing. I would still sanitize user input a little better in the regex to be safe, though. Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-506044 Share on other sites More sharing options...
maxudaskin Posted April 1, 2008 Author Share Posted April 1, 2008 I got an error with your code... Warning: getimagesize(images/TOPLOGO.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home/.grable/vzoom/virtualzoom.net/Untitled-1.php(19) : runtime-created function on line 2 This is some text and this: will be converted to proper code Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-506109 Share on other sites More sharing options...
discomatt Posted April 1, 2008 Share Posted April 1, 2008 Change if (!$img = getimagesize($matches[1]) ) to if (!$img = @getimagesize($matches[1]) ) if you want to suppress errors. Please use my code only as a guideline and not a drop-in replacement. Quote Link to comment https://forums.phpfreaks.com/topic/98729-resize-image/#findComment-506145 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.