2021-02-08

博客介绍了如何重构合成大西瓜应用,实现自动部署和图片处理功能。用户上传图片后,系统会裁剪图片并按指定尺寸转换为圆形PNG,所有操作完成后自动上线。代码包括图片接收、尺寸调整、圆形转换等步骤,并提供了GitHub源码链接。
摘要由CSDN通过智能技术生成

合成大西瓜自动部署上线

自动裁切图片实现了,但是居然没人用,突发奇想,做个自动上线的总该有人用了吧。

重构合成大西瓜:

思路:

(1)服务器上存储一份源文件

(2)复制源文件到melon/目录下,以时间戳命名。

(3)接收用户上传的图片,进行修改裁切,存放至时间戳下\res\raw-assets目录下

开整!

1、接收上传的图片:

// 接收图片
if(!empty($file = $_FILES)){
    // 图片校验:数量、格式
    foreach ($file as $f) {
        $result = picValidate($f);		// 验证图片
        if (!$result) {
            exit('图片出错,请重试');
        }
    }


    $file = $_FILES;
    $time = time();
    make_dir($time);
    $upload_path = 'src\\'.$time.'\\';
    // 按顺序命名存放
    for($i=0; $i<sizeof($file); $i++){
        $f = $file[$i];
        $filename = $ls[$i].$f['name'];
        $new_file = $upload_path.$filename;
        move_uploaded_file($f['tmp_name'],$new_file);
    }

    sleep(1);
    // 图片处理
    toAsset($time);
    // 跳转回本页面
//    header("Location:upload_picture.php");
}

2、toAsset() 函数对图片进行裁切

function toAsset($time)
{
    $sizes = [52, 80, 108, 119, 152, 183, 193, 258, 308, 309, 408];

    $listName = ['ad16ccdc-975e-4393-ae7b-8ac79c3795f2.png',
        '0cbb3dbb-2a85-42a5-be21-9839611e5af7.png',
        'd0c676e4-0956-4a03-90af-fee028cfabe4.png',
        '74237057-2880-4e1f-8a78-6d8ef00a1f5f.png',
        '132ded82-3e39-4e2e-bc34-fc934870f84c.png',
        '03c33f55-5932-4ff7-896b-814ba3a8edb8.png',
        '665a0ec9-6c43-4858-974c-025514f2a0e7.png',
        '84bc9d40-83d0-480c-b46a-3ef59e603e14.png',
        '5fa0264d-acbf-4a7b-8923-c106ec3b9215.png',
        '564ba620-6a55-4cbe-a5a6-6fa3edd80151.png',
        '5035266c-8df3-4236-8d82-a375e97a0d9c.png'];
    $lists = array('ad', '0c', 'd0', '74', '13', '03', '66', '84', '5f', '56', '50');


    // 获取源文件 ===>>> 保存至对应目录
    $arr = get_file_list($time);
    $baseSrcPath = 'src\\' . $time . '\\';
    $basePath = 'melon/'.$time.'/res/raw-assets/';

    // 1、处理成对应尺寸的矩形
    for($i=0; $i<11; $i++){
        $img = resize_image($baseSrcPath.$arr[$i],$sizes[$i],$sizes[$i],'jpg');
        // 保存覆盖原图片
        imagepng($img,$baseSrcPath.$arr[$i]);
    }


    // 2、调用修改函数 => 遍历读取文件夹下图片进行修改成圆形png
    for($i=0; $i<11; $i++){
        $path = $basePath.$lists[$i]."/";
        $name = $listName[$i];
        $dest =  $path.$name;
		// 图片转换成圆形png ==>> 保存至新建的文件夹
        z_image2circle($baseSrcPath.$arr[$i], $dest);
    }

3、z_image2circle()函数

function z_image2circle($src, $dst){

    //获取原图尺寸,并设置新图片的宽度和高度
    list($w, $h) = getimagesize($src);
    if( $w >= $h ){
        $w = $h;
    }else{
        $h = $w;
    }

    $oimgSrc = imagecreatefromstring(file_get_contents($src));
    $oimgDst = imagecreatetruecolor($w, $h);
//    $oimgDst = imagecreatetruecolor($size, $size);

    imagealphablending($oimgDst,false);
    $transparent = imagecolorallocatealpha($oimgDst, 0, 0, 0, 127);
    $r=$w/2;
    for($x=0;$x<$w;$x++){
        for($y=0;$y<$h;$y++){
            $c = imagecolorat($oimgSrc,$x,$y);
            $_x = $x - $w/2;
            $_y = $y - $h/2;
            if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
                imagesetpixel($oimgDst,$x,$y,$c);
            }else{
                imagesetpixel($oimgDst,$x,$y,$transparent);
            }
        }
    }
    // 修改图片尺寸

    imagesavealpha($oimgDst, true);
    imagepng($oimgDst, $dst);

    imagedestroy($oimgDst);
    imagedestroy($oimgSrc);
}

以上就是程序的核心了,还有其他的代码不一一展示。

源码将会上传至我的github:https://github.com/goatyyc/watermelon

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值