PHP 将动态图片与固定背景图片合成并添加水印。

版权声明:本文注明出处可以转载。 https://blog.csdn.net/jimuoyu/article/details/80728508
先放代码。
 public function mergeImage( $path_1, $path_2 )
    {
        $img = imagecreatefromstring(file_get_contents($path_1));
        $image_2 = imagecreatefrompng($path_2);

        //imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )---拷贝并合并图像的一部分
        //将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
        imagecopymerge($img, $image_2, 0, 0, 0, 0, imagesx($image_2), imagesy($image_2), 100);
        return $img;
    }

这是将两张图片合成到一起。$path_1 是背景图,$path_2是动态图片

 public function addTextToImage( $img, $text1,  )
    {
        $font = '../JDJH.ttf';//字体
        $white = 255;   //字体颜色,255对应的白色   
        $fontSize = 80;   //字体大小
        $circleSize = 0; //旋转角度
        $left = 50;      //左边距
        $top = 100;       //顶边距

        imagefttext($img, $fontSize, $circleSize, $left, $top, $white, $font, $text1);
        return $img;

    }

这是在图片上添加文字,要注意的有

1.

imagefttext()

这个方法是使用PHP的GD库的,如果这个方法不能使用的话,看下GD库是否启用,以及GD库中freetype是否启用。

2.字体文件是需要自己下载的

3.$img 是已经实例化的图片,不是图片路径。实例化方法

imagecreatefrompng()

因为我们的图片需要打印,这样生成的图片是72像素,打印出来并不清晰,所以需要改变图片的分辨率。

参考了这篇博文

public function changeResolution( $img, $path_2 )
    {

        imagepng($img, $path_2);

        imagedestroy($img);


        $filename = $path_2;

        $file = file_get_contents($filename);

//数据块长度为9
        $len = pack("N", 9);
//数据块类型标志为pHYs
        $sign = pack("A*", "pHYs");
//X方向和Y方向的分辨率均为300DPI(1像素/英寸=39.37像素/米),单位为米(0为未知,1为米)
        $data = pack("NNC", 300 * 39.37, 300 * 39.37, 0x01);
//CRC检验码由数据块符号和数据域计算得到
        $checksum = pack("N", crc32($sign . $data));
        $phys = $len . $sign . $data . $checksum;

        $pos = strpos($file, "pHYs");
        if ($pos > 0) {
            //修改pHYs数据块
            $file = substr_replace($file, $phys, $pos - 4, 21);
        } else {
            //IHDR结束位置(PNG头固定长度为8,IHDR固定长度为25)
            $pos = 33;
            //将pHYs数据块插入到IHDR之后
            $file = substr_replace($file, $phys, $pos, 0);
        }
        file_put_contents($path_2,$file);  //将字符串保存成图片格式        }
这样所有我们需要的图片处理都完成了,接下来,可以根据个人业务需求,输出图片
//输出图片
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
    case 1://GIF
        header('Content-Type: image/gif');
        imagegif($dst);
        break;
    case 2://JPG
        header('Content-Type: image/jpeg');
        imagejpeg($dst);
        break;
    case 3://PNG
        header('Content-Type: image/png');
        imagepng($dst);
        break;
    default:
        break;
}

或者使用

        imagegif($dst,$file);  //$file 文件保存路径,png,jpg同理

保存图片。

因为我们要实现下载多张图片,采用将图片打包压缩

public function zipImage( $array, $storeid )
    {
        //这里需要注意该目录是否存在,并且有创建的权限
        $zipname = '../attachment/images/1/' . $storeid . '.zip';
        //这是要打包的文件地址数组
        $files = $array;


        $zip = new ZipArchive();
        $res = $zip->open($zipname, ZipArchive::CREATE);
        if ($res === TRUE) {
            foreach ($files as $file) {
                //这里直接用原文件的名字进行打包,也可以直接命名,需要注意如果文件名字一样会导致后面文件覆盖前面的文件,所以建议重新命名
                $new_filename = substr($file, strrpos($file, '/') + 1);
                $zip->addFile($file, $new_filename);
            }
            //关闭文件<span style="white-space:pre;">   </span>
            $zip->close();
        }
        return $zipname;


    }

    public function downZIP( $zipname )
    {
        //这里是下载zip文件<span style="white-space:pre;"> </span>
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: Binary");

        header("Content-Length: " . filesize($zipname));
        header("Content-Disposition: attachment; filename=\"" . basename($zipname) . "\"");

        readfile($zipname);
        exit;
    }
如果单张下载就没必要打包了。

猜你喜欢

转载自blog.csdn.net/jimuoyu/article/details/80728508