Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

youlichika

macrumors member
Original poster
Aug 27, 2010
35
0
I would like to adjust some pictures size. I used this code:

<?php
preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is', $s, $matches);
$firstimg = $matches[0][0];
$newimg = preg_replace('/width=\"\d+\" height=\"\d+\"/', 'width="300" height="200"', $firstimg);
echo $newimg;
?>

But it can not show all the pictures, when the picture rule is <img src="" alt="" title="" /> There is no width and height, it can not work,
how to add a width and height to these kind of pictures?
Thanks.
 
I believe you're asking how to handle the case when the img does not include the height and width attributes. If so, this will help.

PHP:
preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is', $s, $matches);
$firstimg = $matches[0][0];
// Remove width / height
$newimg = preg_replace('/width=\"\d+\" height=\"\d+\"/', '', $firstimg);
// Add new height width
$newimg = preg_replace('/\/>/', 'width="300" height="200" \/>', $newimg);
echo $newimg;
 
Thanks angelwatt, I am a newer for PHP regular. This is what I mean: how to handle the case when the img does not include the height and width attributes.

I have tryed your code, but it not worked, can you check it for me?
I know you first cancel all the 'width' and 'height' attributes, then add the new for them. This is a clever way.

Or, I have another method.(maybe it can) Can anyone help me to finish a "if and else"

if code
Code:
<?php
preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is', $s, $matches);
$firstimg = $matches[0][0];
$newimg = preg_replace('/width=\"\d+\" height=\"\d+\"/', 'width="300" height="200"', $firstimg);
echo $newimg;
?>

else code
Code:
preg_match_all('#<img[^>]*>#i', $s, $matches)
$test2=$matches[0][0];
$test= array('<img'=>'<img width="300px" height="200px"');
$newimg= strtr($test2,$test);}

Waiting for the two method, thanks again.
 
Well you don't define $s so it's hard to say why it's not working for you, but it worked fine for me. Here's my complete test file.

PHP:
<pre>
<?php
$s = '<img src="image.jpg" width="100" height="100" alt="" />';
preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is', $s, $matches);
$firstimg = $matches[0][0];
$newimg = preg_replace('/width=\"\d+\" height=\"\d+\"/', '', $firstimg);
echo htmlentities($newimg), "\n";
$newimg = preg_replace('/\/>/', 'width="300" height="200" \/>', $newimg);
echo htmlentities($newimg);
?>
</pre>
 
Thanks, angelwatt. But when I use the img as below, it can not work.

Code:
<pre> 
<?php 
$s = '<img hspace="4" vspace="4" border="1" alt="Nagoya Institute folding X-Frame car lacks S-foils, hyperdrive, rolls on a big orange ball" src="http://www.blogcdn.com/www.engadget.com/media/2010/09/x-frame-2010-09-02-600.jpg" />'; 
preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is', $s, $matches); 
$firstimg = $matches[0][0]; 
$newimg = preg_replace('/width=\"\d+\" height=\"\d+\"/', '', $firstimg); 
echo htmlentities($newimg), "\n"; 
$newimg = preg_replace('/\/>/', 'width="300" height="200" \/>', $newimg); 
echo htmlentities($newimg); 
?> 
</pre>
 
You need some kind of database, or array, to store the height of each image plus the URL.

Again you're not paying attention to what the thread is about. No databases needed here.

youlichika said:
Thanks, angelwatt. But when I use the img as below, it can not work.
We just need to change the match to simply catch all images, then process them. I also added a for loop to allow processing multiple matches.
PHP:
<pre>
<?php
$s = '<img hspace="4" vspace="4" border="1" alt="Nagoya Institute folding X-Frame car lacks S-foils, hyperdrive, rolls on a big orange ball" src="http://www.blogcdn.com/www.engadget.com/media/2010/09/x-frame-2010-09-02-600.jpg" />
<img src="image.jpg" width="100" height="100" alt="" />'; 
preg_match_all('/<img[^>]*?\/>/is', $s, $matches);
$firstimg = $matches[0][0];

for ($a=0; $a < count($matches[0]); $a++)
{
	$newimg = preg_replace('/ width="\d+" height="\d+"/', '', $matches[0][$a]);
	echo htmlentities(preg_replace('/\/>/', 'width="300" height="200" />', $newimg)), "\n";
}
?>
</pre>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.