Link to home
Start Free TrialLog in
Avatar of Claudia_Presto
Claudia_Presto

asked on

resize image using php and javascript

Dear Experts!

I have a static html page like this, a php remote image resizer like resizer.php

<html><body>

some stuf here
<div id="something">
some stuff
<img src="filename.jpg">
some stuff
<img src="filename2.jpg">
some stuff
</div>

</body></html>

I'd like to resize images bigger than 200px in width in a specific div ( like id=something ) using my php image resizer, and javascript should help here easily,

the result would be something like this: <img src="resizer.php?width=200&file=filename.jpg" />

A nice javascript may solve the problem, or anybody has any better idea to resize images in a specific div in a static html page?

Regards

PS. these static pages are generated by a Publishing Platform and template based,
the file extensions are .php and I can use other php codes in my page. but the img tags are from the publishing engine and can not be changed. (this may help either)
ASKER CERTIFIED SOLUTION
Avatar of flipz
flipz
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Claudia_Presto,

The above script from flipz should do the job for you, but to make it run a bit smoother, maybe you should add the follow logic:

if new width is less than current width + 5 pixels, then just stretch the image
else reload the new image

Obviously you need to do the same logic with the height, and possibly go even further, as to be honest the way you are looking at now, will reload the image for each pixel it is resized which could use a fair amount of bandwidth depending on the image size and how much the user resizes it, plus on top of bandwidth it could cause a bit of a strain on the browser.


Just a thought.
Avatar of exoska
exoska

and for some addition to flipz code , rendering childnodes collection may not be the best way, as images may be deeper levels on DOM..

id try something like this,

<script language="JavaScript">

var objDiv = document.getElementById("something").getElementsByTagName('IMG');


for (idx=0;idx<objDiv.length; idx++) {
    arrPaths = objDiv[idx].src.split("/");
    objDiv[idx].src = "resizer.php?width=200&file=" + arrPaths[arrPaths.length-1];
}
</script>
and cyber-drugs is also right.
so in the previous function, while rendering images. storing their src's and setting them to null(so they would stop loading), and just after the collection render, setting their src s to the php named ones that you d stored might be a better way to challange the bandwidth issue
Avatar of Claudia_Presto

ASKER

Thank you all,

to flipz or exoska,
as I do not know JavaScript a lot,
could you please add an if clause to your function in order to control the size?
for example if the width is less than 200px then do nothing else do the replacement.

Regards