如果图像太小,如何在图像上添加空白以适合给定的大小

时间:2012-05-22 13:00:19

标签: php codeigniter

我有不同尺寸的图像(低于200x200像素)我想通过添加给定颜色的空白(例如白色)使它们为200x200像素。 我尝试使用裁剪库裁剪它们:http://codeigniter.com/user_guide/libraries/image_lib.html 我不工作。 以下是一些我没有成功使用的代码:

$config = Array(
 'image_library' => 'gd2',
 'source_image' => '/path/to/my/image.jpg',
 'new_image' => '/path/to/my/small/image.jpg',
 'thumb_marker' => '',
 'create_thumb' => TRUE,
 'maintain_ratio' => TRUE,
 'width' => '200',
 'height' => '200'
);

$this->load->library('image_lib', $config);
if (!$this->image_lib->resize())
{
 $this->session->set_flashdata('message_error',  $this->image_lib->display_errors());
 redirect('/mon_controller', 'location');
}

$config['source_image'] = '/path/to/my/small/image.jpg';
$config['x_axis'] = '200';
$config['y_axis'] = '200';

$this->image_lib->initialize($config);

if ( ! $this->image_lib->crop())
{
 $this->session->set_flashdata('message_error',  $this->image_lib->display_errors());
 redirect('/my_controller', 'location');
}

编辑:我认为lib符合我的需求:http://www.matmoo.com/digital-dribble/codeigniter/image_moo/

2 个答案:

答案 0 :(得分:1)

我想你需要这个。

http://www.matmoo.com/digital-dribble/codeigniter/image_moo/

答案 1 :(得分:1)

很抱歉没有回答您的问题,但我建议您使用CSS,这样您就可以保留原始图片,并且可以在以后的任何地方使用它们(如果您的设计明天更改,或者想要在另一个页面中使用它们,那该怎么办?不需要相同图像的尺寸要求?)。

只需将图片放入div中,然后使用margin: auto;(或text-align: center,现在就不能说)将其设置为横向居中的样式。
要将它垂直居中,您可以使用此jQuery函数:

(function ($) {
$.fn.vAlign = function(){
    return this.each(function(i){
    var ah = $(this).height();
    var ph = $(this).parent().height();
    var mh = Math.ceil((ph-ah) / 2);
    $(this).css('margin-top', mh);
    });
};

然后:$('#image').vAlign()