ctms/pms-admin/controllers/AttachController.php
2025-04-10 23:19:13 +08:00

58 lines
1.9 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @Author: fm453
* @Date: 2018-08-17 19:39:41
* @Last Modified by: fm453
* @Last Modified time: 2019-01-12 12:13:35
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
class AttachController extends \yii\web\Controller
{
public $enableCsrfValidation = false; //取消csrf令牌验证
//主界面
public function actionIndex()
{
$request = Yii::$app->request;
$get = $request->get();
$img = Urldecode($get['img']);
$file = Yii::getAlias("@upload").$img;
$img = file_get_contents($file);
$this->showImg($file);
}
/*
* php 页面直接输出图片
*/
function showImg($img){
$info = getimagesize($img);
$imgExt = image_type_to_extension($info[2], false); //获取文件后缀
$fun = "imagecreatefrom{$imgExt}";
$imgInfo = $fun($img); //1.由文件或 URL 创建一个新图象。如:imagecreatefrompng ( string $filename )
$mime = $info['mime'];
header('Content-Type:'.$mime);
$quality = 100;
if($imgExt == 'png') $quality = 9; //输出质量,JPEG格式(0-100),PNG格式(0-9)
$getImgInfo = "image{$imgExt}";
$getImgInfo($imgInfo, null, $quality); //2.将图像输出到浏览器或文件。如: imagepng ( resource $image )
//TBD: PNG图片会显示其被修改前的原始文件与直接用SRC引用的方法有所不同
imagedestroy($imgInfo);
}
/*图片转换为 base64格式编码*/
function base64EncodeImage ($image_file) {
$base64_image = '';
$image_info = getimagesize($image_file);
$image_data = fread(fopen($image_file, 'r'), filesize($image_file));
$base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
return $base64_image;
}
}