0

I write this post because I have this problem:

I have a string with html characters.

What I want is to remove from this string, all images which are smaller than 10px width. How could I do this in PHP?

I've been thinking in the use of some loop but I don't know how to implement that. Can somebody help me?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
themazz
  • 1,107
  • 3
  • 10
  • 29
  • you should provide what the content of your string would look like and ask for a regex to do what you want. – Nelson Sep 27 '12 at 19:30
  • How would PHP have any understanding of the display size of an `img` element? [tag:php] is *server-side*, you may want to use JavaScript, but without context I don't know what to suggest. – David Thomas Sep 27 '12 at 19:31
  • This question is very similar to this: http://stackoverflow.com/questions/12609063/php-extracting-first-image-from-html-string-remove-all-images-tag-and-truncate/12609214#12609214 My answer listed several libraries to search for html elements. You could expand on that and get the dimension attributes to determine the.. dimensions – Flosculus Sep 27 '12 at 19:31
  • are you talking about – Toby Allen Sep 27 '12 at 19:33
  • This is a bit difficult to answer. Many times the dimensions of an image are not embedded within the img element. In this case, you'd have to open each image to determine its dimensions. – Tim Ferrell Sep 27 '12 at 19:35

2 Answers2

0

You can try

$final = array();
$images = array();
$template = "<img src='%d.jpg' height='%d' width='%d' />";


for($i = 0; $i < 10;  $i++)
{
    $height = mt_rand(9, 12);
    $weight = mt_rand(9, 12);
    $images[] = sprintf($template , $i,$height,$weight);

}


var_dump($images);


foreach($images as $image)
{
    $attribute = simplexml_load_string($image);
    $attribute = $attribute->attributes();

    if((int) $attribute['height'] <  10 or (int) $attribute['width'] < 10)
        continue ;
    $final[] = $image;
}

var_dump($final);

Output

array
  0 => string '<img src='0.jpg' height='11' width='9' />' (length=41)
  1 => string '<img src='1.jpg' height='12' width='9' />' (length=41)
  2 => string '<img src='2.jpg' height='10' width='11' />' (length=42)
  3 => string '<img src='3.jpg' height='12' width='11' />' (length=42)
  4 => string '<img src='4.jpg' height='11' width='9' />' (length=41)
  5 => string '<img src='5.jpg' height='9' width='11' />' (length=41)
  6 => string '<img src='6.jpg' height='9' width='9' />' (length=40)
  7 => string '<img src='7.jpg' height='10' width='9' />' (length=41)
  8 => string '<img src='8.jpg' height='12' width='9' />' (length=41)
  9 => string '<img src='9.jpg' height='10' width='10' />' (length=42)
array
  0 => string '<img src='2.jpg' height='10' width='11' />' (length=42)
  1 => string '<img src='3.jpg' height='12' width='11' />' (length=42)
  2 => string '<img src='9.jpg' height='10' width='10' />' (length=42)
Baba
  • 94,024
  • 28
  • 166
  • 217
  • this is not exactly what I want but it really helped me. In order to remove the images I don't want, I could use a preg_replace. Thank you! – themazz Sep 27 '12 at 20:51
-1

I would try http://php.net/manual/en/function.getimagesize.php

DaOgre
  • 2,080
  • 16
  • 25