tags:

views:

133

answers:

3

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.

+2  A: 

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.

angryCodeMonkey
i know this is the best approach but my situation is like i described on my question, thanks fo your effort
AXheladini
+1  A: 
$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.

prodigitalson
i get this error from your code: Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Entity 'euml' not defined in /home6/feksinte/public_html/portali/index.php on line 189
AXheladini
Sorry encoding should be `"UTF-8"` updated my post.. however the sample html you provided shows only an image within a div where is this entity coming from if this entity is not in the src for the image then you must have other tags in there you didnt include which may change how you reach the element in the dom. the code i posted will only work for the structure you provided in your sample.
prodigitalson
again the same error. i have and admin panel where i insert data and for this reason i use an ready made rich text editor (java script based). And when i upload an image using that text editor it stores this image exactly the way i showed you up there. I don't know what i am doing wrong but i still have the same problem
AXheladini
how to print(echo) this in PHP
AXheladini
A: 

I think searching and writing codes is more hard than just clicks. Use Automation Anywhere cuz it recognizes the image present on the web page and gives you option whatever you want to do like extract image, extract URL of image, etc. So then you can select URL (as in your case) and your job's done. Get a Free Trial, i think it will surely help you.

Enjoy!! -Techsmart

Techsmart