Jump to content

[SOLVED] Dynamic image resize, width & height


Alexhoward

Recommended Posts

Hello guys,

 

I've got this script from the PHP manual:

 

<?php

while($row = mysql_fetch_array($sub_results)) { 
    $title = $row['title']; 
    $image1 = $row['image1'];
    $fdesc = SUBSTR($row['fdesc'],0,400);
    $user = $row['username'];
    $ref = $row['ref'];


     $image =  "$image1";                
     $size = getimagesize("$image");
       $height = $size[1];
       $width = $size[0];
     if ($height > 100)
          {
               $height = 100;
               $percent = ($size[1] / $height);
               $width = ($size[0] / $percent);
          }
     else if ($width > 100)
          {
               $width = 100;
               $percent = ($size[0] / $width);
               $height = ($size[1] / $percent);
          }
     //echo "<img src\"image/path/image.jpg\" height=\"$height\" width=\"$width\" />";

?>

 

The top part is the end of my select query...

 

Problem is it's not working as i hoped.

 

What i'd like to do is fit whatever sized image into a 100 X 100 cell,

 

so i just assumed take whatever length is bigger and make it 100, then the other dimension would just scale proportionally.

 

However this is not what it's doing.

 

Could any of you suggest how i change this...?

 

Thanks in advance!

Link to comment
Share on other sites

Sussed!

 

This'll do the job...

 

<?php

while($row = mysql_fetch_array($sub_results)) { 
    $title = $row['title']; 
    $image1 = $row['image1'];
    $fdesc = SUBSTR($row['fdesc'],0,400);
    $user = $row['username'];
    $ref = $row['ref'];


     $image =  "$image1";                
     $size = getimagesize("$image");
       $height = $size[1];
       $width = $size[0];
     if ($height > $width)
          {
               $height = 100;
               $percent = ($size[1] / $height);
               $width = ($size[0] / $percent);
          }
     else if ($width > $height)
          {
               $width = 100;
               $percent = ($size[0] / $width);
               $height = ($size[1] / $percent);
          }
     //echo "<img src\"image/path/image.jpg\" height=\"$height\" width=\"$width\" />";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.