Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to proportionally reduce image in IMG SRC

Status
Not open for further replies.

jwhite68

IS-IT--Management
Jun 14, 2007
77
BG
I have the following code:
Code:
  echo '<A HREF="'.$domain.'register.php" ><IMG SRC="image/promo.gif" style="width:50%; height:50%;" border="1" ALT="Click here to register"></A>';

I was expecting it to reduce the image promo.gif proportionately by 50%. Instead it appears as a single horizontal strip. Can anyone tell me where I am going wrong?
 
It's never a good idea to use % for image sizes. I would suggest you calculate the width and height you want, and output them as px values. You can do that server-side.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
I believe that using percentages, resizes the image relative to the containing element. Which means its not 50% of the image, but 50% of whatever its in.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The only time I ever scale an image down in HTML is when I want to increase the resolution for the purpose of printing (such as a logo on a printable form). Often, the penalty for this is that the scaled image looks funny as different browsers display scaled images with varying quality.

In most any other situation, it is wasteful to be sending image date that is not going to be viewed by the average user. Consider scaling your image file before resorting to such manipulation in HTML.
 
You should use a program like Photoshop to make a thumbnail for your purpose. Depending on the size of the original Pic it may take a while for IE to scale it for you and it will look weird like Jimoblak said.
 
Since you use PHP, its easy to resize images...


You can do it as easy as:
Code:
If (is_file($img)) {
  If (is_file("tn_{$img}")) {
    echo showthumb($img); // make function for showing thumb
    } // end: If (is_file("tn_{$img}")) {
  Else {
    makethumb($img, "tn_{$img}"); // You have to make this function too!
    echo showthumb($img);
    } // end : else
  } // end : If (is_file($img)) {

As far as the functions I call in this psuedocode, you have to make them..

The showthumb will only make some html and return it.
makethumb will generate the thumbnail.

You could make the makethumb return true or false, if it fails or does not fail and then you could improve the script like so:

Code:
if (makethumb($img, "tn_{$img}")) {
  echo showthumb($img);
  }
else {
  flagimage($img, 404);
  echo "<!-- image {$img} flagged as useless -->";
  }

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top