imagecopymerge

//水印图像
            $mask_url = "https://store.heytapimage.com/uploadFiles/admin_appstore/202206/23/e1cc4baf054c1cfdb65c9a72d5b98751.png";
            $src = CommonModel::downloadFile('', $mask_url);
            foreach ($results as $result) {
                //原始图像
                $img_url = strpos($result['content'], 'http') !== false ? $result['content'] : (strpos($result['content'], '//') !== false ? ('http:' . $result['content']) : (Yii::$app->params['pic_file_host'] . $result['content']));
                $dst = $img_url;
                $pathInfo = pathinfo($dst);
                $ext = $pathInfo['extension'];

                //得到原始图片信息
                if ($ext == 'png') {
                    $dst_im = @\imagecreatefrompng($dst);
                } else if ($ext == 'jpg') {
                    $dst_im = @\imagecreatefromjpeg($dst);
                } else if ($ext == 'gif') {
                    $dst_im = @\imagecreatefromgif($dst);
                } else {
                    $dst = $img_url . '?region=' . Yii::$app->params['themeapi_package']['package_ocs_region'] . '&x-ocs-process=image/crop,x_0,y_0,w_1080,h_414/format,png';
                    $dst_im = @\imagecreatefromstring(file_get_contents($dst));
                }
                if (!$dst_im) {
                    $dst_im = @\imagecreatefromstring(file_get_contents($dst));
                }
                if (!$dst_im) {
                    Clog::warning('素材图imagecreatefrom失败,[data=' . json_encode($result) . ']');
                    echo '素材图imagecreatefrom失败,[data=' . json_encode($result) . ']';
                    continue;
                }
                $dst_info = getimagesize($dst);
                if (!$dst_info) {
                    Clog::warning('素材图getimagesize失败,[data=' . json_encode($result) . ']');
                    echo '素材图getimagesize失败,[data=' . json_encode($result) . ']';
                    continue;
                }

                //水印图像
                $src_im = imagecreatefrompng($src);
                if (!$src_im) {
                    Clog::warning('水印图imagecreatefrom失败,[data=' . json_encode($result) . ']');
                    echo '水印图imagecreatefrom失败,[data=' . json_encode($result) . ']';
                    continue;
                }
                $src_info = getimagesize($src);
                if (!$src_info) {
                    Clog::warning('水印图getimagesize失败,[data=' . json_encode($result) . ']');
                    echo '水印图getimagesize失败,[data=' . json_encode($result) . ']';
                    continue;
                }

                //水印透明度
                $alpha = 50;

                //合并水印图片
                imagecopymerge($dst_im, $src_im, $dst_info[0] - $src_info[0], $dst_info[1] - $src_info[1], 0, 0, $src_info[0], $src_info[1], $alpha);

                //输出合并后水印图片
                $filename = sys_get_temp_dir() . '/' . md5(microtime(true) . $img_url) . '.png';
                $bool = imagepng($dst_im, $filename);
                if (!$bool) {
                    Clog::warning('imagepng失败,[data=' . json_encode($result) . ']');
                    echo 'imagepng失败,[data=' . json_encode($result) . ']';
                    continue;
                }
                imagedestroy($dst_im);
                imagedestroy($src_im);
                //上传
                $upload_data = ['type' => 'appDomain', 'folder' => '', 'file' => $filename];
                $upload_response = CurlServiceDao::uploadFile("/innerapi/utility/upload", $upload_data, 'inneropenapi', true);;
                if (0 != $upload_response['errno']) {
                    Clog::warning('上传图片到ocs失败#respone=' . json_encode($upload_response, JSON_UNESCAPED_UNICODE) . ',[data=' . json_encode($result) . ']');
                    echo '上传图片到ocs失败#respone=' . json_encode($upload_response, JSON_UNESCAPED_UNICODE) . ',[data=' . json_encode($result) . ']';
                    continue;
                }

                if (!isset($upload_response['data']['uri_path']) || empty($upload_response['data']['uri_path'])) {
                    Clog::warning('上传图片到ocs失败#respone=' . json_encode($upload_response, JSON_UNESCAPED_UNICODE) . ',[data=' . json_encode($result) . ']');
                    echo '上传图片到ocs失败#respone=' . json_encode($upload_response, JSON_UNESCAPED_UNICODE) . ',[data=' . json_encode($result) . ']';
                    continue;
                }
                $merge_mask = $upload_response['data']['uri_path'];
                $data = [
                    'merge_mask' => $merge_mask,
                ];
                $update_response = CdoOperationThemeDao::getInstance()->saveOne($result['id'], $data, 'dev_page_material');
                if (!$update_response) {
                    Clog::warning('保存数据失败,[data=' . json_encode($result) . ']');
                    echo '保存数据失败,[data=' . json_encode($result) . ']';
                    continue;
                }
            }
            @unlink($src);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值