简体   繁体   English

PHP图像裁剪到正方形

[英]PHP Image cropping to square

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/ 我正在使用以下PHP脚本创建方形缩略图,该缩略图已在此处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. 我知道有解决此问题的方法,但是由于我是PHP的新手,所以我无法解决它。

Can someome with real experience in PHP fix this? 某些在PHP中具有实际经验的人可以解决此问题吗? 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); 我这样称呼它-square_crop('source_image','destination_image',65);

You can see the result here http://imageshack.us/photo/my-images/717/imgfl.png/ 您可以在这里查看结果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. 仅在人像图像中会发生这种情况,对风景图像进行裁切时会填满整个正方形。

For cropping only, replace the imagecopyresampled() function with imagecopy(). 仅用于裁剪,将imagecopyresampled()函数替换为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. imagecopy()没有。

you should add an image ration inside the if() statement, so it will understand if it's portrait or landscape. 您应该在if()语句中添加一个图像配比,这样它将了解它是纵向还是横向。

change the line below 更改下面的行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM