我一直在尝试通过 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());
}
}
}
最佳答案
// 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++;
}
关于php - 使用 codeigniter 为图像添加水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43112203/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h