header("Content-type:text/html;charset=utf-8");
1.引入imagick.php这个文件到项目中,这个文件可以去网上下载
require_once("../extend/imagick/imagick.php");
try{
$this->validate->rule([
一些参数验证
]);
if(!$this->validate->check($this->params)){
return json(['code' => 500, 'msg' => $this->validate->getError()]);
}
if($this->params['photo_url']!=''){
$new_file = 保存到服务器的图片路径;
2.保存不带image头信息的base64格式图片
$result=file_put_contents($new_file, base64_decode($this->params['photo_url']));
3.修改图片的分辨率,不传值默认给300
$dpi=isset($this->params['dpi'])?$this->params['dpi']:300;
if($dpi!=0){
$image = new \Imagick();
$image->readImage($new_file);
$image->setImageResolution($dpi,$dpi);
$image->resampleImage($dpi,$dpi,\imagick::FILTER_UNDEFINED,0);
$image->writeImage($new_file);
}
if (!$result) throw new \Exception('图片存储失败');
}
|