2014-10-11 126 views
1

我一直在尝试上传和调整图像大小。上传似乎终于有效,但不是调整大小。Codeigniter文件调整上传

在我的构造函数中我加载库

$this->load->library('image_lib'); 

,然后在我的功能我打电话调整大小并上传

$this->require_auth();  
    $img = $this->input->post('photo1'); 

    $config['upload_path'] = './HTML/files/'; 
    $config['file_name'] = 'photo1.jpg'; 
    $config['file_path'] = './HTML/files/photo1.jpg'; 
    $config['max-size'] = 2000; 
    $config['image_library'] = 'gd2'; 
    $config['source_image'] = $config['file_path']; 
    $config['create_thumb'] = TRUE; 
    $config['maintain_ratio'] = FALSE; 
    $config['width'] = 549; 
    $config['height'] = 549; 
    $config['overwrite'] = TRUE; 
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
    $config['new_image'] = $config['file_path']; 
    $this->load->library('image_lib', $config); 
    $this->image_lib->resize(); 
    $this->load->library('upload', $config);     


    if (! $this->upload->do_upload('photo1')){   
     $error = array('error' => $this->upload->display_errors()); 
     var_dump($error); 
    }else{ 
     $this->blog_model->editServiceImg1($config['file_path']); 
     redirect('/blog'); 
    } 

我也试过这种

$this->require_auth();  
    $img = $this->input->post('service_photo1'); 

    $config['upload_path'] = './HTML/files/'; 
    $config['file_name'] = 'service_photo1.jpg'; 
    $config['file_path'] = './HTML/files/service_photo1.jpg'; 
    $config['max-size'] = 2000; 
    $config['overwrite'] = TRUE; 
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
    $this->load->library('upload', $config);     


    if (! $this->upload->do_upload('service_photo1')){   
     $error = array('error' => $this->upload->display_errors()); 
     var_dump($error); 
    }else{ 
     $config['image_library'] = 'gd2'; 
     $config['source_image'] = $config['file_path']; 
     $config['create_thumb'] = TRUE; 
     $config['maintain_ratio'] = FALSE; 
     $config['width'] = 549; 
     $config['height'] = 549; 
     $config['new_image'] = $config['file_path']; 
     $this->image_lib->initialize($config); 
     $this->image_lib->resize(); 


     $this->blog_model->editServiceImg1($config['file_path']); 
     redirect('/blog'); 
    } 

有什么我失踪?我已经尝试在初始上传后调整大小,这似乎也无能为力。任何帮助将非常感激。

+0

您是否尝试在上传图片之前调整大小? – Xrymz 2014-10-11 14:55:23

+0

@LionKing不幸的是,它仍然不想工作。 – zazvorniki 2014-10-11 14:57:29

+0

@Xrymz,是的,那正是我想要完成的。 – zazvorniki 2014-10-11 14:57:54

回答

0

我通过这个代码,去了很多次,终于解决了这个问题。看来,当你在你的代码有

$config['create_thumb'] = TRUE; 

它会创建与调整比例的缩略图,如果你不告诉它去哪里它会隐藏的文件夹。所以调整大小工作,只是不正确的形象。

要解决这个问题,我改变了上面的。

$config['create_thumb'] = FALSE; 
0
$this->load->library('image_lib'); 
$config['image_library'] = 'gd2'; 
$config['allowed_types'] = 'gif|jpg|png|jpeg'; 
$config['upload_path'] = './HTML/files/'; 
$config['file_path'] = './HTML/files/photo1.jpg'; 
$config['source_image'] = $config['file_path']; 
$config['create_thumb'] = TRUE; 
$config['maintain_ratio'] = TRUE; 
$config['width']  = 75; 
$config['height'] = 50; 

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

如果上述代码不起作用,请检查文件夹上传图片的权限。

你也可以给777权限的文件夹中的下列方式: -

sudo chmod -R 777 path

解决方案一定会帮助你

+0

我其实发现它有什么问题,而不是权限。我可以在该文件夹中写入文件,但他们只是不调整大小。这个问题碰巧是我把拇指变成了真的。所以它正在调整大小,并在应用程序中的其他位置创建另一个文件,而不是在我需要的文件夹或正确的文件中。只要我把拇指设置为假,那么它就可以很好地工作。 – zazvorniki 2014-10-14 13:55:35