feat: init project codebase
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* 上传及图像处理
|
||||
*/
|
||||
class Uploads
|
||||
{
|
||||
|
||||
const DIR = 'uploads';
|
||||
const DIR_PET_IMG = 'pet_img';
|
||||
const DIR_PER_CERT_IMG = 'pet_cert_img';
|
||||
const DIR_PER_CERT_ZIP = 'pet_cert_zip';
|
||||
|
||||
public function get_upload_path(){
|
||||
return FCPATH.self::DIR.DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传宠物证书base64
|
||||
*/
|
||||
public function save_cert_img_base64($content,$device_id,$filename)
|
||||
{
|
||||
$device_id = strtoupper($device_id);
|
||||
$upload_path = $this->get_upload_path();
|
||||
$pet_img_path = Uploads::DIR_PER_CERT_IMG.DIRECTORY_SEPARATOR.$device_id.DIRECTORY_SEPARATOR;
|
||||
$type = $this->base64_image_save(
|
||||
$content,
|
||||
$upload_path.$pet_img_path,
|
||||
$filename
|
||||
);
|
||||
if(!$type){
|
||||
return false;
|
||||
}
|
||||
//png转为jpg,降低文件大小
|
||||
$img = imagecreatefrompng($upload_path.$pet_img_path.$filename.'.'.$type);
|
||||
if($img){
|
||||
imagejpeg($img, $upload_path.$pet_img_path.$filename.'.jpg',75);
|
||||
imagedestroy($img);
|
||||
unlink($upload_path.$pet_img_path.$filename.'.'.$type);
|
||||
}
|
||||
return $filename.'.jpg';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 上传宠物头像
|
||||
*/
|
||||
public function save_pet_img_base64($content,$device_id)
|
||||
{
|
||||
$device_id = strtoupper($device_id);
|
||||
$upload_path = $this->get_upload_path();
|
||||
$pet_img_path = Uploads::DIR_PET_IMG.DIRECTORY_SEPARATOR;
|
||||
$type = $this->base64_image_save(
|
||||
$content,
|
||||
$upload_path.$pet_img_path,
|
||||
$device_id.'_ori'
|
||||
);
|
||||
if(!$type){
|
||||
return false;
|
||||
}
|
||||
$source = $pet_img_path.$device_id.'_ori.'.$type;
|
||||
$target = $pet_img_path.$device_id.'_300.'.$type;
|
||||
$ret = $this->image_center_crop($upload_path.$source, 300, 300, $upload_path.$target);
|
||||
if(!$ret){
|
||||
return $source;
|
||||
}
|
||||
return $target;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
*/
|
||||
public function upload_img($arr)
|
||||
{
|
||||
$list = array();
|
||||
foreach ($arr as $k => $v) {
|
||||
|
||||
$name = $v['name'];
|
||||
$type = strtolower(substr($name, strrpos($name, '.') + 1)); // 得到文件类型,并且都转化成小写
|
||||
$allow_type = array(
|
||||
'jpg',
|
||||
'jpeg',
|
||||
'gif',
|
||||
'png',
|
||||
'mp4',
|
||||
); // 定义允许上传的类型
|
||||
// 判断文件类型是否被允许上传
|
||||
if (! in_array($type, $allow_type)) {
|
||||
// 如果不被允许,则直接停止程序运行
|
||||
return;
|
||||
}
|
||||
// 判断是否是通过HTTP POST上传的
|
||||
if (! is_uploaded_file($v['tmp_name'])) {
|
||||
// 如果不是通过HTTP POST上传的
|
||||
return;
|
||||
}
|
||||
|
||||
$upload_path = FCPATH.self::DIR.DIRECTORY_SEPARATOR."users_info_img/";
|
||||
if (! file_exists($upload_path)) {
|
||||
mkdir($upload_path);
|
||||
}
|
||||
|
||||
$img_id = time() . rand(1, 100) . "." . $type;
|
||||
|
||||
$list[$k] ='users_info_img/'.$img_id;
|
||||
if (move_uploaded_file($v['tmp_name'], $upload_path . $img_id)) {
|
||||
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
return $list[0];
|
||||
}
|
||||
|
||||
/* base64格式编码转换为图片并保存对应文件夹 */
|
||||
function base64_image_save($base64_image_content,$path,$name)
|
||||
{
|
||||
//匹配出图片的格式
|
||||
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
|
||||
$type = $result[2] === 'jpeg'?'jpg':$result[2];
|
||||
if (!file_exists($path)) {
|
||||
//检查是否有该文件夹,如果没有就创建,并给予最高权限
|
||||
mkdir($path, 0755);
|
||||
}
|
||||
$new_file = $name . ".{$type}";
|
||||
if (file_put_contents($path . $new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
|
||||
return $type;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 居中裁剪图片
|
||||
* @param string $source [原图路径]
|
||||
* @param int $width [设置宽度]
|
||||
* @param int $height [设置高度]
|
||||
* @param string $target [目标路径]
|
||||
* @return bool [裁剪结果]
|
||||
*/
|
||||
public function image_center_crop($source, $width, $height, $target)
|
||||
{
|
||||
if (!file_exists($source)) return false;
|
||||
/* 根据类型载入图像 */
|
||||
switch (exif_imagetype($source)) {
|
||||
case IMAGETYPE_JPEG:
|
||||
$image = imagecreatefromjpeg($source);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
$image = imagecreatefrompng($source);
|
||||
break;
|
||||
case IMAGETYPE_GIF:
|
||||
$image = imagecreatefromgif($source);
|
||||
break;
|
||||
}
|
||||
if (!isset($image)) return false;
|
||||
/* 获取图像尺寸信息 */
|
||||
$target_w = $width;
|
||||
$target_h = $height;
|
||||
$source_w = imagesx($image);
|
||||
$source_h = imagesy($image);
|
||||
/* 计算裁剪宽度和高度 */
|
||||
$judge = (($source_w / $source_h) > ($target_w / $target_h));
|
||||
$resize_w = $judge ? ($source_w * $target_h) / $source_h : $target_w;
|
||||
$resize_h = !$judge ? ($source_h * $target_w) / $source_w : $target_h;
|
||||
$start_x = $judge ? ($resize_w - $target_w) / 2 : 0;
|
||||
$start_y = !$judge ? ($resize_h - $target_h) / 2 : 0;
|
||||
/* 绘制居中缩放图像 */
|
||||
$resize_img = imagecreatetruecolor($resize_w, $resize_h);
|
||||
imagecopyresampled($resize_img, $image, 0, 0, 0, 0, $resize_w, $resize_h, $source_w, $source_h);
|
||||
$target_img = imagecreatetruecolor($target_w, $target_h);
|
||||
imagecopy($target_img, $resize_img, 0, 0, $start_x, $start_y, $resize_w, $resize_h);
|
||||
/* 将图片保存至文件 */
|
||||
if (!file_exists(dirname($target))) mkdir(dirname($target), 0777, true);
|
||||
switch (exif_imagetype($source)) {
|
||||
case IMAGETYPE_JPEG:
|
||||
imagejpeg($target_img, $target);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
imagepng($target_img, $target);
|
||||
break;
|
||||
case IMAGETYPE_GIF:
|
||||
imagegif($target_img, $target);
|
||||
break;
|
||||
}
|
||||
return boolval(file_exists($target));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user