Solved

resize image using php and javascript

Posted on 2007-03-20
5
262 Views
Last Modified: 2013-11-19
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)
0
Comment
Question by:Claudia_Presto
5 Comments
 
LVL 11

Accepted Solution

by:
flipz earned 500 total points
ID: 18757299
You may have to tweak this a little depending on if your files (code pages and images) are all in different directories - I can help if they are.

<div id="something">
some stuff
<img src="filename.jpg">
some stuff
<img src="filename2.jpg">
some stuff
</div>
<script language="JavaScript">

var objDiv = document.getElementById("something");

for (idx=0;idx<objDiv.childNodes.length; idx++) {
      if (objDiv.childNodes[idx].nodeName == "IMG") {
            arrPaths = objDiv.childNodes[idx].src.split("/");
            objDiv.childNodes[idx].src = "resizer.php?width=200&file=" + arrPaths[arrPaths.length-1];
      }
}
</script>
0
 
LVL 4

Expert Comment

by:Cyber-Drugs
ID: 18757611
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.
0
 
LVL 3

Expert Comment

by:exoska
ID: 18762961
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>
0
 
LVL 3

Expert Comment

by:exoska
ID: 18762977
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
0
 

Author Comment

by:Claudia_Presto
ID: 18791728
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
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to combine and minify CSS and JS files 5 17
jQuery on Submit 4 41
Inserting data into database 10 35
message Alert on an empty search 10 20
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question