Jump to content

[SOLVED] function within preg_replace


moshdj2uk

Recommended Posts

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

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]
[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.
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]
[tt]Parse error: syntax error, unexpected '<' in D:\Web Stuff\pixelduel\v0.1\includes\common.inc.php(647) : regexp code on line 1

Fatal 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...

Strange. I have tested and it worked fine for me.

This is what I did when I tested it:
[code]<?php

function 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.