PHP
·
发表于 5年以前
·
阅读量:8296
±¾ÎÄʵÀý½²ÊoÁËCI¿o¼Üa×°µÄ³£ÓÃͼÏñ´¦Ài½¨¡£ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º
Æaʵ΢ÐÅÊÖ»u¶ËÉÏͼʱ£¬Áбiͼ×iºÃÊÇËoÂÔͼ£¬½ÚÊ¡Á÷Á¿£¬Õa²»£¬ÓÖ±»Òƶ¯¿ÓÁËÒ»°Ñ£¬»°ÑÇ(C)Ò»Ö¾ÍÍ£»u£¬Á÷Á¿Ç*µ½90¿e²ÅÍ£»u£¬ÎÒÒ²ÊÇ×iÁË¡£¡£¡£
²»Ëµ*Ï»°ÁË£¬ÏÂÃaeÊÇÓÃCI µÄÄÚÖô¦ÀiͼÏñµÄ¿aдµÄ£¬Ð¡µÜ²»²Å£¬ÒÅÂ(C)Ö®´¦¾´ÇeÖ¸³o£¬Ð»Ð»¡£
/**
* Éu³ÉËoÂÔͼ
* @param $path ÔͼµÄ±¾µØÂ*¾¶
* @return null ´´½¨Ò»¸o Ôͼ_thumb.À(C)Õ¹Ãu µÄÎļþ
*
*/
public function dealthumb($path){
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['create_thumb'] = TRUE;
//Éu³ÉµÄËoÂÔͼ½«ÔÚ±£³Ö×ݺa±ÈÀý ÔÚ¿i¶ÈºÍ¸ß¶ÈÉϽӽuËuÉe¶¨µÄwidthºÍheight
$config['maintain_ratio'] = TRUE;
$config['width'] = 80;
$config['height'] = 80;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
}
/*
* ´¦ÀiͼÏñÐýת
*/
public function transroate($path,$imgpath){
$this->load->library('image_lib');
//(±ØÐe)ÉeÖÃͼÏñ¿a
$config['image_library'] = 'gd2';
$newname = time().'_rote.jpg';
//ÉeÖÃͼÏñµÄÄ¿±eÃu/Â*¾¶
$config['new_image'] =$imgpath.$newname;
//(±ØÐe)ÉeÖÃÔʼͼÏñµÄÃu×Ö/Â*¾¶
$config['source_image'] = $path;
//¾o¶¨ÐÂͼÏñµÄÉu³ÉÊÇҪдÈeÓ²ÅÌ»¹ÊǶ¯Ì¬µÄ´aeÔÚ
$config['dynamic_output'] = FALSE;
//ÉeÖÃͼÏñµÄÆ*ÖÊ¡£Æ*ÖÊÔ½¸ß£¬Í¼ÏñÎļþÔ½´o
$config['quality'] = '90%';
//ÓÐ5¸oÐýתѡÏi ÄaeʱÕe90 180 270 ¶È vrt ÊuÏo*ת hor ºaÏo*ת
$config['rotation_angle'] = 'vrt';
$this->image_lib->initialize($config);
if(@$this->image_lib->rotate()){
$this->image_lib->clear();
return $config['new_image'];
}else{
$this->image_lib->clear();
return '';
}
}
/**
* ´¦ÀiͼÏñˮӡ
*/
public function overlay($path,$imgpath){
$this->load->library('image_lib');
$newname = time().'_over.jpg';
//ÉeÖÃÐÂͼÏñÃu³Æ
$config['new_image'] =$imgpath.$newname;
//µ÷ÓÃphp gd¿a »aeͼ
$config['image_library'] = 'gd2';
//ԴͼÏñ ±¾µØµØÖ*
$config['source_image'] = $path;
//¸²¸ÇÎÄ×Ö
$config['wm_text'] = 'Copyright 2015 - Friker';
//¸²¸ÇÀaÐÍ ÎÄ×Ö/ͼÏñ
$config['wm_type'] = 'text';
//ÎÄ×Ö×ÖÌaÀaÐÍ
//$config['wm_font_path'] = 'C:\Windows\Fonts\vrinda.ttf';
//×ÖÌa´oС
$config['wm_font_size'] = '16';
//×ÖÌaÑÕÉ«
$config['wm_font_color'] = 'ff0000';
//´¹Ö±*½Ïo¾aÀe¶¥¶Ë¾aÀe
$config['wm_vrt_alignment'] = '20';
//ˮƽ*½Ïo¾aÀe×o¶Ë¾aÀe
$config['wm_hor_alignment'] = 'center';
//padding
$config['wm_padding'] = '20';
$this->image_lib->initialize($config);
if($this->image_lib->watermark()){
$this->image_lib->clear();
return $config['new_image'];
}else{
$this->image_lib->clear();
return '';
}
}
/**
* ´¦ÀiͼƬÉÏ´«
* ÎļþÉÏ´«Àa ͨ¹ýǰ̨ ÉÏ´«Îļþ
*/
public function uploadfile(){
//ÎļþÉÏ´«²¿*Ö
// ´¦ÀiÎļþ
// $data = '';
$this->load->helper('url');
$formpic = key($_FILES);
//Îļþ´¦Ài²¿*Ö
if(false === empty($_FILES[$formpic]['tmp_name'])){
//ÉeÖÃÎļþÉÏ´«µÄÂ*¾¶
$upload['upload_path'] = "./public/img/";
//ÏÞÖÆÎļþÉÏ´«µÄÀaÐÍ
$upload['allowed_types'] = 'jpeg|jpg|gif|png';
//ÏÞÖÆÎļþÉÏ´«µÄ´oС
$upload['max_size'] = 2048;
//ÉeÖÃÎļþÉÏ´«µÄÂ*¾¶
$upload['file_name'] = date('YmdHis', time()).rand(10000, 99999);
//¼ÓÔØÎļþÉÏ´«ÅaÖÃÐÅÏ¢
$this->load->library('upload', $upload);
//´¦ÀiÎļþÉÏ´«
$this->upload->do_upload($formpic);
//*µ»ØÎļþÉÏ´«ÐÅÏ¢
$image = $this->upload->data();
/*
'file_name' => string '2015071702051718388.jpg' (length=23)
'file_type' => string 'image/jpeg' (length=10)
'file_path' => string 'E:/wamp/www/testci/public/img/' (length=30)
'full_path' => string 'E:/wamp/www/testci/public/img/2015071702051718388.jpg' (length=53)
'raw_name' => string '2015071702051718388' (length=19)
'orig_name' => string '2015071702051718388.jpg' (length=23)
'client_name' => string 'u=415761610,1548338330&fm;=116&gp;=0.jpg' (length=38)
'file_ext' => string '.jpg' (length=4)
'file_size' => float 3.74
'is_image' => boolean true
'image_width' => int 146
'image_height' => int 220
'image_type' => string 'jpeg' (length=4)
'image_size_str' => string 'width="146" height="220"' (length=24)
*/
//var_dump($image);
//*µ»ØÎļþÉÏ´«Ãu×Ö
$data = $image['file_name'];
$this->dealthumb($image['full_path']);
$this->overlay($image['full_path'],$image['file_path']);
$this->transroate($image['full_path'],$image['file_path']);//
$thumbdata = '';
//Éu³ÉËoÂÔͼÃu³Æ
$pos = strripos($image['file_name'], ".");
$newname = substr($image['file_name'], 0,$pos)."_thumb".substr($image['file_name'], $pos);
if(file_exists($image['file_path'].$newname)){
$thumbdata = $newname;
}
}
//$dirroot = $_SERVER['DOCUMENT_ROOT'];
//$this->dealthumb($dirroot."/public/img/".$data);
//ÉÏ´«Ê§°Ü
if(!$data){
echo json_encode(array('status'=>0,'msg'=>"ÉÏ´«Ê§°Ü£¡"));
}else{
//ÉÏ´«³É¹¦
echo json_encode(array(
'name'=>$data,
'pic'=>base_url()."public/img/".$data,
'picthumb'=>$thumbdata == '' ?$data:$thumbdata
));
}
}
ÏÂÃaeÊÇÇ°¶ËµÄ»u±¾html´uÂe£º
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/public/stylesheets/bootstrap.min.css" />
<link rel="stylesheet" href="/public/stylesheets/bootstrap-responsive.min.css" />
<link rel="stylesheet" href="/public/stylesheets/matrix-style.css" />
<link rel="stylesheet" href="/public/stylesheets/matrix-media.css" />
<script type="text/javascript" src="/public/javascripts/jquery.min.js"></script>
<script type="text/javascript" src="/public/javascripts/jquery.form.js"></script>
<script type="text/javascript" src="/public/javascripts/jquery.validate.js"></script>
<style type="text/css">
body{background:#eeeeee; margin:0px;}
</style>
</head>
<body>
<div class="control-group">
<label class="control-label"> *ÖÏilogo£º </label>
<div class="controls">
<input type="file" name="sharepic" id="sharepic"/>
<input type="hidden" name="act_sharepic" value="" id="act_sharepic"/>(<sapn class="fred">×i¼Ñ´oСΪ 80 X 80 ÏñËØ</sapn>)
<p style="margin:20px 0;"><img src="/public/img/default.png" alt="" id="sharepic_img"></p>
</div>
</div>
<script type="text/javascript">
$(function () {
/*****************ͼƬÉÏ´«²¿*Ö¿ªÊ¼ *******************/
var act = "<form class='myupload' action='"+"<?php echo site_url('mytest/uploadfile');?>"+"' method='post' enctype='multipart/form-data'></form>";
$("#sharepic").change(function(){
$(this).wrap(act);
$(this).parent(".myupload").ajaxSubmit({
dataType: 'json',
success: function(data) {
var src = data.pic;
//¸u¸ÄÔ¤ÀÀͼÏñµØÖ*
$('#sharepic_img').attr("src",src);
$('#act_sharepic').val(data.name);
$('#sharepic').unwrap();
},
error:function(xhr){
alert(JSON.parse(xhr));
}
});
});
})
</script>
</body>
</html>
¸u¶a¹ØÓÚCodeIgniterÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶codeigniterÈeÃŽ̡̳¡¢¡¶CI(CodeIgniter)¿o¼Ü½ø½×½Ì³Ì¡¡¢¡¶phpÓÅÐa¿ª*¢¿o¼Ü×ܽa¡¡¢¡¶ThinkPHPÈeÃŽ̡̳¡¢¡¶ThinkPHP³£Óý¨×ܽa¡¡¢¡¶Zend FrameWork¿o¼ÜÈeÃŽ̡̳¡¢¡¶phpÃaeÏo¶ÔÏo³ÌÐoÉe¼ÆÈeÃŽ̡̳¡¢¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡*
Ï£Íu±¾ÎÄËuÊo¶Ô´o¼Ò»uÓÚCodeIgniter¿o¼ÜµÄPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£