学习日记-2016.3.31

昨晚编写上传部分有部分报错,已经修改,详情请看2016.3.30笔记

今天学习了上传后在,对上传的图像进行剪裁,处理写入到数据库,这个操作是再上传之前完成的

<?php
/**
* Created by PhpStorm.
* User: 兰小宇
* Date: 2016/3/30
* Time: 23:08
*/
//图像处理类
class Image{
private $file; //图像地址
private $width; //获取图像的宽度
private $height; //获取图像的高度
private $type; //获取图像的类型
private $img; //原来图像的资源句柄
private $new; //新的资源句柄
//构造方法
public function __construct($file){
$this->file = $_SERVER['DOCUMENT_ROOT'].$file;
list($this->width,$this->height,$this->type) = getimagesize($this->file);
$this->img = $this->getType($this->file,$this->type);
}


/* 图像剪裁一:按照百分比
* @param $pre ,设定百分比的参数,除以100 再乘以原来的宽度,得到新的宽度,
* 结果图片容易失真
*
*/
// public function thumb($pre){
// $new_width = $this->width * ( $pre / 100);
// $new_height = $this->height * ( $pre / 100);
// /**
// *创建一个背景图
// * 使用函数imagecreatetruecolor();两个参数高度和宽度
// *
// */
// $this->new = imagecreatetruecolor($new_width,$new_height);
// //创建剪裁后的图像
// imagecopyresampled($this->new,$this->img,0,0,0,0,$new_width,$new_height,$this->width,$this->height);
// }


//判断图像类型,然后加载图像资源
private function getType($file,$type){
$img = '';
switch($type){
case 1:
$img = imagecreatefromgif($file);
break;
case 2:
$img = imagecreatefromjpeg($file);
break;
case 3:
$img = imagecreatefrompng($file);
break;
default:
Tool::alertBack('请上传图片类型为gif,jpg,png的文件!');
}
return $img;
}
//图像输出
public function out(){
imagepng($this->new,$this->file);//输出
imagedestroy($this->img);//销毁资源
imagedestroy($this->new);//销毁
}
}

//备注:今天更新第一种处理图像的方法,是按照百分比进行处理,由于时间太晚,明天还要上班,暂不更新,明天更新,等比例处理图像和,完美处理图像两种方法!晚安!

原文地址:https://www.cnblogs.com/lanxiaoyu/p/5339682.html