本文介绍了PHP裁剪图像以固定宽度和高度而不会丢失尺寸比率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找创建100px×100px维度的缩略图。我已经看过许多解释这些方法的文章,但是如果要保持尺寸比,大多数文章最终会有宽度!=高度。

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.

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

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.

我看过flickr的图像,看起来很棒。例如:

thumbnail:

中等大小:

大尺寸:

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

tks

推荐答案

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

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.

因为你的问题中有裁剪,我不确定你是否还不知道这个,但你想知道什么呢?

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

这篇关于PHP裁剪图像以固定宽度和高度而不会丢失尺寸比率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:26