Image resizing problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aaronic
    New Member
    • Nov 2006
    • 11

    Image resizing problem

    This is in reference to my previos post but a completely different problem.

    Previous problem and code can be found here:


    When I run the function like this, it gives me weird images (ratio is way off)

    [php]
    $uploaddir = '/home/avonprom/www/product_images/';
    $thumbdir = '/home/avonprom/www/product_images/';
    $uploadfile = $uploaddir . $_POST['sku'] . ".jpg";
    $thumbfile = $thumbdir . "sm_" . $_POST['sku'] . ".jpg";
    $mainfile = $thumbdir . "mid_" . $_POST['sku'] . ".jpg";

    if (move_uploaded_ file($_FILES['thumbnail']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";

    createthumb($up loadfile,$thumb file,120,100);

    createthumb($up loadfile,$mainf ile,285,407);

    } else {
    echo "Possible file upload attack!\n";
    }

    [/php]

    For example, one image was 285 x 450 which should have created a slightly smaller image due to 450 being too wide. Instead it came out 285 x 600 or something like that.

    Does anyone see the error in calculation?
    Thanks in advance

    Aaron

    EDIT:
    In case you want to see the function here, here it is:

    [php]
    function createthumb($na me,$filename,$n ew_w,$new_h)
    {
    $system=explode (".",$name);
    if (preg_match("/jpg|jpeg/",$system[1])){$src_img=ima gecreatefromjpe g($name);}
    if (preg_match("/png/",$system[1])){$src_img=ima gecreatefrompng ($name);}
    $old_x=imageSX( $src_img);
    $old_y=imageSY( $src_img);
    if ($old_x > $old_y)
    {
    $thumb_w=$new_w ;
    $thumb_h=$old_y *($new_h/$old_x);
    }
    if ($old_x < $old_y)
    {
    $thumb_w=$old_x *($new_w/$old_y);
    $thumb_h=$new_h ;
    }
    if ($old_x == $old_y)
    {
    if ($new_w < $new_h)
    {
    $thumb_w=$new_w ;
    $thumb_h=$new_w ;
    }
    elseif ($new_h < $new_w)
    {
    $thumb_w=$new_h ;
    $thumb_h=$new_h ;
    }
    else
    {
    $thumb_w=$new_w ;
    $thumb_h=$new_h ;
    }
    }
    $dst_img=ImageC reateTrueColor( $thumb_w,$thumb _h);
    imagecopyresamp led($dst_img,$s rc_img,0,0,0,0, $thumb_w,$thumb _h,$old_x,$old_ y);
    if (preg_match("/png/",$system[1]))
    {
    imagepng($dst_i mg,$filename);
    } else {
    imagejpeg($dst_ img,$filename);
    }
    imagedestroy($d st_img);
    imagedestroy($s rc_img);
    }
    [/php]
  • aaronic
    New Member
    • Nov 2006
    • 11

    #2
    I think this has something to do with the fact that the script was made to think that the width x height ratio would be the same between the source and the end result. The script needs some modification which I am going to look into but I would appreciate if anyone here could figure it out.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      In order to avoid stretching, either height or width, you'll need to crop the image at some point.
      Code:
      Assume: image 400 x 200.
      Required sizes: 200 x 100  = no problem ratio unchanged
                      200 x 50   = ratio 200 x 100: you'd have to crop 50 pixels from one side or 25 from left and right side
                      200 x 150  = ratio 300 x 150: you'd have to crop 100 from the top or 50 from top and bottom
      You can use imagecopy to crop your image.

      Ronald :cool:

      Comment

      • aaronic
        New Member
        • Nov 2006
        • 11

        #4
        Originally posted by ronverdonk
        In order to avoid stretching, either height or width, you'll need to crop the image at some point.
        Code:
        Assume: image 400 x 200.
        Required sizes: 200 x 100  = no problem ratio unchanged
                        200 x 50   = ratio 200 x 100: you'd have to crop 50 pixels from one side or 25 from left and right side
                        200 x 150  = ratio 300 x 150: you'd have to crop 100 from the top or 50 from top and bottom
        You can use imagecopy to crop your image.

        Ronald :cool:

        Cropping is not an option. I would simply reduce the image size in that situation. I'll use your example and hopefully you can help come up with some code:

        Code:
        Assume: image 400 x 200.
        Required sizes: 200 x 100  = no problem ratio unchanged
                        200 x 50 (final max size)   = final image 100 x 50: Keeping within our 50 height, our image would now be 100x 50
                        200 x 150 (final max size)  = final image 300 x 150: To keep within our max heights of 150.
        Please help :D

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Ok, so no cropping. But what is the leading size for your thumb, height or width. You'll have to establish that first as the basis for determining the ratio.

          Ronald :cool:

          Comment

          • aaronic
            New Member
            • Nov 2006
            • 11

            #6
            Originally posted by ronverdonk
            Ok, so no cropping. But what is the leading size for your thumb, height or width. You'll have to establish that first as the basis for determining the ratio.

            Ronald :cool:

            Thanks for the help, but the script should work no matter what the source image ratio is, or what the thumbnail ratio is. The function allows you to send it any width/height.

            I think I am going to just make like 10 different examples and come up with a formula that produces the right answer everytime.

            If you have any further help please post :D

            Comment

            • aaronic
              New Member
              • Nov 2006
              • 11

              #7
              Ok please help :)

              I need to create two images, one that is max 120x100 and another 285x407.

              Now with this information I guess the function wouldn't have to work with ANY possible destination size.

              Does that help?

              Comment

              • aaronic
                New Member
                • Nov 2006
                • 11

                #8
                RESOLVED!

                Changes to code posted below:

                [php]
                function createthumb($na me,$filename,$n ew_w,$new_h)
                {
                $system=explode (".",$name);
                if (preg_match("/jpg|jpeg/",$system[1])){$src_img=ima gecreatefromjpe g($name);}
                if (preg_match("/png/",$system[1])){$src_img=ima gecreatefrompng ($name);}
                $old_x=imageSX( $src_img);
                $old_y=imageSY( $src_img);
                //my edits STAR HERE
                $old_ratio = $old_x / $old_y;

                $new_ratio = $new_w / $new_h;

                if ($old_ratio < $new_ratio)
                {

                $thumb_h=$new_h ;
                $thumb_w=$new_h * $old_ratio;
                }
                if ($old_ratio > $new_ratio)
                {
                $thumb_h=$new_w / $old_ratio;
                $thumb_w=$new_w ;
                }
                if ($old_ratio == $new_ratio)
                {
                $thumb_w=$new_w ;
                $thumb_h=$new_h ;
                }
                // AND END HERE. ENJOY
                $dst_img=ImageC reateTrueColor( $thumb_w,$thumb _h);
                imagecopyresamp led($dst_img,$s rc_img,0,0,0,0, $thumb_w,$thumb _h,$old_x,$old_ y);
                if (preg_match("/png/",$system[1]))
                {
                imagepng($dst_i mg,$filename);
                } else {
                imagejpeg($dst_ img,$filename);
                }
                imagedestroy($d st_img);
                imagedestroy($s rc_img);
                }



                [/php]

                Comment

                Working...