我正在使用 codeigniter 图像类将图像调整大小并将其裁剪为特定大小。如果源图像的纵横比不同,无法将其设置为我想要的大小,我会填充图像添加空间,直到它适合为止。问题是我需要将此颜色设置为白色。测试图像是黑色的,我怀疑它选择的颜色是任意的。我认为核心图像库需要修改,但会喜欢一种非黑客方式。请帮忙。这是我的代码供参考。显然我正在向它传递参数,它应该很明显。
copy($row['photo'],$new_filename);
$this->CI->load->library('image_lib');
if ($row['width']>$device_params['width'] || $row['height']>$device_params['height']) {
$config['image_library'] = 'gd2';
$config['source_image'] = $new_filename;
$config['maintain_ratio'] = TRUE;
$config['quality'] = '40%';
if ($scale) {
$device_params['height'] = ($scale / 100) * $device_params['height'];
$device_params['width'] = ($scale / 100) * $device_params['width'];
}
$config['height'] = $device_params['height'];
$config['width'] = $device_params['width'];
$config['master_dim'] = 'height';
if ($row['width']<$row['height']) {
$config['rotation_angle'] = '270';
$config['master_dim'] = 'height';
if (strtolower(substr($new_filename,-3))=='jpg') {
$this->CI->image_lib->rotate();
}
} else {
}
$this->CI->image_lib->initialize($config);
$this->CI->image_lib->resize();
$this->CI->image_lib->clear();
unset($config);
}
if ($device_params['crop']) {
$config['image_library'] = 'gd2';
//$config['library_path'] = '/usr/bin/';
$config['source_image'] = $new_filename;
$size = getimagesize($new_filename);
//var_dump($size);
$x_axis = $size[0]/2-($device_params['width']/2);
$y_axis = $size[1]/2-($device_params['height']/2);
//echo $x_axis.'-'.$y_axis;
//die();
$config['maintain_ratio'] = FALSE;
$config['height'] = $device_params['height'];
$config['width'] = $device_params['width'];
$config['y_axis'] = $y_axis;
$config['x_axis'] = $x_axis;
$config['master_dim'] = 'auto';
$this->CI->image_lib->initialize($config);
if ( ! $this->CI->image_lib->crop())
{
$data['output']['status'] = 0;
$data['output']['message'] = $this->CI->image_lib->display_errors();
}
}