PHP图像裁剪到正方形 [英] PHP Image cropping to square

查看:1186
本文介绍了PHP图像裁剪到正方形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下PHP脚本创建方形缩略图,我在这里 http://www.abeautifulsite.net/blog/2009/08/cropping-an-image-to-make-square-thumbnails-in-php/

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比较陌生,我无法解决它。

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? 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;
     }
    }
    ?>

我称之为square_crop('source_image','destination_image',65);

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

你可以在这里看到结果 http://imageshack.us/photo/my-images/717/imgfl.png/

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.

推荐答案

仅用于裁剪,用imagecopy替换imagecopyresampled()函数( )。

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

如果源和目标坐标以及宽度和高度不同,重新采样会对图像执行适当的拉伸或缩小。 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.

这篇关于PHP图像裁剪到正方形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆