By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

How do I resize an image to a given window size?

P: n/a
I am writing an image viewer. I want to be able to resize the image so
that it always fits within a window of given height and width.

I've tried a couple of these from the web, but the don't seem to take
into account both the image dimensions AND the window dimensions.

Are there any algorithms out there? I'm not keen on reinventing the
wheel here.

Jan 26 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Robert S wrote:
I am writing an image viewer. I want to be able to resize the image so
that it always fits within a window of given height and width.

I've tried a couple of these from the web, but the don't seem to take
into account both the image dimensions AND the window dimensions.

Are there any algorithms out there? I'm not keen on reinventing the
wheel here.
It can't be done in PHP. PHP is server-side only, and knows nothing
about the window dimensions. You would have to use a client-side
scripting language such as javascript to send the information to the
server, then have a program resize based on the javascript input.

But you'll still have problems if the user resizes the window while your
page is displayed.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 26 '07 #2

P: n/a
AFAIK, screen dimension can only be accessed with browser-side language
such as JavaScript. Since PHP has already ended to work when browser
displays the page, I can suggest using JavaScript to re-open the page
again, passing screen/window dimensions as parameters.

Jan 26 '07 #3

P: n/a
Someone wrote: "it can't be done with php alone....you'll need
javascript too"
........and then "you'll still have trouble if they resize the screen
after sending the html"

=====================
This got me thinking. I've been working with Google's new
gwt libraries recently. This is really cool stuff. They use javascript
and ajax
like messaging to talk to server, with a system modeled after JavaRMI.
They take care of the cross-browser-javascript nightmare for you.
All of a sudden web applications are empowered with drag and drop,
dynamic resizing, anything you want. Microsft beware. Office suites
can now be web based applications....and you'll be able to work on
your office stuff from an internet cafe in Mongolia, if you want.

To make this stuff work you write the code in java, and their compiler
turns (the required client side
portions) into javascript for you. The PHP community should jump on
this and make a similar system,
else be left behind.

Jan 26 '07 #4

P: n/a

To make this stuff work you write the code in java, and their compiler
turns (the required client side
portions) into javascript for you.
Sorry - I didn't make myself clear on my original post. I've managed
to do the javascript code that calculates the size of the window and
sends it back to the server. I want the PHP code that adjusts the
image to the dimensions supplied by javascript.

Jan 26 '07 #5

P: n/a
Rik
Robert S <ro**********************@gmail.comwrote:
>To make this stuff work you write the code in java, and their compiler
turns (the required client side
portions) into javascript for you.

Sorry - I didn't make myself clear on my original post. I've managed
to do the javascript code that calculates the size of the window and
sends it back to the server. I want the PHP code that adjusts the
image to the dimensions supplied by javascript.
By supplying PHP with the height & width, preferably as GET variables.
Then you can use the function imagecopyresampled to scale the image as
needed. See the examples at http://nl2.php.net/imagecopyresampled.

Just set the src of the image to be the php script, imagejpg/-png/-gif
will send the resized image back to the browser as long as you don't
specify a filename.
--
Rik Wasmus
Jan 26 '07 #6

P: n/a
Then you can use the function imagecopyresampled to scale the image as
needed. See the examples athttp://nl2.php.net/imagecopyresampled.
Thanks. Got some good ideas. This this is what I want:
http://au.php.net/manual/en/function...pled.php#57990

Jan 26 '07 #7

P: n/a
Robert S wrote:
>Then you can use the function imagecopyresampled to scale the image as
needed. See the examples athttp://nl2.php.net/imagecopyresampled.

Thanks. Got some good ideas. This this is what I want:
http://au.php.net/manual/en/function...pled.php#57990

You will find that after resizing (resampling) photos you'll lose some
image quality, but you can fix that by using an Unsharp Mask. Here's one
written in Php: http://vikjavev.no/computing/ump.php

ImageMagick also has a good unsharp mask.

--
*****************************
Chuck Anderson Boulder, CO
http://www.CycleTourist.com
*****************************
Jan 27 '07 #8

P: n/a
image quality, but you can fix that by using an Unsharp Mask. Here's one
written in Php:http://vikjavev.no/computing/ump.php

ImageMagick also has a good unsharp mask.
Thanks. I've actually used the unsharp mask in ImageMagick for my
thumbnails and I've used the PHP one on my script. I think its OK for
thumbnails but its pretty slow for a full-sized image.

Jan 27 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.