php有关ci框架中图像水印的博客,php – 使用codeigniter为图像添加水印

我一直试图通过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("

","
");

$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! You can enter one photo per contest.";

}

$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 "
";

}

}

}

echo $i;

echo "
";

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());

}

}

}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值