将每种尺寸的图像调整为固定的宽度和高度

Joh*_*ohn 1 php image image-resizing

我尝试了很多方法来做到这一点,但我仍然有很多麻烦.

是否可以将所有图像调整为固定的宽度和高度?

我想每一个上传的图片与width>=200pxheight>=260px被调整到width=200pxheight=260px,但我想保持一点比例,如果图像是大于200x260px调整其大小比例,然后捕获图像的中心200x260px.

我只是想知道从哪里开始和做什么,但如果你有一个例子,我希望看到它.谢谢.

Dja*_*ous 5

如果您想修剪图像,可以通过以下方式进行修剪: -

//Your Image
$imgSrc = "image.jpg";

//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);

//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);

// calculating the part of the image to use for thumbnail
if ($width > $height) {
  $y = 0;
  $x = ($width - $height) / 2;
  $smallestSide = $height;
} else {
  $x = 0;
  $y = ($height - $width) / 2;
  $smallestSide = $width;
}

// copying the part into thumbnail
$thumbSize = 100;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize,        $smallestSide, $smallestSide);

//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);
Run Code Online (Sandbox Code Playgroud)