Thumbnails - Halving width but doubling height?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jeigh
    New Member
    • Jul 2007
    • 73

    Thumbnails - Halving width but doubling height?

    Hello, I've been searching for a tutorial to create thumbnails for a while and finally found one that works for me. It's doing everything it's suppose to except one thing.

    I created a test image 600px wide, 700px high, so to get 300px width it would need to half that, as well as the 700px height. It's resizing the width to half the size (300px) except instead of halfing the height to 350px, it doubles it to 1400px, which obviously causes the image to come out very wrong.

    Here's the code I'm using:

    [code=php]
    function createThumbnail ($imageDirector y, $imageName, $thumbDirectory , $thumbWidth)
    {
    $srcImg = imagecreatefrom jpeg("$imageDir ectory/$imageName");
    $origWidth = imagesx($srcImg );
    $origHeight = imagesy($srcImg );

    $ratio = $origWidth / $thumbWidth;
    $thumbHeight = $origHeight * $ratio;

    $thumbImg = imagecreate($th umbWidth, $thumbHeight);
    imagecopyresize d($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($thumbI mg), imagesy($thumbI mg));

    imagejpeg($thum bImg, "$thumbDirector y/$imageName");
    }

    createThumbnail ("uploads/examples", "$newname", "uploads/examples/thumbnails", 300);
    [/code]

    Thanks,
    Jeigh.
  • jx2
    New Member
    • Feb 2007
    • 228

    #2
    change line 7
    [php]
    ratio = $thumbWidth / $origWidth ;
    [/php]

    regards
    jx2

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      At line 8, you should divide, not multiply.

      Comment

      • jx2
        New Member
        • Feb 2007
        • 228

        #4
        Originally posted by Atli
        Hi.

        At line 8, you should divide, not multiply.
        LOL i think you're confusing him
        yeah
        it can be your way

        regards
        jx2

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by jx2
          LOL i think you're confusing him
          yeah
          it can be your way

          regards
          jx2
          Well, there are two ways I would consider using for this.

          The first one. I've used a number of times.
          [code=php]
          $ratio = $originalHeight / $originalWidth;
          $thumbHeight = $thumbWidth * $ratio;
          [/code]

          The second one. A little limited but works in this case.
          [code=php]
          $ratio = $originalWidth / $thumbWidth;
          $thumbHeight = $originalHeight / $ratio;
          [/code]

          I'ts like Jeigh was using the second one but mixed up the second line. Has probbly happend to all of us at some point.

          Comment

          • Jeigh
            New Member
            • Jul 2007
            • 73

            #6
            Thanks for that guys, it worked but it seems to crop the image instead of resizing it, am I using the wrong sort of script or can this be modified to do that?

            Comment

            • jx2
              New Member
              • Feb 2007
              • 228

              #7
              Originally posted by Jeigh
              Thanks for that guys, it worked but it seems to crop the image instead of resizing it, am I using the wrong sort of script or can this be modified to do that?
              look here

              you need two more parameters in line 11 for sorce height and width

              regards
              jx2

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Originally posted by jx2
                look here

                you need two more parameters in line 11 for sorce height and width

                regards
                jx2
                This one is pretty much the same imagecopyresamp led(), except this one not only resizes the image, it also resamples it, so it gives you better quality.

                Comment

                • jx2
                  New Member
                  • Feb 2007
                  • 228

                  #9
                  Originally posted by Atli
                  This one is pretty much the same imagecopyresamp led(), except this one not only resizes the image, it also resamples it, so it gives you better quality.
                  thx Atli
                  nice job i didnt know that one ;-)

                  regards
                  jx2

                  Comment

                  • Jeigh
                    New Member
                    • Jul 2007
                    • 73

                    #10
                    I had a look at the links you gave me and now I'm using:

                    [code=php]
                    function createThumbnail ($imageDirector y, $imageName, $thumbDirectory , $thumbWidth)
                    {
                    $srcImg = imagecreatefrom jpeg("$imageDir ectory/$imageName");
                    $origWidth = imagesx($srcImg );
                    $origHeight = imagesy($srcImg );

                    list($width, $height) = getimagesize($n ewname);

                    $ratio = $origWidth / $thumbWidth;
                    $thumbHeight = $origHeight / $ratio;

                    $thumbImg_p = imagecreatetrue color($thumbWid th, $thumbHeight);

                    imagecopyresamp led($thumbImg_p , $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($thumbI mg), imagesy($thumbI mg));

                    imagejpeg($thum bImg_p, "$thumbDirector y/$imageName");
                    }

                    createThumbnail ("uploads/examples", "$newname", "uploads/examples/thumbnails", 300);
                    [/code]

                    But I'm still getting the same result, the correct size but it's cropped instead of resized. Obviously I'm missing somthing I just dont know what :P

                    (This is the first time I've used anything with GD)

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      The last two parameters of the imagecopyresamp led() function, you use in line 14, are supposed to be the size of the original image, not the thumbnail.

                      Try this:
                      [code=php]
                      imagecopyresamp led($thumbImg_p , $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);
                      [/code]

                      Comment

                      • Jeigh
                        New Member
                        • Jul 2007
                        • 73

                        #12
                        Ah, thanks a lot Atli. Works great now :D

                        Just trying to get it to work with .png and .gif as well as just jpeg using this:

                        [code=php]

                        function createThumbnail ($imageDirector y, $imageName, $thumbDirectory , $thumbWidth)
                        {

                        switch ($ext) {
                        case ".jpeg":
                        $srcImg = imagecreatefrom jpeg("$imageDir ectory/$imageName");
                        break;
                        case ".png":
                        $srcImg = imagecreatefrom png("$imageDire ctory/$imageName");
                        break;
                        case ".gif":
                        $srcImg = imagecreatefrom gif("$imageDire ctory/$imageName");
                        break;
                        }

                        $origWidth = imagesx($srcImg );
                        $origHeight = imagesy($srcImg );

                        list($width, $height) = getimagesize($n ewname);

                        $ratio = $origWidth / $thumbWidth;
                        $thumbHeight = $origHeight / $ratio;

                        $thumbImg_p = imagecreatetrue color($thumbWid th, $thumbHeight);
                        $thumbImg = imagecreate($th umbWidth, $thumbHeight);
                        /*imagecopyresiz ed($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($thumbI mg), imagesy($thumbI mg));*/

                        imagecopyresamp led($thumbImg_p , $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);

                        switch ($ext) {
                        case ".jpeg":
                        imagejpeg($thum bImg_p, "$thumbDirector y/$imageName");
                        break;
                        case ".png":
                        imagepng($thumb Img_p, "$thumbDirector y/$imageName");
                        break;
                        case ".gif":
                        imagegif($thumb Img_p, "$thumbDirector y/$imageName");
                        break;
                        }


                        }

                        createThumbnail ("uploads/examples", "$newname", "uploads/examples/thumbnails", 300);
                        [/code]

                        Which brings up an error for just about every function I was using, any clues?

                        Comment

                        • Atli
                          Recognized Expert Expert
                          • Nov 2006
                          • 5062

                          #13
                          You are using a variable called $ext in the two switch statements that has no value. I would guess that's the problem.

                          Comment

                          • Jeigh
                            New Member
                            • Jul 2007
                            • 73

                            #14
                            Sorry, forgot to mention, $ext is defined earlier in the script, which is just the extension of the file:

                            [code=php]
                            $ext = strrchr($_FILES['uploaded_image ']['name'], ".");
                            if ($ext != ".gif" AND $ext != ".jpg" AND $ext != ".jpeg" AND $ext != ".bmp" AND $ext != ".GIF" AND $ext != ".JPG" AND $ext != ".JPEG" AND $ext != ".BMP" AND $ext != ".png") {
                            $error = "your file was an unacceptable type.<br />";
                            [/code]

                            Comment

                            • jx2
                              New Member
                              • Feb 2007
                              • 228

                              #15
                              Originally posted by Jeigh
                              Sorry, forgot to mention, $ext is defined earlier in the script, which is just the extension of the file:

                              [code=php]
                              $ext = strrchr($_FILES['uploaded_image ']['name'], ".");
                              if ($ext != ".gif" AND $ext != ".jpg" AND $ext != ".jpeg" AND $ext != ".bmp" AND $ext != ".GIF" AND $ext != ".JPG" AND $ext != ".JPEG" AND $ext != ".BMP" AND $ext != ".png") {
                              $error = "your file was an unacceptable type.<br />";
                              [/code]
                              is it a code writen whitin the function or $ext is global variable?

                              if this is code writen outside this function you need to make sure $ext exist in a function

                              [php]global $ext;[/php]

                              line 22[php]$thumbImg = imagecreate($th umbWidth, $thumbHeight);[/php]
                              you never uuse this variable what this is for?

                              i strongly recomend you try to write this function on your own (its not very difficukt!!) so you can know how it works

                              regards
                              jx2

                              Comment

                              Working...