Reputation: 1826
I have a text editor on my web page, on that text editor i put image and than i use that image in different parts of my website, but in different places the image size differs.
The image in database is stored this way: <div><img width="210" height="150" alt="" src="/portali/userfiles/image/Kategorite/Teknologji/-1.jpg" /></div>
Now in one part of my web site i want to use this same image with different size and for this reason i need some php script to grab only the path of the image, in this sample (/portali/userfiles/image/Kategorite/Teknologji/-1.jpg).
Can some body write here some basic script to do this having in mind that the whole html code of the image is in an single variable.
Upvotes: 0
Views: 855
Reputation: 60403
$html = '<?xml version="1.0" encoding="UTF-8"?>'.$yourHTMLFromDb;
$elements = new SimpleXmlElement($html);
$url = $elements->img->attributes()->src;
Now im sure i wont be the first to say it but... "Youre doing it wrong". You should only be storing the path to the image in the database (and maybe some other attribnutes) not an html snippet.
Upvotes: 1
Reputation: 16817
I think your general approach may be wrong depending on what you are doing. Instead of storing the image string in your database just store the file name and possibly a path. Then in your HTML code build the string manually:
<img src='<?= $row['path'] . '/' . $row['image'] ?>' width='XXX' height='XXX'/>
That way you can add as many different parameters as you want.
Also to make it as efficient as possible don't resize your images using style tags or inline width/height, but instead use something like PHP GD or ImageMagick to actually make thumbnails in the sizes you need to display. In the long run this will save you a lot of headaches and bandwidth.
Upvotes: 2