Jump to content

Resizing Images inside [img=] if they exceed a certain width/height dimension


Recommended Posts

Hello! I have a text-formatting function which looks for any [img=] tag and parses them into images -- which works exactly as it should. But when I try add in some functionality to resize the image if it exceeds a certain width and height, I get an error -- the error being:

Warning: getimagesize($1) [function.getimagesize]: failed to open stream: No such file or directory...

I know why it's getting this error. This error is occurring because the function getimagesize is taking the $1 literally. Is there anyway to get around this?

 

<?php
function format($str) {
$str = trim($str);
$str = stripslashes($str);
$str = htmlentities($str, ENT_COMPAT, 'UTF-8');
$str = nl2br($str);
$formats = array(
		'/\[img=(.*?)\]/si' => ((current(getimagesize('$1')) > $_CL['img']['maxwidth']) && (next(getimagesize('$1')) > $_CL['img']['maxheight'])) ? '<a href="$1" class="image"><img src="$1" class="image"></a>' : '<a href="$1"><img src="$1" class="image"></a>',//'<img src="$1" class="image">',
		);
$str = preg_replace(array_keys($formats), $formats, $str);
return $str;
}
?>

php is trying to execute getimagesize at the time that you are assigning the value to the array element, not when you are actually performing the preg_replace.  Outside of preg_replace, $1 means nothing but a literal $1 so getimagesize is trying to find a file literally called '$1'

 

You are going to have to reorganize your code.  One way to do it would be to wrap the entire array element value in quotes (whole thing, function calls and all, so none of that stuff gets executed; php treats it as just some random string for now), and use the e modifier for preg_replace, which will cause php to parse the replacement argument in preg_replace as if you were running eval.  Doing it this way will have that whole value "...getimagesize('$1')..." evaluated as php code, but since it is actually evaluated inside the preg_replace call, $1 will actually mean something. 

 

The danger to doing this though is if your preg_replace fails to make a match and capture, or if it captures something unexpected, or if the image just doesn't exist, you're going to get the same error thrown up at you, and there's nothing you can do to safeguard against that.

 

In light of that concern, another, better way to do it would be to first preg_match for the pattern and then use the returned value from that, instead of $1.  Somewhere in-between your preg_match and getimagesize, you can check to see if the image actually exists, before you try to getimagesize it.

Thank you for pointing me in the right direction Crayon Violent! I have it working just as it should now with this:

<?php
if(preg_match_all('/\[img=(.*?)\]/si', $str, $match))
	foreach($match[1] as $url)
		if($img = @getimagesize($url)) $str = str_replace('[img='.$url.']', (($img[0] > $_CL['img']['maxwidth']) && ($img[1] > $_CL['img']['maxheight'])) ? '<a href="'.$url.'" class="image"><img src="'.$url.'" class="image"></a>' : '<a href="'.$url.'"><img src="'.$url.'" class="image"></a>', $str);
?>

 

With that preg_match_all, I get a two-dimensional array with the first dimension holding all the matches with the text surrounding the subpattern (<URL>) and the second dimension simply holds the matched URL.

 

I notice that when loading a page while using this code-snippet, the loading-process seems to hang for about 1 or 2 seconds. Is any way to lessen that hang-time? The load-time isn't occurring because of Error-Suppressor prepended to getimagesize, is it? Or does this hang-time occur because of something outside of my control (such as the remote-server's response time)?

You know, I suspected preg_replace_callback would be the answer. I will attempt to get it working with preg_replace_callback. My script doesn't hang (when loading) anymore (I made a few changes from the code I posted above) but the way I'm doing it now just seems very poor so I'm going to attempt to get this working with preg_replace_callback instead.

 

Thank you, Crayon Violent and Daniel0!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.