这就是我这样做的方式:
public function saveImage($web_id, $user_id, $photo, $index = -1, $path=false, $crop = true, $crop_x = 0, $crop_y = 0, $width = 0, $height = 0)
{
/* Image upload */
$this->ci->load->helper("string");
$this->ci->load->library('upload');
if(!$path)
$path = getcwd().'/assets/img/locations/'.$user_id."/".$web_id;
else
$path = getcwd().$path;
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = random_string('alpha', 39);
$config['overwrite'] = true;
$this->ci->upload->initialize($config);
if (!$this->ci->upload->do_upload($photo, $index))
{
/*echo "<br>path: ".$config['upload_path']."<br>";
echo "user_id: ".$user_id." web_id: ".$web_id." error: ". $this->ci->upload->display_errors();
die;*/
$photo = '';
}
else{
$image_data = $this->ci->upload->data();
$photo = $image_data['file_name'];
if($crop)
{
$this->ci->load->library('image_lib');
$config = array(
'image_library' => 'gd',
'source_image' => $image_data['full_path'],
'new_image' => $path,
'x_axis' => $crop_x,
'y_axis' => $crop_y,
'width' => $width,
'height' => $height,
'overwrite' => true,
'maintain_ratio' => false
);
$this->ci->image_lib->initialize($config);
if ( ! $this->ci->image_lib->crop())
{
echo $this->ci->image_lib->display_errors();
die;
}
}
}
return $photo;
}
有很多东西你不需要使用,但你可以用它作为基础。
我希望它对你有帮助。