ChapterImageController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2018/1/19
  6. * Time: 下午3:15
  7. */
  8. namespace App\Http\Controllers\Channel\Book;
  9. use GuzzleHttp\Client;
  10. use GuzzleHttp\Exception\RequestException;
  11. use Hashids;
  12. use Illuminate\Http\Request;
  13. use App\Http\Controllers\Channel\BaseController;
  14. use App\Http\Controllers\Channel\Book\Transformers\ChapterImageTransformer;
  15. use App\Libs\ChapterToImage;
  16. use App\Modules\Book\Services\ChapterImageService;
  17. use App\Modules\Book\Services\ChapterService;
  18. use Psr\Http\Message\ResponseInterface;
  19. class ChapterImageController extends BaseController
  20. {
  21. /**
  22. * @apiDefine promotion 推广
  23. */
  24. /**
  25. * @apiVersion 1.0.0
  26. * @apiDescription 获取章节图片列表
  27. * @api {GET} api/book/fiveChapterImage 获取章节图片列表
  28. * @apiGroup promotion
  29. * @apiName api/book/fiveChapterImage
  30. * @apiParam {String} bid 图书ID
  31. * @apiSuccessExample {json} Success-Response:
  32. *
  33. * {
  34. * "code": 0,
  35. * "msg": "",
  36. * "data":[
  37. * {
  38. * }
  39. * ]
  40. * }
  41. */
  42. public function getTopFiveChapter(Request $request) {
  43. $bid = $request->has('bid') ? $request->input('bid') : '';
  44. if (empty($bid)) return response()->error("PARAM_EMPTY");
  45. $bid = Hashids::decode($bid)[0];
  46. $chapter_count = ChapterImageService::getChapterImgCount($bid);
  47. $chapters = ChapterService::getTopFiveChapter($bid);
  48. $result = ChapterImageService::getChapterImage($bid);
  49. if(empty($result) || sizeof($result) == 0 || $chapter_count < sizeof($chapters)) {
  50. ChapterToImage::createChapterImage($bid,'',$chapters);
  51. $result = ChapterImageService::getChapterImage($bid);
  52. }
  53. $resultAA = array();
  54. if($result && sizeof($result) > 0) {
  55. foreach ($result as $rr) {
  56. $cid = $rr['cid'];
  57. $resultAA[$cid][] = ['image_url'=>$rr['image_url'],'height'=>$rr['image_height'],'id'=>$rr['id']];
  58. }
  59. }
  60. $resultBB = array();
  61. $client = new Client();
  62. if($resultAA && sizeof($resultAA) > 0) {
  63. foreach ($resultAA as $key => $value) {
  64. //$resultBB[] = $value;
  65. $temp = [];
  66. foreach ($value as $img){
  67. $height = $img['height'];
  68. //$imageInfo = @file_get_contents($img.'?x-oss-process=image/info');
  69. if(!$height){
  70. preg_match("/(\w+:\/\/)([^\/:]+)([^?]+)?([\/s\/S]*)/",$img['image_url'],$matches);
  71. $imageInfo = $client->request('GET', env('OSS_INTERNAL_DOMAIN').$matches[3].'?x-oss-process=image/info')->getBody()->getContents();
  72. //\Log::info($imageInfo);
  73. if($imageInfo){
  74. $imageInfo = json_decode($imageInfo,true);
  75. if(isset($imageInfo['ImageHeight']['value'])){
  76. $height = $imageInfo['ImageHeight']['value'];
  77. $param = array(
  78. 'file_size'=>$imageInfo['FileSize']['value'],
  79. 'image_height'=>$imageInfo['ImageHeight']['value'],
  80. 'image_width'=>$imageInfo['ImageWidth']['value']
  81. );
  82. ChapterImageService::updateChapterImage($img['id'],$param);
  83. }
  84. }
  85. if(!$height){
  86. $height = 300;
  87. }
  88. }
  89. $temp[] = ['image_url'=>$img['image_url'],'height'=>$height];
  90. }
  91. $resultBB[] = $temp;
  92. }
  93. }
  94. // return response()->collection(new ChapterImageTransformer(), $result);
  95. return response()->success($resultBB);
  96. }
  97. }