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

版权声明:本文为博主原创文章,未经博主允许不得转载。切~ https://blog.csdn.net/xla9071352/article/details/78875937
<?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);

?>

没有更多推荐了,返回首页