Resize then crop PHP

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

I spent many hours playing with it and I get nothing. I got it to crop a section of a larger image, etc., but I especially need the image to be resized and 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 ImageCreate functions...
  // So let 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);
}
+3
source share
4 answers

It takes some work, but it should give you enough to get you started.

function crop($filename, $width, $height)
{
    // image resource, assuming it 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 the ugly hack above.

: $src_x $src_y, manual:

imagecopyresampled() src_image of width src_w height src_h (src_x, src_y) dst_image dst_w dst_h at position (dst_x, dst_y).

, $filename , src_x 0, , src_y 0. , :

$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, ,

<?
$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 ImageCreate functions...
  // So let 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);
+4

Source: https://habr.com/ru/post/1780277/


All Articles