Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting crop to false squashes/stretches images instead of the desired resize result #23

Open
rorysmorris opened this issue Feb 25, 2016 · 1 comment

Comments

@rorysmorris
Copy link

If I pass an image through this script with crop set to false, instead of simply resizing the image, it nastily stretches/squashes the image into the specified width/height.

Example: original image is 100x500.

I want the script to take the values width:50, height:50, crop:false and return me an image that's 10x50. What is actually does is returns that portrait image squashed into a 50x50 space.

@rorysmorris
Copy link
Author

Right, I managed to sort this by hacking the code. If anyone finds this thread with the same issue, this should help:

Replace this:

if ( $crop ) {

    $cmp_x = $orig_width / $dest_width;
    $cmp_y = $orig_height / $dest_height;

    // Calculate x or y coordinate, and width or height of source
    if ( $cmp_x > $cmp_y ) {
        $src_w = round( $orig_width / $cmp_x * $cmp_y );
        $src_x = round( ( $orig_width - ( $orig_width / $cmp_x * $cmp_y ) ) / 2 );
    }
    else if ( $cmp_y > $cmp_x ) {
        $src_h = round( $orig_height / $cmp_y * $cmp_x );
        $src_y = round( ( $orig_height - ( $orig_height / $cmp_y * $cmp_x ) ) / 2 );
    }

}

// Time to crop the image!
$editor->crop( $src_x, $src_y, $src_w, $src_h, $dest_width, $dest_height );

With this:

if ( $crop ) {

    $cmp_x = $orig_width / $dest_width;
    $cmp_y = $orig_height / $dest_height;

    // Calculate x or y coordinate, and width or height of source
    if ( $cmp_x > $cmp_y ) {
        $src_w = round( $orig_width / $cmp_x * $cmp_y );
        $src_x = round( ( $orig_width - ( $orig_width / $cmp_x * $cmp_y ) ) / 2 );
    }
    else if ( $cmp_y > $cmp_x ) {
        $src_h = round( $orig_height / $cmp_y * $cmp_x );
        $src_y = round( ( $orig_height - ( $orig_height / $cmp_y * $cmp_x ) ) / 2 );
    }


    // Time to crop the image!
    $editor->crop( $src_x, $src_y, $src_w, $src_h, $dest_width, $dest_height );

} else {


    // Time to resize the image!
    $editor->resize( $dest_width, $dest_height, false );

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant