开发者

Resizing Images with Codeigniter

Hey :) I want to resize an image when I address it in a form(save to database). I dont think开发者_StackOverflow社区 it will be a problem [pointing to the image etc, however the space I am going display the thumb in is 228 x 228. I know I can maintain the ratio and determine the size in the controller, yet how is it done on images that are frequently different sizes?

Is the best way to address this is to put pre-calculated values(make my own ratio) in my form so that the controller can use those values to determine max height and width? See code.

$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width']     = 75; // use $this->input->post('width') instead??
$config['height']   = 50;  // use $this->input->post('height')

Or is there a way to tell the function no bigger than 228 and no wider than 228?

Thanks


I am not sure what you want exactly, but by using the following settings you can make sure either the width or the height of the image is 228px. If width > height, width will be 228px. And vice versa.

$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'auto' // auto is default, so you can leave this out
$config['width'] = 228;
$config['height'] = 228;

master_dim

Specifies what to use as the master axis when resizing or creating thumbs. For example, let's say you want to resize an image to 100 X 75 pixels. If the source image size does not allow perfect resizing to those dimensions, this setting determines which axis should be used as the hard value. "auto" sets the axis automatically based on whether the image is taller then wider, or vice versa.

If this is not what you mean, please add a bit more explanation.


getImageSize()

I think you'd have to throw some logic in your controller to handle this. Using the function above you can test the height/width of the uploaded image and resize if necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜