PHP裁剪图像以固定宽度和高度而不丢失尺寸比
我想创建尺寸为100px×100px的缩略图。我已经看过很多文章解释了这些方法,但大多数文章最后都有宽度=如果要保持尺寸比,则为高度 例如,我有一个450px x 350px的图像。我想在100px之前裁剪到100px。如果我保持这个比例,我会得到100px乘以77px的结果。当我把这些图片列成一行和一列时,这会让人觉得很难看。然而,没有尺寸比的图像看起来也很糟糕 我看过flickr的图片,它们看起来棒极了。例如:PHP裁剪图像以固定宽度和高度而不丢失尺寸比,php,image,thumbnails,crop,Php,Image,Thumbnails,Crop,我想创建尺寸为100px×100px的缩略图。我已经看过很多文章解释了这些方法,但大多数文章最后都有宽度=如果要保持尺寸比,则为高度 例如,我有一个450px x 350px的图像。我想在100px之前裁剪到100px。如果我保持这个比例,我会得到100px乘以77px的结果。当我把这些图片列成一行和一列时,这会让人觉得很难看。然而,没有尺寸比的图像看起来也很糟糕 我看过flickr的图片,它们看起来棒极了。例如: 缩略图: 中等尺寸: 大尺寸: tks这是通过仅使用图像的一部分作为高宽比为1:
缩略图:
中等尺寸:
大尺寸:
tks这是通过仅使用图像的一部分作为高宽比为1:1(主要是图像的中心)的缩略图来完成的。如果仔细观察,可以在flickr缩略图中看到它 因为你的问题中有“作物”,我不确定你是否还不知道,但你想知道什么 要使用裁剪,以下是一个示例:
//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);
我喜欢使用GDLib来处理图像,它也非常容易处理。那里有很多博客文章和教程。不过,我建议使用一个类或类似的类,因为处理图像中的所有变化可能非常耗时。根据较小的宽度或高度裁剪正方形图像
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);
}
公共函数jpegImgCrop($target\u url){//支持
$image=imagecreatefromjpeg($target\u url);
$filename=$target\u url;
$width=imagesx($image);
$height=imagesy($image);
$image_type=imagetypes($image);//IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM
如果($width==$height){
$thumb_width=$width;
$thumb_height=$height;
}elseif($width$height){
$thumb_width=$height;
$thumb_height=$height;
}否则{
$thumb_宽度=150;
$thumb_高度=150;
}
$original_aspect=$width/$height;
$thumb\u aspect=$thumb\u width/$thumb\u height;
如果($original\u aspect>=$thumb\u aspect){
//如果图像比缩略图宽(在纵横比意义上)
$new_height=$thumb_height;
$new_width=$width/($height/$thumb_height);
}
否则{
//如果缩略图比图像宽
$new_width=$thumb_width;
$new_height=$height/($width/$thumb_width);
}
$thumb=ImageCreateTureColor($thumb\u宽度,$thumb\u高度);
//调整大小并裁剪
ImageCopy重新采样($thumb,
$image,
0-($new\u width-$thumb\u width)/2,//将图像水平居中
0-($new\u height-$thumb\u height)/2,//将图像垂直居中
0, 0,
$new_width,$new_height,
$width,$height);
imagejpeg($thumb,$filename,80);
}
您可以使用此代码。
您需要传递px中的源图像路径和缩略图大小,以及可选的目标路径。如果您通过,它将保存图像,否则将显示拇指
只允许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);
}
}
}
要完成@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;
}
哦,现在我知道为什么有人因为那件事责骂我了。我必须点击勾号!!。天哪,我还没注意到。好的,让我看看我脑海中浮现的回答:如果我拼写正确,请仔细检查你的名字=)。但是我不知道怎么做。想和大家分享吗?嘿,斯文,我试过你的剧本,效果很好。非常感谢你。完美的谢伊·斯文,我有个问题。使用你的脚本效果很好。但我意识到裁剪后的图像质量与原始图像不一样。例如,我从flickr和crop上取了一张样本图片,它们的尺寸相同,看起来不同。任何保留质量的解决方案??您可以在imagejpeg()函数中指定jpg质量,请参见此处(使用NULL作为文件名参数)。默认质量是75。只是一个建议——一个我们都忘记的很好的小函数:$smallestSide=min($height,$width);简易解决方案;但是先检查文件类型,然后使用imagecreatefrom jpg、png或gif可能更有帮助。。请帮帮我。。如何像图片一样显示它在哪里?拇指路径是什么?也写一些描述。