image resize

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simon2x1
    New Member
    • Dec 2008
    • 123

    image resize

    i have an image which width is 213 and height is 200 when
    i echo the image and i resize it
    echo "<img src='company/$present' width='70' height='68'/>";
    the image was not as clear as when it was 213 * 200.how can i
    make the image clear after i have resize it to 70 * 200.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    When you resize it as you do, with the <img> height and width attributes, you are essentially leaving it up to the browser to resize it.
    Predictably, Internet Explorer does a horrible job at this, while the other major browsers do a pretty good job resizing them.

    If you want to ensure that the image is scaled smoothly, you can have PHP resize the image. The GD extension offers the imagecopyresamp led function, which does an excellent job resizing images. (See the documentation for examples on how to use it)

    Note that image processing is a very resource intensive task, so if you frequently display you images resized, you should save the resized image to the server's file-system and serve it from there, rather then resize it for every request.

    Comment

    • simon2x1
      New Member
      • Dec 2008
      • 123

      #3
      about image resize

      what if i want to echo the image from my database, which
      will involve vaious image how can i then resize it and
      also keep the resize image smooth like the original.i
      will appreciate it if the answer that will be given to me will
      be edited on my code

      Code:
      <?php
      $query = "SELECT * FROM photo";
      $result = mysql_query ($query) or die('query error');
      $count = 0;
      while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
      $image = $line[picname];
      echo "<img src='company/$image'/> ";
      $count++;
      }

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        if you want to echo the image from DB (that is, you have the image itself stored there (as binary data)) you need a script of its own that fetches the image, applies any changes you want (e.g. resizing) and gives out the result.

        Comment

        • tharden3
          Contributor
          • Jul 2008
          • 916

          #5
          This article about Uploading files into a MySQL database using PHP
          might help you. It discusses a great way to handle image files, although it might not directly answer your "resizing" question. Just throwing it out there as a reference.

          Thanks Atli ;)

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Resizing images 'on the fly' (whenever you pull them from a database, etc) will eat into your resources like a fat kid into some delicious cake. When a photo is uploaded, use the GD extension that Atli linked to, and then save a seperate copy of the resized image. That way, you're saving resources, but sacrificing a small amount of HDD space.

            Comment

            • simon2x1
              New Member
              • Dec 2008
              • 123

              #7
              Image not displaying

              i have a problem with the following code below, the image is not showing
              the image is display as if there is a missing link, i have check the code
              and i see not thing wrong with it and aslo i have enable the GD Library on
              my wamp server

              Code:
              <?php
              //my Image
              $imgSrc = '36.jpg'; 
              list($width, $height) = getimagesize($imgSrc); 
              //saving the image into memory (for manipulation with GD Library)
              $myImage = imagecreatefromjpeg($imgSrc); 
              if($width > $height) $biggestSide = $width; 
              else $biggestSide = $height; 
              $cropPercent = .5; 
              $cropWidth   = $biggestSide*$cropPercent; 
              $cropHeight  = $biggestSide*$cropPercent; 
              $c1 = array("x"=>($width-$cropWidth)/2, "y"=>($height-$cropHeight)/2);
              $thumbSize = 60; 
              $thumb = imagecreatetruecolor($thumbSize, $thumbSize); 
              imagecopyresampled($thumb, $myImage, 0, 0, $c1['x'], $c1['y'], $thumbSize, $thumbSize, $cropWidth, $cropHeight); 
              $lineWidth = 1;
              $margin    = 0;  
              $green    = imagecolorallocate($thumb, 193, 252, 182);
               
              for($i=0; $i<2; $i++){
              imagefilledrectangle($thumb, $margin, $margin, $margin+$lineWidth, $thumbSize-$margin, $green); 
              imagefilledrectangle($thumb, $thumbSize-$margin-$lineWidth, $margin, $thumbSize-$margin, $thumbSize-$margin, $green);
              imagefilledrectangle($thumb, $margin, $margin, $thumbSize-$margin-$lineWidth, $margin+$lineWidth, $green); 
              imagefilledrectangle($thumb, $margin, $thumbSize-$margin-$lineWidth, $thumbSize-$margin-$lineWidth, $thumbSize-$margin,$green);
              $margin+=4; 
              } 
              	header('Content-type: image/jpeg');
              	imagejpeg($thumb);
              	imagedestroy($thumb);
               
              ?>
              Last edited by Markus; Feb 8 '09, 04:32 PM. Reason: Added [code] tags.

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                Line 7, 8 should have {curly brackets}:
                Code:
                if($width > $height) {$biggestSide = $width;}
                else {$biggestSide = $height;}
                Other than that I can't see anything big... Will look again later if noone has replied.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by TheServant
                  Line 7, 8 should have {curly brackets}:
                  Code:
                  if($width > $height) {$biggestSide = $width;}
                  else {$biggestSide = $height;}
                  Other than that I can't see anything big... Will look again later if noone has replied.
                  Not true in this case; because the conditional statement only has one statement within it, curly brackets can be omitted.

                  Code:
                  // will work
                  if ( $a === $b )
                   echo "hi";
                  else
                   echo "yo";
                  
                  // won't work
                  if ( $a === $b )
                   echo "hello ";
                   echo "world";
                  else
                   echo "hi";

                  Comment

                  • TheServant
                    Recognized Expert Top Contributor
                    • Feb 2008
                    • 1168

                    #10
                    Interesting. I had a feeling that was the case, but wasn't 100% sure. Anyway, I use them all the time for consistency. Thanks for the tip. Suggestion withdrawn.

                    Comment

                    • simon2x1
                      New Member
                      • Dec 2008
                      • 123

                      #11
                      i have made the neccessary adjustment to line 7 and 8 of the code... but the image is still not displaying as told.

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        Originally posted by simon2x1
                        i have made the neccessary adjustment to line 7 and 8 of the code... but the image is still not displaying as told.
                        As I said above: it wouldn't make a difference.

                        Turn on PHP Debugging Messages

                        Comment

                        • simon2x1
                          New Member
                          • Dec 2008
                          • 123

                          #13
                          is there any thing else i can do to make the image display

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #14
                            Originally posted by simon2x1
                            is there any thing else i can do to make the image display
                            Have you turned on debugging messages? What does happen? What doesn't happen.

                            The more info you give us, the better.

                            Comment

                            • simon2x1
                              New Member
                              • Dec 2008
                              • 123

                              #15
                              Gd library and image resize prolblem

                              i have visited the link (imagecopyresam ple) you gave to me and i try almost
                              every code on the link non gave me an error but the image still did not display
                              like the code below is met to resize an image but the image did not display.
                              i have turn on my Gd library on my wamp server, the image i want to resize is
                              in the directory with the page so what is wrong and what should i do
                              Code:
                              <?php
                              // define image to resize
                              $file = "Aizen.jpg";
                              // create object from original image
                              $imOrig = imagecreatefromjpeg($file);
                              list($width, $height) = getimagesize($file);
                              $imNew = imagecreatetruecolor($width * 1.5, $height * 1.5);
                              // resample and resize old image
                              imagecopyresampled($imNew, $imOrig, 0, 0, 0, 0, $width * 1.5,
                              $height * 1.5, $width, $height);
                              // output resized image
                              header("Content-type: image/jpeg");
                              imagejpeg($imNew);
                              imagedestroy($im);
                              ?>

                              Comment

                              Working...