用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4481|回复: 3
收起左侧

[初级] 详细介绍使用CI图片类来进行图片尺寸的修改和添加水印

[复制链接]
发表于 2012-2-27 15:16:58 | 显示全部楼层 |阅读模式
CI有一个很方便的图片类可以对将要上传图片进行尺寸的修改和添加水印
代码如下:
1:upload.php控制器
   <?php
class Upload extends Controller {
function __construct()
{
  parent::__construct();
  $this->load->helper(array('form', 'url'));
}
function index()
{
  $this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '100';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';
  $this->load->library('upload', $config);
  if ( ! $this->upload->do_upload())
  {
   $error = array('error' => $this->upload->display_errors());
   $this->load->view('upload_form', $error);
  }
  else
  {
   $data = array('upload_data' => $this->upload->data());
   $this->load->view('upload_success', $data);
  }
}
/**
  * CI的图像类
  *
  */
function do_image()
{
   $this->load->library('image_lib');
   $config['image_library'] = 'GD';
   $config['source_image'] = "uploads/Sunset.jpg";
   $config['new_image'] = 'uploads/new_image/Sunset.jpg';//制定生成的新文件的吗,路径和名称 ;这句代码不设置的话,生产的新图片会覆盖原来的图片
   $config['create_thumb'] = TRUE;         //生成缩略图 。如果设置成false的时候,可以生成新的尺寸的那张图片;当为ture的时候,只会生成缩略图
   $config['width'] = 140;
   $config['height'] = 104;
   $this->image_lib->initialize($config);
   if(!$this->image_lib->resize())
   {echo "failed";}
   else{echo 'success!';}
}
/**为图片添加水印
*/
function wm_image()
{
   header("Content-type:text/html;charset=utf-8;");
   $this->load->library('image_lib');
   $config['source_image'] = 'uploads/Sunset.jpg';
   $config['wm_text'] = 'www.justdoit.com';
   $config['wm_type'] = 'text';
   $this->image_lib->initialize($config);
   if(!$this->image_lib->watermark())
   {echo '添加水印失败了';}
   else {echo '添加水印成功了';}
}
}
?>
2:上传成功的页面视图upload_success.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
3:创建上传表单视图upload_form.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>

发表于 2014-1-18 00:54:31 | 显示全部楼层
不成功。总失败
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_image');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
发表于 2014-1-18 00:54:57 | 显示全部楼层
我想生成缩略图
发表于 2015-1-12 13:35:47 | 显示全部楼层
表示一样 还是弄不成缩略图

本版积分规则