moshdj2uk Posted December 17, 2006 Share Posted December 17, 2006 I'm currently building a forum and at the moment, working on BB code.When someone posts the [tt][img src="image.jpg"][/tt] tag, it calls the fncScaleImage function, which first checks the images properties, then scales the image should it be over a specific height or width. However, I can't seem to get this working properly. I'm severely lacking in sleep, so chances are, i'm missing something stupid, or there's a quick workaround.[code]$str = preg_replace('/\[img src="(.*?)"\]/si',fncScaleImage('\\1'), $str);[/code][code]function fncScaleImage($image){ $size = getimagesize($image); $height = $size[1]; $width = $size[0]; if ($height > 400) { $height = 400; $percent = ($size[1] / $height); $width = ($size[0] / $percent); @$msg = '<font class="detail">This image has been scaled down. Click <a class="detail" href="'.$image.'" target="_blank">here</a> for full size.</font>'; } else if ($width > 400) { $width = 400; $percent = ($size[0] / $width); $height = ($size[1] / $percent); @$msg = '<font class="detail">This image has been scaled down. Click <a class="detail" href="'.$image.'" target="_blank">here</a> for full size.<br></font>'; } return('<img src="'.$image.'" height="'.$height.'" width="'.$width.'"><br>'.@$msg.'<br>');}[/code]Any suggestions? Link to comment https://forums.phpfreaks.com/topic/30987-solved-function-within-preg_replace/ Share on other sites More sharing options...
Psycho Posted December 17, 2006 Share Posted December 17, 2006 You don't state what the problem is. Is it not scaling the iamge? Is the scaling incorrectly?One problem I see is the use of "if" and "else if". You are assuming that if the image's height is over 400 that once it is scaled down it will also have a width less than 400, but that may not be the case.Here is some code I just converted from javascript. It's not tested, so there may be some minor errors, but the logic is sound:[code]<?php{$size = getimagesize($image);$width = $size[0];$height = $size[1];$maxwidth = 400;$maxheight = 400;if ($width>$maxwidth || $height>$maxheight) { $imgScale = ($width / $height); if ( ($width/$maxwidth) > ($height/$maxheight) ) { //Need to scale on width $width = maxWidth; $height = round($maxwidth / $imgScale); } else { //Need to scale on height $width = round($maxheight * $imgScale); $height = maxHeight; } @$msg = '<font class="detail">This image has been scaled down. Click <a class="detail" href="'.$image.'" target="_blank">here</a> for full size.<br></font>';}return('<img src="'.$image.'" height="'.$height.'" width="'.$width.'"><br>'.@$msg.'<br>');}?>[/code] Link to comment https://forums.phpfreaks.com/topic/30987-solved-function-within-preg_replace/#findComment-143018 Share on other sites More sharing options...
moshdj2uk Posted December 17, 2006 Author Share Posted December 17, 2006 [tt]Warning: getimagesize(\1) [function.getimagesize]: failed to open stream: No such file or directory in D:\Web Stuff\pixelduel\v0.1\includes\common.inc.php on line 746[/tt]It appears to be getting hooked on the \\1 part of the preg_replace? If i simply use \\1 instead of fncScaleImage('\\1'), everything goes fine, apart from the function to scale the image doesn't get run. I'm not clued up when it comes to the preg_replace function. Link to comment https://forums.phpfreaks.com/topic/30987-solved-function-within-preg_replace/#findComment-143023 Share on other sites More sharing options...
wildteen88 Posted December 17, 2006 Share Posted December 17, 2006 If you want to run functions from preg_replace you'll want to add the e modifier to the expression aswell as wrap the function in quotes:That should now call the fncScaleImage function.[code]$str = preg_replace('/\[img src="(.*?)"\]/sie', "fncScaleImage('\\1')", $str);[/code] Link to comment https://forums.phpfreaks.com/topic/30987-solved-function-within-preg_replace/#findComment-143050 Share on other sites More sharing options...
moshdj2uk Posted December 17, 2006 Author Share Posted December 17, 2006 [tt]Parse error: syntax error, unexpected '<' in D:\Web Stuff\pixelduel\v0.1\includes\common.inc.php(647) : regexp code on line 1Fatal error: preg_replace() [function.preg-replace]: Failed evaluating code: <img src="http://local.pixelduel.com/images/poster.png" width="" height=""> in D:\Web Stuff\pixelduel\v0.1\includes\common.inc.php on line 647[/tt]Getting there... Link to comment https://forums.phpfreaks.com/topic/30987-solved-function-within-preg_replace/#findComment-143057 Share on other sites More sharing options...
wildteen88 Posted December 17, 2006 Share Posted December 17, 2006 Strange. I have tested and it worked fine for me.This is what I did when I tested it:[code]<?phpfunction fncScaleImage($image){ $size = getimagesize($image); $width = $size[0]; $height = $size[1]; $maxwidth = 200; $maxheight = 200; if ($width>$maxwidth || $height>$maxheight) { $imgScale = ($width / $height); if ( ($width/$maxwidth) > ($height/$maxheight) ) { //Need to scale on width $width = $maxwidth; $height = round($maxwidth / $imgScale); } else { //Need to scale on height $width = round($maxheight * $imgScale); $height = $maxheight; } @$msg = '<font class="detail">This image has been scaled down. Click <a class="detail" href="'.$image.'" target="_blank">here</a> for full size.<br></font>'; } return '<img src="' . $image . '" height="' . $height . '" width="' . $width . '"><br>' . @$msg . '<br>';}$str = "[img src=\"myimage.gif\"]";$str = preg_replace('/\[img src="(.*?)"\]/sie', "fncScaleImage('\\1')", $str);echo $str;?>[/code]There must be an error in your code somewhere which causes this. Link to comment https://forums.phpfreaks.com/topic/30987-solved-function-within-preg_replace/#findComment-143059 Share on other sites More sharing options...
moshdj2uk Posted December 17, 2006 Author Share Posted December 17, 2006 Tweaked it a little. Works a treat now. Many, MANY thanks for the help! Link to comment https://forums.phpfreaks.com/topic/30987-solved-function-within-preg_replace/#findComment-143069 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.