我正在使用 Codeigniter 的图像处理库将上传的图像重新调整为三种尺寸,小、正常和大。
重新调整大小效果很好。但是,如果我正在调整垂直图像的大小,则库会旋转图像以使其水平。
这些是我的配置设置:
$this->resize_config['image_library'] = 'gd2';
$this->resize_config['source_image'] = $this->file_data['full_path'];
$this->resize_config['maintain_ratio'] = TRUE;
// These change based on the type (small, normal, large)
$this->resize_config['new_image'] = './uploads/large/'.$this->new_file_name.'.jpg';
$this->resize_config['width'] = 432;
$this->resize_config['height'] = 288;
我没有设置 master_dim 属性,因为它默认设置为 auto,这是我想要的。
我的假设是该库将采用垂直图像,看到高度大于宽度并适本地转换高度/宽度配置,以使图像保持垂直。
正在发生的事情(显然)是库在图像垂直时旋转图像并根据配置调整其大小。
这是我必须进行实际调整大小的代码:
log_message('debug', 'attempting '.$size.' photo resize');
$this->CI->load->library('image_lib');
$this->CI->image_lib->initialize($this->resize_config);
if ($this->CI->image_lib->resize())
{
$return_value = TRUE;
log_message('debug', $size.' photo resize successful');
}
else
{
$this->errors[] = $this->CI->image_lib->display_errors();
log_message('debug', $size.' photo resize failed');
}
$this->CI->image_lib->clear();
return $return_value;
编辑
我认为问题可能来自上传库。当我从上传中获取 image_height 和 image_width 时,即使我上传了垂直图像,宽度似乎也更大。
这是我用来上传照片的代码的一部分:
$this->upload_config['allowed_types'] = 'jpg|jpeg';
$this->upload_config['max_size'] = '2000';
$this->upload_config['max_width'] = '0';
$this->upload_config['max_height'] = '0';
$this->upload_config['upload_path'] = './uploads/working/';
$this->CI->load->library('upload', $this->upload_config);
if ($this->CI->upload->do_upload($this->posted_file))
{
$this->file_data = $this->CI->upload->data();
$return_value = TRUE;
log_message('debug', 'upload successful');
}
我添加了一些日志记录来检查值:
$this->is_vertical = $this->file_data['image_height'] > $this->file_data['image_width'];
log_message('debug', 'image height:'.$this->file_data['image_height']);
log_message('debug', 'image width:'.$this->file_data['image_width']);
if ($this->is_vertical)
{
$this->resize_config['master_dim'] = 'height';
}
else
{
$this->resize_config['master_dim'] = 'width';
}
log_message('debug', 'master_dim setting:'.$this->resize_config['master_dim']);
这些是日志的结果:
调试 - 2010-03-16 18:35:06 --> 图像高度:1536
调试 - 2010-03-16 18:35:06 --> 图像宽度:2048
调试 - 2010-03-16 18:35:06 --> master_dim 设置:宽度
在 Photoshop 中查看图像,这些是尺寸:
高度:2048
宽度:1536
任何人都知道是什么可能导致上传库这样做?
最佳答案
我从来没有使用过这个库,但是在阅读了文档之后,我想知道 master_dim 属性是否有帮助。如果您将其设置为垂直图像的“高度”,可能会使它们保持正确的向上。您可以通过条件解析每个图像以查看图像是否垂直对齐,然后仅在需要时设置此属性。
我的另一个想法是关于 maintain_ratio 属性。文档说,将此设置为“TRUE”时,它将在保持纵横比的同时尽可能接近目标值。我想知道它是否认为旋转图像可以更准确地保持这个比例?作为实验,尝试将此值设置为垂直图像的“FALSE”。
关于Codeigniter 图像处理类在调整大小期间旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2452088/