PHP Image cropping to square

你离开我真会死。 提交于 2019-12-23 05:20:34

问题


I was using the following PHP script to create square thumbnails which I got here http://www.abeautifulsite.net/blog/2009/08/cropping-an-image-to-make-square-thumbnails-in-php/

I was able to integrate this into my image upload script, which uploads full sized image and after that takes the uploaded image and creates a thumbnail from that. The problem is the author of the script said, it will crop landscape and portrait images without problem. It crops landscape images perfectly, but as it faces portrait image, the output thumbnail will not be cropped, but it appears scaled down to fit the given square thumbnail height and the emtpy space on the sides will be filled with black color. I know there is a way to fix this, but as I am relatively new to PHP I am not able to solve it.

Can someome with real experience in PHP fix this? Thank you in advance!

The script is here: $

 function square_crop($src_image, $dest_image, $thumb_size = 64, $jpg_quality = 90) {

// Get dimensions of existing image
$image = getimagesize($src_image);

// Check for valid dimensions
if( $image[0] <= 0 || $image[1] <= 0 ) return false;

// Determine format from MIME-Type
$image['format'] = strtolower(preg_replace('/^.*?\//', '', $image['mime']));

// Import image
switch( $image['format'] ) {
    case 'jpg':
    case 'jpeg':
        $image_data = imagecreatefromjpeg($src_image);
    break;
    case 'png':
        $image_data = imagecreatefrompng($src_image);
    break;
    case 'gif':
        $image_data = imagecreatefromgif($src_image);
    break;
    default:
        // Unsupported format
        return false;
    break;
}

// Verify import
if( $image_data == false ) return false;

// Calculate measurements
if( $image[0] & $image[1] ) {
    // For landscape images
    $x_offset = ($image[0] - $image[1]) / 2;
    $y_offset = 0;
    $square_size = $image[0] - ($x_offset * 2);
} else {
    // For portrait and square images
    $x_offset = 0;
    $y_offset = ($image[1] - $image[0]) / 2;
    $square_size = $image[1] - ($y_offset * 2);
}

// Resize and crop
$canvas = imagecreatetruecolor($thumb_size, $thumb_size);
if( imagecopyresampled(
    $canvas,
    $image_data,
    0,
    0,
    $x_offset,
    $y_offset,
    $thumb_size,
    $thumb_size,
    $square_size,
    $square_size
)) {

    // Create thumbnail
    switch( strtolower(preg_replace('/^.*\./', '', $dest_image)) ) {
        case 'jpg':
        case 'jpeg':
            return imagejpeg($canvas, $dest_image, $jpg_quality);
        break;
        case 'png':
            return imagepng($canvas, $dest_image);
        break;
        case 'gif':
            return imagegif($canvas, $dest_image);
        break;
        default:
            // Unsupported format
            return false;
        break;
    }

     } else {
    return false;
     }
    }
    ?>

And I call it like this - square_crop('source_image', 'destination_image', 65);

You can see the result here http://imageshack.us/photo/my-images/717/imgfl.png/

It happens only with portrait images, landscape images are cropped in a way that it fills the whole square.


回答1:


For cropping only, replace the imagecopyresampled() function with imagecopy().

The resampled performs an appropriate stretching or shrinking of the image if the source and destination coordinates and width and heights differ. imagecopy() doesn't.




回答2:


you should add an image ration inside the if() statement, so it will understand if it's portrait or landscape.

change the line below

// Calculate measurements
if( $image[0] > $image[1] ) {


来源:https://stackoverflow.com/questions/10107306/php-image-cropping-to-square

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!