Solved

resize image using php and javascript

Posted on 2007-03-20
5
257 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now