我一直试图通过codeigniter为图像添加水印,下面是我的代码,图像,调整大小和拇指都在工作,但水印似乎无法正常工作。我实际上在这个论坛上看了类似的帖子,但似乎对我不起作用,也许有些事情做得不对
class Upload extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("user_model");
/*if($this->user_model->login_status() === FALSE)
{
redirect('main');
}*/
$this->load->helper("file");
$this->load->library("greetings");
$this->load->helper("error_message");
$this->load->helper("custom_date_helper");
//$this->greetings->show_greetings();
}
public function index()
{
if($this->user_model->login_status() === FALSE)
{
$msg="You need to login before you can submit entry!";
echo $msg;
}
else
{
$contest_id=strip_tags($_GET["contest_id"]);
$data["contest_id"]=$contest_id;
$str = $this->load->view('upload/upload_form',$data);
echo trim($str);
}
}
/**
* Uploads a new contest entry
*/
public function do_upload()
{
$contest_id=$this->input->post("contest_id");
$config["file_name"]=$this->session->userdata("username").'_'.time();
$config["upload_path"]= $this->config->item("custom_upload_path");
$config["allowed_types"]="jpeg|jpg|gif|png";
$config["max_size"]="8888";
$this->load->library("upload",$config);
$field_name="file";
$title=$this->input->post("title");
$description=$this->input->post("description");
$this->load->library("form_validation");
$this->form_validation->set_rules("terms","Terms","callback_accept_terms");
$this->form_validation->set_rules("title","Title","required");
$this->form_validation->set_rules("description","Description","required");
$this->form_validation->set_error_delimiters("<div class='error'>","</div>");
$date=new DateTime();
$date_uploaded=$date->format("Y-m-d H:i:s");
if(!$this->upload->do_upload($field_name) OR $this->form_validation->run() === FALSE)
{
if(empty($_POST["submit"]))
{
$data["error"]='';
$data["contest_id"]=$this->input->post("contest_id");
$this->load->view("upload/upload_form",$data);
}
else
{
$data["error"]=$this->upload->display_errors();
$data["contest_id"]=$this->input->post("contest_id");
$this->load->view("upload/upload_form_1",$data);
}
}
else
{
$this->load->model("contest_model");
$file_info=$this->upload->data();
$username=$this->session->userdata("username");
$uploaded_by=$this->contest_model->get_user($username);
$path=$file_info["file_name"];
$this->watermark($path);
$this->_create_thumb($path);
$this->resize($path);
$info=array(
'contest_id'=>$contest_id,
'uploaded_by'=>$uploaded_by,
'path'=>$path,
'title'=>$title,
'description'=>$description,
'date_uploaded'=>$date_uploaded,
'status'=>0,
);
if($this->contest_model->already_submitted($contest_id,$uploaded_by) === FALSE)
{
if($this->contest_model->add_submission($info) === TRUE)
{
$msg="Your submission was recorded and awaiting for admin approval!";
}
else
{
$msg="There was something wrong! Try later!";
}
}
else
{
$msg="You have already submitted your entry for this contest! <span style='color:red;'> You can enter one photo per contest.</span>";
}
$this->session->set_flashdata( 'message', array( 'title' => '', 'content' => $msg, 'type' => 'message' ));
redirect("user/profile/");
}
}
public function accept_terms($terms)
{
if($terms == "on")
{
return TRUE;
}
else
{
$this->form_validation->set_message("accept_terms","You must agree with this");
return FALSE;
}
}
public function validate_breed($breed_type)
{
if($breed_type == "none")
{
$this->form_validation->set_message("validate_breed","You must select a breed");
return FALSE;
}
else
{
return TRUE;
}
}
private function _create_thumb($image_name)
{
$this->load->library("image_lib");
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/'.$image_name;
$config['new_image'] = 'uploads/'.$image_name;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 135;
$config['height'] = 135;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
function change_file_name($image)
{
$img_arr=pathinfo($image);
$filename=$img_arr["filename"];
$extension=$img_arr["extension"];
$thumb=$filename."-new.".$extension;
return $thumb;
}
public function test()
{
ini_set("memory_limit","128M");
$this->load->helper("directory");
$map=directory_map("uploads");
$i=0;
foreach($map as $file)
{
if(!strstr($file,"thumb") AND !strstr($file,"new"))
{
if(!in_array($this->change_file_name($file), $map))
{
$i++;
echo $file;
$this->resize($file);
echo "<br/>";
}
}
}
echo $i;
echo "<br/>";
echo count($map);
//array_map(array($this, "_create_thumb"), $map);
}
private function resize($file)
{
$this->load->library("image_lib");
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/'.$file;
$config['new_image'] = 'uploads/'.$this->change_file_name("$file");
$config['create_thumb'] = false;
$config['maintain_ratio'] = true;
$config['dynamic_output'] = false;
$config['quality'] = '100%';
$config['width'] = 300;
$config['height'] = 1000;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
//watermark function
function watermark($image_name)
{
$this->load->library("image_lib");
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/'.$image_name;
$config['wm_text'] = 'Copyright 2006 - John Doe';
$config['wm_type'] = 'text';
$config['wm_font_path'] = './system/fonts/texb.ttf';
$config['wm_font_size'] = '16';
$config['wm_font_color'] = 'ffffff';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_padding'] = '20';
$this->image_lib->clear();
$this->image_lib->initialize($config);
//$this->image_lib->clear();
if ( $this->image_lib->watermark())
{
echo $this->image_lib->display_errors();
//var_dump(gd_info());
}
}
}
答案 0 :(得分:1)
// Define it in configuration file. And call it
// About configurations: https://codeigniter.com/user_guide/libraries/config.html
$wm_font_size = 16; // Watermark height (font size) contains 16% of image
$this->load->library('upload');
$this->load->library('image_lib');
$config['image_library'] = 'GD2';
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['file']['name'] = $files['file']['name'][$i];
$_FILES['file']['type'] = $files['file']['type'][$i];
$_FILES['file']['tmp_name'] = $files['file']['tmp_name'][$i];
$_FILES['file']['error'] = $files['file']['error'][$i];
$_FILES['file']['size'] = $files['file']['size'][$i];
$config['source_image'] = $files['file']['tmp_name'][$i];
$config['wm_text'] = 'Copyright example.com';
$config['wm_type'] = 'text';
$config['wm_font_size'] = ceil($files['file'][image_height][$i]/100*wm_font_size);
$config['wm_vrt_alignment'] = 'middle';
$config['wm_hor_alignment'] = 'center';
$this->image_lib->initialize($config);
$this->image_lib->watermark();
$config['source_image'] = $files['file']['tmp_name'][$i];
$config['wm_overlay_path'] = './application/assets/images/example.png';
$config['wm_type'] = 'overlay';
$config['width'] = '50';
$config['height'] = '50';
$config['padding'] = '50';
$config['wm_opacity'] = '100';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'right';
$config['wm_vrt_offset'] = '100';
$this->image_lib->initialize($config);
$this->image_lib->watermark();
if (!$this->upload->do_upload("file")) {
$errors++;
}