PHP获取服务器图片并添加水印

<?php
header ( "Content-type: text/html; charset=utf-8" );
date_default_timezone_set ( 'PRC' ); //设置中国时区

include 'PHPExcel.php';
$dir=dirname(__FILE__);

$filename = "/Users/jiangminghui/Documents/test/hantai/hantaipic.xlsx";

$objPHPExcel = PHPExcel_IOFactory::load($filename);//加载文件

$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$src_path1 = '111.png';
$src_path2 = '222.png';
$src1 = imagecreatefromstring(file_get_contents($src_path1));
$src2 = imagecreatefromstring(file_get_contents($src_path2));
list($src_w1, $src_h1) = getimagesize($src_path1);
list($src_w2, $src_h2) = getimagesize($src_path2);
for($i=1;$i<=$highestRow;$i++) {
    $A = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
    $B = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
    $C = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();

//从服务器获取图片++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    $url = 'https://xx.xx.com'.$C;
    $ext = strrchr($url, ".");
    $filename = $A.'_'.$B.$ext;

    ob_start();//打开输出
    readfile($url);//输出图片文件
    $img = ob_get_contents();//得到浏览器输出
    ob_end_clean();//清除输出并关闭
    $size = strlen($img);//得到图片大小
    $fp2 = @fopen($filename, "a");
    fwrite($fp2, $img);//向当前目录写入图片文件,并重新命名
    fclose($fp2);

    //usleep(200000);
    //return $filename;//返回新的文件名

    $dst_path = 'img/'.$filename;

//创建图片的实例++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    $dst = imagecreatefromstring(file_get_contents($dst_path));


//获取水印图片的宽高

    list($src_w0, $src_h0) = getimagesize($dst_path);

//将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
    imagecopymerge($dst, $src1, 10, 10, 0, 0, $src_w1, $src_h1, 100);
//如果水印图片本身带透明色,则使用imagecopy方法
    imagecopy($dst, $src2, $src_w0-$src_w2-10, $src_h0-$src_h2-10, 0, 0, $src_w2, $src_h2);

//输出图片
    list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
    switch ($dst_type) {
        case 1://GIF
            header('Content-Type: image/gif');
            imagegif($dst,$filename);
            break;
        case 2://JPG
            header('Content-Type: image/jpeg');
            imagejpeg($dst,$filename);
            break;
        case 3://PNG
            header('Content-Type: image/png');
            imagepng($dst,$filename);
            break;
        default:
            break;
    }

    imagedestroy($dst);

    echo "$i";

}
imagedestroy($src1);
imagedestroy($src2);

?>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值