image resizing in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isekhari
    New Member
    • Oct 2006
    • 4

    image resizing in php

    hi all

    In my program i have to upload some gif,jpg like images and store these images in a floder. when i am display these images by giving the width=100 and height=100 the image is not dispalying clearly.

    i.e. i want how i resize an image with the same clarity.


    thanks in advance.

    bye
  • vssp
    Contributor
    • Jul 2006
    • 268

    #2
    <img src="as.jpg" width=30 hight = 30 >

    if the set the width and hight then display immage automatically resize and dipalying well

    vssp

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      When you resize an image, you have to keep the ratio of width and height intact, otherwise the result gets distorted (stretched). So if you decrease the width by 30% you must also decrease the height by the same percentage.

      If you want to get the best shrink result, you'd have to use GD to do that dynamically. Second best is to set the sizes in the <img> statement.

      Following is a function I use to display images without actuallly resizing them via GD. It takes the desired height of the (shrunken) image, calculates the required width and displays it in an img tag. Call the function passing it the image file name, the required height, and the title which is displayed when you set the cursor on it. You can easily adapt it if you want the width to be the determining factor for shrinking. Have fun.
      [php]function showImage($img, $height, $title) {
      // --- determine width and height of the original image
      list($orig_widt h, $orig_height) = getimagesize($i mg);
      // --- calculate the compression ratio of thumbnail height and original height
      $thumb_ratio = $height / $orig_height;
      // --- re-calculate new thumbnail width by using the compression ratio
      $width = $orig_width * $thumb_ratio;
      // --- show the image
      echo "<img src='$img' width='$width' height='$height ' border='0' title='$title'> " ;
      }[/php]

      Ronald :cool:

      Comment

      • steven
        New Member
        • Sep 2006
        • 143

        #4
        There's actually some code to do this in the PHP manual (if you had read it)...

        You want to resize the image with resampling, so that it doesn't look distorted and also doesn't have as large a filesize and a larger image just displayed smaller through html width and height img attributes.

        http://fr.php.net/imagecopyresamp led

        Code:
        <?php
        // The file
        $filename = 'test.jpg';
        
        // Set a maximum height and width
        $width = 200;
        $height = 200;
        
        // Content type
        header('Content-type: image/jpeg');
        
        // Get new dimensions
        list($width_orig, $height_orig) = getimagesize($filename);
        
        $ratio_orig = $width_orig/$height_orig;
        
        if ($width/$height > $ratio_orig) {
           $width = $height*$ratio_orig;
        } else {
           $height = $width/$ratio_orig;
        }
        
        // Resample
        $image_p = imagecreatetruecolor($width, $height);
        $image = imagecreatefromjpeg($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
        
        // Output
        imagejpeg($image_p, null, 100);
        ?>
        The code is quite easy to follow. Oh and in future, please at least look at the manual before asking. It doesn't hurt to keep the online manual bookmarked and to occasionally have a look through it. If you see code you don't understand in the example on that page, simply pop the function name that you're not familiar with, into the search box at the top of the page and have a look.

        Good luck.

        Comment

        • exoskeleton
          New Member
          • Sep 2006
          • 104

          #5
          "Oh and in future, please at least look at the manual before asking", you shouldn't tell him/her this phrase pal, you may hurt his/her feelings pal...WATCH YOUR WORD..this is a community where questions are welcome...

          Originally posted by steven
          There's actually some code to do this in the PHP manual (if you had read it)...

          You want to resize the image with resampling, so that it doesn't look distorted and also doesn't have as large a filesize and a larger image just displayed smaller through html width and height img attributes.



          Code:
          <?php
          // The file
          $filename = 'test.jpg';
          
          // Set a maximum height and width
          $width = 200;
          $height = 200;
          
          // Content type
          header('Content-type: image/jpeg');
          
          // Get new dimensions
          list($width_orig, $height_orig) = getimagesize($filename);
          
          $ratio_orig = $width_orig/$height_orig;
          
          if ($width/$height > $ratio_orig) {
             $width = $height*$ratio_orig;
          } else {
             $height = $width/$ratio_orig;
          }
          
          // Resample
          $image_p = imagecreatetruecolor($width, $height);
          $image = imagecreatefromjpeg($filename);
          imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
          
          // Output
          imagejpeg($image_p, null, 100);
          ?>
          The code is quite easy to follow. Oh and in future, please at least look at the manual before asking. It doesn't hurt to keep the online manual bookmarked and to occasionally have a look through it. If you see code you don't understand in the example on that page, simply pop the function name that you're not familiar with, into the search box at the top of the page and have a look.

          Good luck.

          Comment

          Working...