PHP裁剪图像以适应宽度和高度,同时不失去尺寸比率

13

我想创建100像素乘100像素的缩略图。我看过很多介绍方法的文章,但大多数会出现宽度!=高度的问题,如果要保持尺寸比例。

例如,我有一张450像素乘350像素的图像。我想将其裁剪为100像素乘100像素。如果我想保持比例,最终会得到100像素乘77像素。当我在行和列中列出这些图片时,这使得它们难看。然而,没有尺寸比例的图像也会显示糟糕。

我看过来自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

谢谢。


哦,现在我知道为什么有人之前骂我了。我必须点击打勾的标志!!天啊,我没注意到它。好的,让我看看回复。 - chrizonline
5个回答

42
这是通过仅使用图像的一部分作为缩略图来完成的,该缩略图具有1:1的宽高比(主要是图像中心部分)。如果您仔细观察,可以在flickr缩略图中看到它。
因为您在问题中提到了"crop",我不确定您是否已经知道了这一点,那么您想知道什么呢?
要使用剪裁,这是一个示例:
//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);

嗨,skoschnike,请确认我是否拼写正确 =)这个想法在我的脑海中闪过。但是我不知道如何做到这一点。能分享一下吗? - chrizonline
嗨Sven,我尝试了你的脚本,它很棒。非常感谢你。无瑕疵。 - chrizonline
嘿,Sven,我有一个问题。使用你的脚本非常好,但是我发现裁剪后的图像质量与原始图像不同。例如,我从Flickr中选择了一张样例图片并将其裁剪到相同的尺寸,结果它们看起来不同。有什么解决方法可以保留质量吗? - chrizonline
1
您可以在imagejpeg()函数中指定jpg质量,参见http://de.php.net/imagejpeg(文件名参数使用NULL)。默认质量为75。 - Sven Koschnicke
5
一个建议 - 一个我们经常忘记的好用函数:$smallestSide = min($height, $width)。将取出宽和高中较小的那个值。 - Enigma Plus
显示剩余3条评论

3

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

 public function croppThis($target_url) {

    $this->jpegImgCrop($target_url);

 }

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

 }

简单的工作解决方案;但首先检查文件类型并使用imagecreatefrom jpg、png或gif可能会更有帮助。 - web2kx
如何操作呢?请帮帮我。如何显示像图片那样的东西。 - user3501407

3
你可以使用以下代码。你需要传递源图像路径和缩略图大小(以像素为单位),可选的目标路径。如果你传递了目标路径,它将保存图像,否则将显示缩略图。
只允许使用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);
    }
  }
}

1
它是在哪里创建缩略图的?缩略图路径是什么?请写一些描述。 - Pathik Vejani

0
为了完成@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;
 }

0

我喜欢使用GDLib来处理图片,它非常容易使用。有很多博客文章和教程可供参考。不过我建议使用类或类似的东西,因为处理所有图像变化可能非常耗时。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接