编程语言
首页 > 编程语言> > PHP获取服务器图片并添加水印

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

作者:互联网

  1. <?php

  2. header ( "Content-type: text/html; charset=utf-8" );

  3. date_default_timezone_set ( 'PRC' ); //设置中国时区

  4.  
  5. include 'PHPExcel.php';

  6. $dir=dirname(__FILE__);

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

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

  11.  
  12. $sheet = $objPHPExcel->getSheet(0);

  13. $highestRow = $sheet->getHighestRow(); // 取得总行数

  14. $highestColumn = $sheet->getHighestColumn(); // 取得总列数

  15. $src_path1 = '111.png';

  16. $src_path2 = '222.png';

  17. $src1 = imagecreatefromstring(file_get_contents($src_path1));

  18. $src2 = imagecreatefromstring(file_get_contents($src_path2));

  19. list($src_w1, $src_h1) = getimagesize($src_path1);

  20. list($src_w2, $src_h2) = getimagesize($src_path2);

  21. for($i=1;$i<=$highestRow;$i++) {

  22. $A = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();

  23. $B = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();

  24. $C = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();

  25.  
  26. //从服务器获取图片++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  27. $url = 'https://xx.xx.com'.$C;

  28. $ext = strrchr($url, ".");

  29. $filename = $A.'_'.$B.$ext;

  30.  
  31. ob_start();//打开输出

  32. readfile($url);//输出图片文件

  33. $img = ob_get_contents();//得到浏览器输出

  34. ob_end_clean();//清除输出并关闭

  35. $size = strlen($img);//得到图片大小

  36. $fp2 = @fopen($filename, "a");

  37. fwrite($fp2, $img);//向当前目录写入图片文件,并重新命名

  38. fclose($fp2);

  39.  
  40. //usleep(200000);

  41. //return $filename;//返回新的文件名

  42.  
  43. $dst_path = 'img/'.$filename;

  44.  
  45. //创建图片的实例++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  46. $dst = imagecreatefromstring(file_get_contents($dst_path));

  47.  
  48.  
  49. //获取水印图片的宽高

  50.  
  51. list($src_w0, $src_h0) = getimagesize($dst_path);

  52.  
  53. //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果

  54. imagecopymerge($dst, $src1, 10, 10, 0, 0, $src_w1, $src_h1, 100);

  55. //如果水印图片本身带透明色,则使用imagecopy方法

  56. imagecopy($dst, $src2, $src_w0-$src_w2-10, $src_h0-$src_h2-10, 0, 0, $src_w2, $src_h2);

  57.  
  58. //输出图片

  59. list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);

  60. switch ($dst_type) {

  61. case 1://GIF

  62. header('Content-Type: image/gif');

  63. imagegif($dst,$filename);

  64. break;

  65. case 2://JPG

  66. header('Content-Type: image/jpeg');

  67. imagejpeg($dst,$filename);

  68. break;

  69. case 3://PNG

  70. header('Content-Type: image/png');

  71. imagepng($dst,$filename);

  72. break;

  73. default:

  74. break;

  75. }

  76.  
  77. imagedestroy($dst);

  78.  
  79. echo "$i";

  80.  
  81. }

  82. imagedestroy($src1);

  83. imagedestroy($src2);

  84.  
  85. ?>

标签:src,objPHPExcel,get,dst,水印,filename,服务器,PHP,图片
来源: https://blog.csdn.net/keke795/article/details/113813968