简体   繁体   English

调整图像大小以完全适合尺寸(Codeigniter)

[英]Resize Image to Fit Dimensions Completely (Codeigniter)

I am using the image library in Codeigniter to resize my image.我正在使用 Codeigniter 中的图像库来调整图像大小。 I notice that CI only resizes the image to fit inside the specified dimensions: A 50px by 50px image resized to fit a 30px by 20px box will be resized to 20px by 20px and leaves a black area for the empty space.我注意到 CI 仅调整图像大小以适应指定尺寸:调整大小以适应 30 像素 x 20 像素框的 50 像素 x 50 像素图像将被调整为 20 像素 x 20 像素,并为空白空间留下黑色区域。

What I want is to resize such that the resize image fills the entire 30px by 30px space with the excess portion being cropped out.我想要的是调整大小,使调整大小的图像填充整个 30px x 30px 的空间,多余的部分被裁剪掉。

Is this possible in Codeigniter?这在 Codeigniter 中是否可行?

I think you'll need to resize the image first, choosing either width or height as the "master" dimension, then crop it.我认为您需要先调整图像大小,选择宽度或高度作为“主”尺寸,然后裁剪它。

I haven't tested this code, but give it a shot.我没有测试过这段代码,但试一试。 If nothing else, it should give you the idea.如果没有别的,它应该给你的想法。

$config['source_image'] = '/path/to/image/mypic.jpg';

$config['width'] = 30;
$config['height'] = 0; // No restraint on height
$this->load->library('image_lib', $config);
$this->image_lib->resize();

$config['x_axis'] = 30; // Same width as before
$config['y_axis'] = 20; // Crop to height
$this->image_lib->initialize($config);
$this->image_lib->crop();

You may want to try to center the crop dimensions instead.您可能想尝试将裁剪尺寸居中。 Whichever way you choose to do it, the image needs to be cropped at some point.无论您选择哪种方式,都需要在某些时候裁剪图像。

I should note that I'm not sure if $config['height'] = 0 is the right way to ignore height, it might have to be FALSE , a really high number, or removed altogether (haven't worked with the CI image lib in a while).我应该注意,我不确定$config['height'] = 0是否是忽略高度的正确方法,它可能必须是FALSE ,一个非常高的数字,或者完全删除(没有使用 CI一会儿图像库)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM