我正在使用codeigniter附带的上传类:
$config['upload_path'] = getcwd() . '/public/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
echo $error = array('error' => $this->upload->display_errors());
}
else
{
echo $data = array('upload_data' => $this->upload->data());
}
上传文件很方便,但现在我想从图像中修剪所有额外的空白区域。我查看了图像处理类,但它似乎没有这样做。所以我环顾四周找到了Crop whitespace from image in PHP。我不确定如何把两者放在一起。有什么想法吗?
答案 0 :(得分:1)
你是对的,图像处理类不支持这样做。
但是,您可以扩展库(http://codeigniter.com/user_guide/general/creating_libraries.html的底部)并根据您找到的链接中的代码添加新方法。
在这里,我在工作中感到无聊,扩展了CI的图像处理库并添加了这个方法:
public function trim_whitespace($color = 'FFFFFF')
{
//load the image
$img = $this->image_create_gd();
//find the size of the borders
$b_top = 0;
$b_btm = 0;
$b_lft = 0;
$b_rt = 0;
//top
for(; $b_top < imagesy($img); ++$b_top) {
for($x = 0; $x < imagesx($img); ++$x) {
if(imagecolorat($img, $x, $b_top) != '0x'.$color) {
break 2; //out of the 'top' loop
}
}
}
//bottom
for(; $b_btm < imagesy($img); ++$b_btm) {
for($x = 0; $x < imagesx($img); ++$x) {
if(imagecolorat($img, $x, imagesy($img) - $b_btm-1) != '0x'.$color) {
break 2; //out of the 'bottom' loop
}
}
}
//left
for(; $b_lft < imagesx($img); ++$b_lft) {
for($y = 0; $y < imagesy($img); ++$y) {
if(imagecolorat($img, $b_lft, $y) != '0x'.$color) {
break 2; //out of the 'left' loop
}
}
}
//right
for(; $b_rt < imagesx($img); ++$b_rt) {
for($y = 0; $y < imagesy($img); ++$y) {
if(imagecolorat($img, imagesx($img) - $b_rt-1, $y) != '0x'.$color) {
break 2; //out of the 'right' loop
}
}
}
//copy the contents, excluding the border
$newimg = imagecreatetruecolor(
imagesx($img)-($b_lft+$b_rt), imagesy($img)-($b_top+$b_btm));
imagecopy($newimg, $img, 0, 0, $b_lft, $b_top, imagesx($newimg), imagesy($newimg));
// Output the image
if ($this->dynamic_output == TRUE)
{
$this->image_display_gd($newimg);
}
else
{
// Or save it
if ( ! $this->image_save_gd($newimg))
{
return FALSE;
}
}
}
用法:
// load extended image lib
$this->load->library('image_lib');
// configure image lib
$config['image_library'] = 'gd2';
$config['source_image'] = 'path/to/source.img';
$config['new_image'] = 'path/to/output.img';
$this->image_lib->initialize($config);
$this->image_lib->trim_whitespace('38ff7e'); // set colour to trim, defaults to white (ffffff)
$this->image_lib->clear();
值得注意的是,即使它会起作用,你也不应该将它用作dynamic_output,在保存时修剪它,否则它只会减慢一切。此外,它只是寻找1个颜色值(虽然,这是你发布的代码的限制,可能有更好的功能,所以如果它是一个压缩的jpg你可能无法获得所有的空白。< / p>
答案 1 :(得分:0)
要使用ImageMagick,您应该将该函数添加到image_lib.php:
public function trimimg()
{
$protocol = 'image_process_'.$this->image_library;
return $this->$protocol('trimimg');
}
然后添加到函数
public function image_process_imagemagick($action = 'resize')
{
// Do we have a vaild library path?
if ($this->library_path === '')
{
$this->set_error('imglib_libpath_invalid');
return FALSE;
}
if ( ! preg_match('/convert$/i', $this->library_path))
{
$this->library_path = rtrim($this->library_path, '/').'/convert';
}
// Execute the command
$cmd = $this->library_path.' -quality '.$this->quality;
if ($action === 'crop')
{
$cmd .= ' -crop '.$this->width.'x'.$this->height.'+'.$this->x_axis.'+'.$this->y_axis;
}
elseif ($action === 'rotate')
{
$cmd .= ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
? ' -flop'
: ' -rotate '.$this->rotation_angle;
}
elseif ($action === 'trimimg')
{
$cmd .= ' -trim';
}
最后一个。
然后
$this->image_lib->trimimg())