qq_33893544 2021-02-21 15:39 采纳率: 35.7%
浏览 189
已采纳

imagepng 保存后图片非常达到1m多 原来仅是100k,该怎么处理 下面是代码

<?php
imgshuiyin("./img/1.jpg",'./img/6.jpg',100,100);

   function imgshuiyin($dname,$wname,$pos=7,$tou=50){
        //img(主图1,主图2,位置,透明度)
        function imgclass($name){
            $img_info=getimagesize($name);
            //getimagesize($dname)函数可以动态的获取图片类型,大小,宽度和高度等

            $img_width=$img_info[0];
            $img_height=$img_info[1];
            $img_mime=$img_info['mime'];
            //根据类型来判断资源
            switch ($img_mime) {
                case 'image/jpeg':
                    $img=imagecreatefromjpeg($name);
                    break;
                case 'image/gif':
                    $img=imagecreatefromgif($name);
                    break;
                case 'image/png':
                    $img=imagecreatefrompng($name);
                    break;
                case 'image/wbmp':
                    $img=imagecreatefromwbmp($name);
                    break;
            }
            return array('width'=>$img_width,'height'=>$img_height,'res'=>$img);
        }
        $dst_info=imgclass($dname);
        $water_info=imgclass($wname);
        //getimagesize($dname)函数可以动态的获取图片类型,大小,宽度和高度等

        $dst_width=$dst_info['width'];
        $dst_height=$dst_info['height'];

        $water_width=$water_info['width'];
        $water_height=$water_info['height'];
        $water=$water_info['res'];
        $dst=$dst_info['res'];

        /*
        输出:array(3) { ["width"]=> int(3104) ["height"]=> int(4192) ["mime"]=> string(10) "image/jpeg" }
              array(3) { ["width"]=> int(1470) ["height"]=> int(1391) ["mime"]=> string(10) "image/jpeg" }
         */
        switch ($pos) {
            case 1:
                $x=0;
                $y=0;
                break;

            case 2:
                $x=$dst_width/2-$water_width/2;
                $y=0;
                break;

            case 3:
                $x=$dst_width-$water_width;
                $y=0;
                break;

            case 4:
                $x=0;
                $y=$dst_height/2-$water_height/2;
                break;

            case 5:
                $x=$dst_width/2-$water_width/2;
                $y=$dst_height/2-$water_height/2;
                break;
            case 6:
                $x=$dst_width-$water_height;
                $y=$dst_height/2-$water_height/2;
                break;
            case 7:
                $x=0;
                $y=$dst_height-$water_height;
                break;
            case 8:
                $x=$dst_width/2-$water_height/2;
                $y=$dst_height-$water_height;
                break;
            case 9:
                $x=$dst_width-$water_height;
                $y=$dst_height-$water_height;
                break;

        }
        imagecopymerge($dst,$water,$x,$y,0,0,$water_width,$water_height,$tou);

        header("content-type:image/png");

        imagepng($dst,'./img/6.jpg');//可以自定义参数imagepng($dst,1.png);会自动生成1.png图像,最好随机生成
        imagedestroy($dst);
        imagedestroy($water);
    }
  • 写回答

4条回答 默认 最新

  • 歇歇 2021-02-21 19:20
    关注

    PNG是一种无损格式,对于通常以JPEG文件压缩的​​照片和其他复杂图像,不会产生良好的压缩率。

    更如果要将JPEG文件转换为PNG,则PNG还必须逐像素再现由JPEG有损压缩引起的压缩伪像。文件会变大。文件大小又图片像素值决定,您可以缩小图片的分辨率(长,宽)来缩小png文件

    quality

    压缩级别:从0(无压缩)到9。默认(-1)使用zlib压缩默认值。有关更多信息,请参见»zlib手册

    filters

    允许减小PNG文件的大小。它是一个位掩码字段,可以将其设置为PNG_FILTER_XXX 常量的任何组合。PNG_NO_FILTER或者 PNG_ALL_FILTERS也可以分别用于禁用或激活所有过滤器。默认值(-1)禁用过滤。

    采用楼上答案即可

     

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型