PHP 裁剪图像以固定宽度和高度而不丢失尺寸比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3255773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:08:43  来源:igfitidea点击:

PHP crop image to fix width and height without losing dimension ratio

phpimagethumbnailscrop

提问by nuttynibbles

im looking to create thumbnails that has 100px by 100px dimension. i've seen many articles explaining the methods but most end up having the width!=height if the dimension ratio is to be kept.

我希望创建具有 100px x 100px 尺寸的缩略图。我看过很多解释这些方法的文章,但如果要保持尺寸比,大多数文章最终都会有宽度!=高度。

for example, i have a 450px by 350px image. i would like to crop to 100px by 100px. if i were to keep the ratio, i would end up having 100px by 77px. this makes it ugly when im listing these images in a row and column. however, a image without dimension ratio will look terrible as well.

例如,我有一个 450 像素 x 350 像素的图像。我想裁剪到 100 像素乘 100 像素。如果我要保持比例,我最终会得到 100 像素 x 77 像素。当我在一行和一列中列出这些图像时,这使得它很难看。然而,没有尺寸比的图像也会看起来很糟糕。

i've seen images from flickr and they look fantastic. for example:
thumbnail: http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
medium size: http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
large size: http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg

我看过来自 flickr 的图片,它们看起来很棒。例如:
缩略图:http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
中等大小:http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
大尺寸:HTTP:// farm1 .static.flickr.com/23/32608803_29470dfeeb_b.jpg

tks

tks

回答by Sven Koschnicke

This is done by only using a part of the image as the thumbnail which has a 1:1 aspect ratio (mostly the center of the image). If you look closely you can see it in the flickr thumbnail.

这是通过仅使用图像的一部分作为具有 1:1 纵横比(主要是图像的中心)的缩略图来完成的。如果仔细观察,您可以在 flickr 缩略图中看到它。

Because you have "crop" in your question, I'm not sure if you didn't already know this, but what do you want to know then?

因为您的问题中有“作物”,我不确定您是否已经知道这一点,但是您想知道什么?

To use cropping, here is an example:

要使用裁剪,这里是一个例子:

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

回答by ShivarajRH

Crop image with square based on lesser width or height

根据较小的宽度或高度使用正方形裁剪图像

 public function croppThis($target_url) {

    $this->jpegImgCrop($target_url);

 }

$target_url - is Name of image.

$target_url - 是图像的名称。

 public function jpegImgCrop($target_url) {//support



  $image = imagecreatefromjpeg($target_url);
  $filename = $target_url;
  $width = imagesx($image);
  $height = imagesy($image);
  $image_type = imagetypes($image); //IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM

  if($width==$height) {

   $thumb_width = $width;
   $thumb_height = $height;

  } elseif($width<$height) {

   $thumb_width = $width;
   $thumb_height = $width;

  } elseif($width>$height) {

   $thumb_width = $height;
   $thumb_height = $height;

  } else {
   $thumb_width = 150;
   $thumb_height = 150;
  }

  $original_aspect = $width / $height;
  $thumb_aspect = $thumb_width / $thumb_height;

  if ( $original_aspect >= $thumb_aspect ) {

     // If image is wider than thumbnail (in aspect ratio sense)
     $new_height = $thumb_height;
     $new_width = $width / ($height / $thumb_height);

  }
  else {
     // If the thumbnail is wider than the image
     $new_width = $thumb_width;
     $new_height = $height / ($width / $thumb_width);
  }

  $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

  // Resize and crop
  imagecopyresampled($thumb,
         $image,
         0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
         0 - ($new_height - $thumb_height) / 2, // Center the image vertically
         0, 0,
         $new_width, $new_height,
         $width, $height);
  imagejpeg($thumb, $filename, 80);

 }

回答by sajad abbasi

You can use this code. You need to pass source image path and thumbnail size in px, and optional destination path. if you pass it will save the image else the thumb will be shown.

您可以使用此代码。您需要以 px 为单位传递源图像路径和缩略图大小,以及可选的目标路径。如果您通过,它将保存图像,否则将显示拇指。

Only jpg, jpeg and png are allowed.

只允许使用 jpg、jpeg 和 png。

function cropImage($sourcePath, $thumbSize, $destination = null) {

  $parts = explode('.', $sourcePath);
  $ext = $parts[count($parts) - 1];
  if ($ext == 'jpg' || $ext == 'jpeg') {
    $format = 'jpg';
  } else {
    $format = 'png';
  }

  if ($format == 'jpg') {
    $sourceImage = imagecreatefromjpeg($sourcePath);
  }
  if ($format == 'png') {
    $sourceImage = imagecreatefrompng($sourcePath);
  }

  list($srcWidth, $srcHeight) = getimagesize($sourcePath);

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

  $destinationImage = imagecreatetruecolor($thumbSize, $thumbSize);
  imagecopyresampled($destinationImage, $sourceImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);

  if ($destination == null) {
    header('Content-Type: image/jpeg');
    if ($format == 'jpg') {
      imagejpeg($destinationImage, null, 100);
    }
    if ($format == 'png') {
      imagejpeg($destinationImage);
    }
    if ($destination = null) {
    }
  } else {
    if ($format == 'jpg') {
      imagejpeg($destinationImage, $destination, 100);
    }
    if ($format == 'png') {
      imagepng($destinationImage, $destination);
    }
  }
}

回答by Albuquerque Web Design

To complete @SvenKoschnicke code, here is little tool to process other image formats:

要完成@SvenKoschnicke 代码,这里有一个处理其他图像格式的小工具:

$sourceProperties = getimagesize($imgSrc);

 $width = $sourceProperties[0];

 $height = $sourceProperties[1];

 switch ($sourceProperties[2]) {

 case IMAGETYPE_PNG:
        $myImage = imagecreatefrompng($imgSrc); 
        break;

  case IMAGETYPE_GIF:
        $myImage = imagecreatefromgif($imgSrc); 
        break;

  case IMAGETYPE_JPEG:
        $myImage = imagecreatefromjpeg($imgSrc); 
        break;
 }

回答by David Yell

I like using GDLib to fiddle with images, it's fantastically easy to work with also. There are lots of blog posts and tutorials out there. I would recommend using a class for it or similar though, as taking care of all the variations in the image can be very time consuming.

我喜欢使用 GDLib 来处理图像,它也非常容易使用。那里有很多博客文章和教程。不过,我建议使用一个类或类似的类,因为处理图像中的所有变化可能非常耗时。