Resize then crop PHP

2020-07-22 18:05发布

问题:

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..

Any help would be greatly appreciated.

// 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);
}

回答1:

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 :)
}

Edit: Trying to explain "ugly hack" above.

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

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).

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]
    );
}


回答2:

ok, here's a working one;

<?
$img = 'leaf.jpg';
// get image size of img
$x = @getimagesize($img);

// image dimensions
$sw = $x[0];
$sh = $x[1];

//dest size
$dSize = 170;

//find smallerst part and get needed scale and offset
$yOff = 0;
$xOff = 0;
if($sw < $sh) {
  $scale = $dSize / $sw;
  $yOff = $sh/2 - $dSize/$scale/2; 
} else {
  $scale = $dSize / $sh;
  $xOff = $sw/2 - $dSize/$scale/2; 
}

$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 ($dSize,$dSize);
  // Copy from image source, resize it, and paste to image destination
  imagecopyresampled($thumb, $im, 
    0, 0, 
    $xOff,$yOff,
    $dSize, $dSize, 
    $dSize / $scale ,$dSize / $scale);
}
header('content-type:image/jpeg');
imagejpeg($thumb);
//imagejpeg($im);


回答3:

Are you using ImageMagic? If not, you should. http://php.net/manual/en/book.imagick.php



回答4:

You can try this out, I haven't yet but it looks promising. http://phpthumb.sourceforge.net/