sk4*_*b3n 12 image image-processing laravel-4
我想通过Laravel 4中的干预图像功能调整我的图像大小,但为了保持图像的宽高比,这就是我的代码:
$image_make = Image::make($main_picture->getRealPath())->fit('245', '245', function($constraint) { $constraint->aspectRatio(); })->save('images/articles/'.$gender.'/thumbnails/245x245/'.$picture_name);
Run Code Online (Sandbox Code Playgroud)
问题是这不能保持我的图像的宽高比,谢谢.
Bog*_*dan 25
如果您需要在限制范围内调整,你应该使用resize没有fit.如果您还需要将图像置于约束中心,则应创建一个新canvas图像并在其中插入已调整大小的图像:
// This will generate an image with transparent background
// If you need to have a background you can pass a third parameter (e.g: '#000000')
$canvas = Image::canvas(245, 245);
$image = Image::make($main_picture->getRealPath())->resize(245, 245, function($constraint)
{
$constraint->aspectRatio();
});
$canvas->insert($image, 'center');
$canvas->save('images/articles/'.$gender.'/thumbnails/245x245/'.$picture_name);
Run Code Online (Sandbox Code Playgroud)
小智 9
只需将其调整为图像的最大宽度/高度,并使画布适合所需的最大宽度和高度
Image::make($main_picture->getRealPath())->resize(245, 245,
function ($constraint) {
$constraint->aspectRatio();
})
->resizeCanvas(245, 245)
->save('images/articles/'.$gender.'/thumbnails/245x245/'.$picture_name, 80);
Run Code Online (Sandbox Code Playgroud)
我知道这是一个旧线程,但如果有一天有人需要它,我会分享我的实现。
我的实现正在查看接收到的图像的纵横比,并将使用新的高度或宽度调整大小(如果需要)。
public function resizeImage($image, $requiredSize) {
$width = $image->width();
$height = $image->height();
// Check if image resize is required or not
if ($requiredSize >= $width && $requiredSize >= $height) return $image;
$newWidth;
$newHeight;
$aspectRatio = $width/$height;
if ($aspectRatio >= 1.0) {
$newWidth = $requiredSize;
$newHeight = $requiredSize / $aspectRatio;
} else {
$newWidth = $requiredSize * $aspectRatio;
$newHeight = $requiredSize;
}
$image->resize($newWidth, $newHeight);
return $image;
}
Run Code Online (Sandbox Code Playgroud)
您需要传递图像 ( $image = Image::make($fileImage->getRealPath());) 和所需的大小(例如:480)。
这是输出
100x100。什么都不会发生,因为宽度和高度小于所需的480尺寸。3000x1200。这是一张横向图像,将调整为:(480x192保持纵横比)980x2300。这是一幅肖像图像,将被调整为:204x480。1000x1000。这是宽度和高度相等的 1:1 纵横比图像。这将调整为:480x480。| 归档时间: |
|
| 查看次数: |
9402 次 |
| 最近记录: |