调整大小然后裁剪PHP [英] Resize then crop PHP

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

问题描述

好吧,基本上我希望所有图像都为170x170px正方形. 因此,如果图像不是正方形,我希望将其调整大小,然后在中间裁剪.

Ok, basically I want all images to be 170x170px squares. Thus if an image is not a square i want it to be resized, and then cropped in the middle..

我花了很多时间来处理这个问题,但是却一无所获.我已经得到了它来裁剪较大图像的一部分,但是我特别需要调整图像的大小,然后裁剪.

I have spent numerous hours playing with this and I am getting nowhere.. I have gotten it to crop a section of the bigger image etc, but i specifically need the image to be resized, then cropped..

任何帮助将不胜感激.

// get image size of img
$x = @getimagesize($img);
// image width
$sw = $x[0];
// image height
$sh = $x[1];

if($sw > $sh) // Horizontal Rectangle?
{
  $newwidth = ($sw/$sh)*170;
  $newheight=170;   
  $x_pos = ($sw - $sh) / 2;
  $x_pos = ceil($x_pos);
  $y_pos=0;
}

else if($sh > $sw) // Vertical Rectangle?
{
  $newheight = ($sh/$sw)*170;
  $newwidth=170;
  $y_pos = ($sh - $sw) / 2;
  $y_pos = ceil($y_pos);
  $x_pos=0;
}
else //Already Square
{
  $newheight=170;
  $newwidth=170;
}

$im = @ImageCreateFromJPEG ($img) or // Read JPEG Image
$im = @ImageCreateFromPNG ($img) or // or PNG Image
$im = @ImageCreateFromGIF ($img) or // or GIF Image
$im = false; // If image is not JPEG, PNG, or GIF

if (!$im) {
  // We get errors from PHP's ImageCreate functions...
  // So let's echo back the contents of the actual image.
  readfile ($img);
} else {
  // Create the resized image destination
  $thumb = @ImageCreateTrueColor (170, 170);
  // Copy from image source, resize it, and paste to image destination
  imagecopyresampled($thumb, $im, 0, 0, 180, $y_pos, 170, 170, $newwidth, 
    $newheight);
}

推荐答案

需要一些工作,但是它应该给您足够的开始.

Needs some work, but it should give you enough to start with.

function crop($filename, $width, $height)
{
    // image resource, assuming it's PNG
    $resource = imagecreatefrompng($filename);
    // resource dimensions
    $size = array(
        0 => imagesx($resource),
        1 => imagesy($resource),
    );
    // sides
    $longer  = (int)($size[0]/$width > $size[1]/$height);
    $shorter = (int)(!$longer);
    // ugly hack to avoid condition for imagecopyresampled()
    $src = array(
        $longer  => 0,
        $shorter => ($size[$shorter]-$size[$longer])/2,
    );
    // new image resource
    $new = imagecreatetruecolor($width, $height);
    // do the magic
    imagecopyresampled($new, $resource,
        0,  0,
        $src[0], $src[1],
        $width, $height,
        $size[$longer], $size[$longer]
    );

    // save it or something else :)
}

试图在上面解释丑陋的骇客".

Trying to explain "ugly hack" above.

有问题的两个参数是$src_x$src_y,取自手册:

Two parameters in question are $src_x and $src_y, taken from manual:

imagecopyresampled()将在src_image中占据一个矩形区域 在位置(src_x,src_y)的宽度src_w和高度src_h并将其放置在 dst_image的矩形区域,宽度为dst_w,高度为dst_h 位置(dst_x,dst_y).

imagecopyresampled() will take an rectangular area from src_image of width src_w and height src_h at position (src_x,src_y) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x,dst_y).

表示$filename的宽度更长,src_x必须为0,如果高度更长,src_y必须为0.翻译成代码,看起来像这样:

Meaning if $filename's width is longer, src_x has to be 0, and if height is longer, src_y has to be 0. Translated into code, it would look something like this:

$src = ($size[$shorter]-$size[$longer])/2;

if ( $longer === 1 )
{
    imagecopyresampled($new, $resource,
        0,  0,
        $src, 0,
        $width, $height,
        $size[$longer], $size[$longer]
    );
}
else
{
    imagecopyresampled($new, $resource,
        0,  0,
        0, $src,
        $width, $height,
        $size[$longer], $size[$longer]
    );
}

这篇关于调整大小然后裁剪PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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