PHP gets server image and add watermark

  1. <?php

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

  3. date_default_timezone_set ('PRC' ); //Set China time zone

  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);//Load file

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

  13. $highestRow = $sheet->getHighestRow(); // Get the total number of rows

  14. $highestColumn = $sheet->getHighestColumn(); // Get the total number of columns

  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. //Get pictures from the server++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++

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

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

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

  30.  
  31. ob_start();//Open the output

  32. readfile($url);//output picture file

  33. $img = ob_get_contents();//Get browser output

  34. ob_end_clean();//Clear output and close

  35. $size = strlen($img);//Get the picture size

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

  37. fwrite($fp2, $img);//Write the picture file to the current directory and rename it

  38. fclose($fp2);

  39.  
  40. //usleep(200000);

  41. //return $filename;//Return the new file name

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

  44.  
  45. //Create an example of the picture++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++

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

  47.  
  48.  
  49. //Get the width and height of the watermark image

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

  52.  
  53. //Copy the watermark picture to the target picture, the last parameter 50 is to set the transparency, here the semi-transparent effect is achieved

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

  55. //If the watermark picture itself has a transparent color, use the imagecopy method

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

  57.  
  58. //Output picture

  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. ?>

Guess you like

Origin blog.csdn.net/zl17822307869/article/details/114006885