TestController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Authority;
  4. use App\Batch;
  5. use App\Commodity;
  6. use App\CommodityMaterialBoxModel;
  7. use App\Components\AsyncResponse;
  8. use App\Components\Database;
  9. use App\Components\ErrorPush;
  10. use App\ErrorTemp;
  11. use App\Feature;
  12. use App\Http\ApiControllers\LoginController;
  13. use App\Http\Requests\OrderDelivering;
  14. use App\Jobs\BatchTaskJob;
  15. use App\Jobs\CacheShelfTaskJob;
  16. use App\Jobs\OrderCreateInstantBill;
  17. use App\Jobs\OrderCreateWaybill;
  18. use App\Jobs\SettlementBillReportJob;
  19. use App\Jobs\StoreCreateInstantBill;
  20. use App\Jobs\TestJob;
  21. use App\Jobs\WeightUpdateInstantBill;
  22. use App\LaborReport;
  23. use App\LaborReportStatus;
  24. use App\MaterialBox;
  25. use App\MaterialBoxModel;
  26. use App\Notifications\RoutineNotification;
  27. use App\OracleDOCASNHeader;
  28. use App\OracleDOCOrderHeader;
  29. use App\OracleDocOrderPackingSummary;
  30. use App\Order;
  31. use App\OrderBin;
  32. use App\OrderIssue;
  33. use App\OrderIssueProcessLog;
  34. use App\OrderPackage;
  35. use App\Owner;
  36. use App\OwnerAreaReport;
  37. use App\OwnerFeeDetail;
  38. use App\OwnerFeeDetailLogistic;
  39. use App\OwnerFeeExpress;
  40. use App\OwnerFeeLogistic;
  41. use App\OwnerFeeOperation;
  42. use App\OwnerFeeOperationDetail;
  43. use App\OwnerFeeStorage;
  44. use App\OwnerPriceOperation;
  45. use App\OrderPackageCountingRecord;
  46. use App\ProcurementCheckSheet;
  47. use App\RejectedBill;
  48. use App\SeeLog;
  49. use App\Services\BatchService;
  50. use App\Services\CacheShelfService;
  51. use App\Services\ForeignHaiRoboticsService;
  52. use App\Services\NotificationService;
  53. use App\Services\OrderPackageReceivedSyncService;
  54. use App\Services\OrderPackageService;
  55. use App\Services\OrderService;
  56. use App\Services\OwnerFeeTotalService;
  57. use App\Services\OwnerLogisticFeeReportService;
  58. use App\Services\OwnerPriceOperationService;
  59. use App\Services\OwnerStoreFeeReportService;
  60. use App\Services\OwnerStoreOutFeeReportService;
  61. use App\Services\ReviewService;
  62. use App\Services\StationService;
  63. use App\Services\StorageService;
  64. use App\Services\StoreService;
  65. use App\Station;
  66. use App\StationTask;
  67. use App\StationTaskMaterialBox;
  68. use App\Store;
  69. use App\TaskTransaction;
  70. use App\Unit;
  71. use App\User;
  72. use App\UserDetail;
  73. use App\UserDutyCheck;
  74. use App\ValueStore;
  75. use App\Waybill;
  76. use App\WorkOrder;
  77. use Carbon\Carbon;
  78. use Carbon\CarbonPeriod;
  79. use Decimal\Decimal;
  80. use Doctrine\DBAL\Exception;
  81. use Firebase\JWT\JWT;
  82. use Illuminate\Database\Eloquent\Collection;
  83. use Illuminate\Database\Eloquent\HigherOrderBuilderProxy;
  84. use Illuminate\Foundation\Http\FormRequest;
  85. use Illuminate\Http\Request;
  86. use Illuminate\Support\Facades\Broadcast;
  87. use Illuminate\Support\Facades\Cache;
  88. use Illuminate\Support\Facades\Auth;
  89. use Illuminate\Support\Facades\Cookie;
  90. use Illuminate\Support\Facades\DB;
  91. use Illuminate\Support\Facades\Http;
  92. use Illuminate\Support\Facades\Log;
  93. use Illuminate\Support\Facades\Notification;
  94. use Illuminate\Support\Facades\URL;
  95. use Illuminate\Support\Facades\Validator;
  96. use Illuminate\Support\Str;
  97. use Laravel\Horizon\Events\JobFailed;
  98. use Monolog\Handler\IFTTTHandler;
  99. use phpDocumentor\Reflection\Types\Resource_;
  100. use PhpOffice\PhpSpreadsheet\Calculation\Web\Service;
  101. use Ramsey\Uuid\Uuid;
  102. use Symfony\Component\ErrorHandler\Error\FatalError;
  103. class TestController extends Controller
  104. {
  105. use AsyncResponse, ErrorPush, Database;
  106. const ASNREFERENCE_2 = 'ASNREFERENCE2';
  107. public function __construct()
  108. {
  109. $this->data["active_test"] = "active";
  110. }
  111. public function method(Request $request, $method)
  112. {
  113. try {
  114. return call_user_func([$this, $method], $request);
  115. }catch (\BadMethodCallException $e){
  116. dd("方法不存在");
  117. }
  118. }
  119. /**
  120. * @param $wave
  121. * @return string
  122. */
  123. private function wms_status($wave): string
  124. {
  125. switch ($wave->wavestatus) {
  126. case 00:
  127. $wms_status = '创建';
  128. break;
  129. case 40:
  130. $wms_status = '部分收货';
  131. break;
  132. case 90:
  133. $wms_status = '取消';
  134. break;
  135. case 99:
  136. $wms_status = '完成';
  137. break;
  138. case 62:
  139. $wms_status = '部分装箱';
  140. break;
  141. default:
  142. $wms_status = (string)$wave->wavestatus;
  143. }
  144. return $wms_status;
  145. }
  146. public function assignBatch()
  147. {
  148. $code = \request("code");
  149. $batches = Batch::query()->where("code",$code)->get();
  150. if (!$batches->count()){
  151. $wave = DB::connection("oracle")->selectOne(DB::raw("select * from DOC_WAVE_HEADER where WAVENO = ?"),[$code]);
  152. if (!$wave){
  153. dd("FLUX无波次");
  154. }
  155. $owner = app("OwnerService")->codeGetOwner($wave->customerid);
  156. $obj = [
  157. "wms_status" => $this->wms_status($wave),
  158. "wms_type"=>$wave->descr,
  159. "created_at"=>date("Y-m-d H:i:s"),
  160. "wms_created_at"=>$wave->addtime,
  161. "updated_at"=>$wave->edittime,
  162. "owner_id"=>$owner->id,
  163. ];
  164. $wave = Batch::query()->where("code",$code)->first();
  165. if (!$wave){
  166. $obj["code"] = $code;
  167. $wave = Batch::query()->create($obj);
  168. }else{
  169. Batch::query()->where("code",$code)->update($obj);
  170. }
  171. $ordernos = array_column(DB::connection("oracle")->select(DB::raw("select orderno from DOC_WAVE_DETAILS where WAVENO = ?"),[$code]),"orderno");
  172. Order::query()->whereIn("code",$ordernos)->update([
  173. "batch_id"=>$wave->id
  174. ]);
  175. Order::query()->with(["batch","bin"])->whereIn("code",$ordernos)->get()->each(function ($order){
  176. if (!$order->bin){
  177. $bin = DB::connection("oracle")->selectOne(DB::raw("select seqno from DOC_WAVE_DETAILS where waveno = ? and orderno = ?"),[$order->batch->code,$order->code]);
  178. if ($bin){
  179. OrderBin::query()->create([
  180. 'order_id' => $order->id,
  181. 'number' => $bin->seqno,
  182. ]);
  183. }
  184. }
  185. });
  186. $batches = Batch::query()->where("code",$code)->get();
  187. }
  188. app("BatchService")->assignTasks($batches);
  189. }
  190. /**
  191. * 检查和处理揽收
  192. *
  193. * @param OrderPackage|\stdClass $package
  194. */
  195. public function checkingAndProcess(OrderPackage $package, int $lineNo)
  196. {
  197. //校验快递商 订单状态 复核标记 揽收标记
  198. $package->loadMissing("order.logistic");
  199. if ($package->collecting_status == '1' || !$package->logistic_number)dd(1);
  200. if (strpos($package->order->logistic->code ?? '','ZTO') === false)dd(2);
  201. $statusMapping = array_flip(Order::STATUS);
  202. //if (($statusMapping[$package->order->wms_status] ?? 99) > 63)dd(3);
  203. /** @var OrderPackageService $orderPackageService */
  204. $orderPackageService = app('OrderPackageService');
  205. $result = $orderPackageService->collectUpload([$package->logistic_number]);
  206. dump($result);
  207. if (!$result["success"]){
  208. dd("自动揽收失败",["message"=>$result["message"],"param"=>$package->logistic_number]);
  209. }
  210. $result = app("OrderService")->allocation($package->order->code,$lineNo);
  211. dump($result);
  212. if (mb_substr($result,0,3)=='000'){
  213. if (OrderPackage::query()->where("id",$package->id)
  214. ->where("collecting_status",'0')->update(["collecting_status"=>'1'])!=1)
  215. dd("自动揽收异常",["message"=>"未能成功修改揽收标记","param"=>$package->toArray()]);
  216. }else dd("自动揽收失败",["message"=>"分配失败:{$result}","param"=>$package->order->code." : {$lineNo}"]);
  217. }
  218. public function test()
  219. {
  220. $a = memory_get_usage();
  221. $tmp = str_repeat('http://blog.huachen.me/', 4000);
  222. $b = memory_get_usage();
  223. dump($b-$a);
  224. dd(Notification::send(User::query()->where("name","zhouzhendong")->get(),
  225. new RoutineNotification(SeeLog::query()->first()->toArray())));
  226. $user = Auth::user();
  227. $remark = "zengjunlj";
  228. $ownerName = 'zengjunlj' ?? '';
  229. $clientCode = 'zengjunljljlj' ?? '';
  230. $msg = $user["name"]."建立了新工单<br/>".$ownerName.":".$clientCode."<br/>".$remark;
  231. NotificationService::SingleRegister($msg,$clientCode,"订单管理-问题件");
  232. dd(1);
  233. $seeLog = SeeLog::query()->find(9);
  234. Notification::send(Auth::user(),new RoutineNotification($seeLog->toArray()));
  235. dd(1);
  236. Broadcast::channel('notification', function ($user, $id) {
  237. return (int) $user->id === (int) $id;
  238. });
  239. dd(1);
  240. Notification::send(Auth::user(),new RoutineNotification($seeLog->toArray()));
  241. dd(1);
  242. $username = config('database.connections.oracle.username');
  243. $password = config('database.connections.oracle.password');
  244. $host = config('database.connections.oracle.host');
  245. $service_name = config('database.connections.oracle.service_name');
  246. $conn = oci_connect($username, $password, $host . '/' . $service_name,"utf8");
  247. $sql = <<<sql
  248. BEGIN
  249. SPSO_HardAllocation_Process('WH01','Allocation','By OrderNO',
  250. null,'SO21090100007','0',null,
  251. 'zhouzhendong',:CODE);
  252. END;
  253. sql;
  254. $stmt = oci_parse($conn, $sql);
  255. $code = "";
  256. oci_bind_by_name($stmt, ':CODE', $code,300);
  257. oci_execute($stmt);
  258. oci_close($conn);
  259. dd($code);
  260. //
  261. $no = "SO21090900001";
  262. $db = DB::connection("oracle");
  263. //$order = $db->selectOne(DB::raw("select * from DOC_ORDER_HEADER where orderno = ?"),[$no]);
  264. dd($db->select($sql,[
  265. ]));
  266. $db->select("CALL SPSO_HardAllocation_Process(?,'Allocation','By OrderNO',null,?,'0',null,'zhouzhendong',@code)",[
  267. $order->warehouseid,$no
  268. ]);
  269. //SPSO_HardAllocation_Process
  270. dd($db->selectOne("select @code"));
  271. $date = "2021-09-14 15:47:00";
  272. dispatch(new TestJob("1"))->delay(Carbon::parse($date));
  273. dispatch(new TestJob("2"))->delay(Carbon::parse($date));
  274. dispatch(new TestJob("3"))->delay(Carbon::parse($date));
  275. dispatch(new TestJob("4"))->delay(Carbon::parse($date));
  276. dispatch(new TestJob("5"))->delay(Carbon::parse($date));
  277. dispatch(new TestJob("6"))->delay(Carbon::parse($date));
  278. dispatch(new TestJob("7"))->delay(Carbon::parse($date));
  279. dispatch(new TestJob("8"))->delay(Carbon::parse($date));
  280. dispatch(new TestJob("9"))->delay(Carbon::parse($date));
  281. dispatch(new TestJob("10"))->delay(Carbon::parse($date));
  282. dd(1);
  283. Station::query()->where("station_type_id", 5)->update(["status" => 1]);
  284. Cache::forget("CACHE_SHELF_AVAILABLE");
  285. $station = ["HAIB1-01-01", "HAIB1-02-01"];
  286. $material = ["IDE0001824", "IDE0001740", "IDE0002710"];
  287. Station::query()->whereIn("code", $station)->update(["status" => 0]);
  288. $stations = Station::query()->whereIn("code", $station)->get();
  289. $materials = MaterialBox::query()->whereIn("code", $material)->get();
  290. $dateTime = date("Y-m-d H:i:s");
  291. $task1 = StationTaskMaterialBox::query()->create([
  292. 'station_id' => $stations[0]->id,
  293. 'material_box_id' => $materials[0]->id,
  294. 'station_task_batch_id' => 1,
  295. 'status' => '待处理'
  296. ]);
  297. $task2 = StationTaskMaterialBox::query()->create([
  298. 'station_id' => $stations[1]->id,
  299. 'material_box_id' => $materials[1]->id,
  300. 'station_task_batch_id' => 1,
  301. 'status' => '待处理'
  302. ]);
  303. $task3 = StationTaskMaterialBox::query()->create([
  304. 'station_id' => 6,
  305. 'material_box_id' => $materials[2]->id,
  306. 'station_task_batch_id' => 1,
  307. 'status' => '待处理'
  308. ]);
  309. TaskTransaction::query()->insert([[
  310. "doc_code" => "test",
  311. "bar_code" => "test",
  312. "to_station_id" => $stations[0]->id,
  313. "material_box_id" => $materials[0]->id,
  314. "task_id" => $task1->id,
  315. "commodity_id" => 505012,//XUNI03
  316. "amount" => 1,
  317. "type" => "出库",
  318. "status" => 0,
  319. "mark" => 2,
  320. "bin_number" => 1,
  321. "created_at" => $dateTime,
  322. "updated_at" => $dateTime,
  323. ], [
  324. "doc_code" => "test",
  325. "bar_code" => "test",
  326. "to_station_id" => $stations[1]->id,
  327. "material_box_id" => $materials[1]->id,
  328. "task_id" => $task2->id,
  329. "commodity_id" => 505012,//XUNI03
  330. "amount" => 1,
  331. "type" => "出库",
  332. "status" => 0,
  333. "mark" => 2,
  334. "bin_number" => 1,
  335. "created_at" => $dateTime,
  336. "updated_at" => $dateTime,
  337. ], [
  338. "doc_code" => "test",
  339. "bar_code" => "test",
  340. "to_station_id" => 6,
  341. "material_box_id" => $materials[2]->id,
  342. "task_id" => $task3->id,
  343. "commodity_id" => 505012,//XUNI03
  344. "amount" => 1,
  345. "type" => "出库",
  346. "status" => 3,
  347. "mark" => 2,
  348. "bin_number" => 1,
  349. "created_at" => $dateTime,
  350. "updated_at" => $dateTime,
  351. ]]);
  352. $foreignHaiRoboticsService = new ForeignHaiRoboticsService();
  353. $toLocation = collect($station);
  354. $taskMaterialBoxes = collect([$task1, $task2]);
  355. $foreignHaiRoboticsService->
  356. fetchGroup_multiLocation($toLocation, $taskMaterialBoxes, '', '立架出至缓存架', 20, false);
  357. foreach ($toLocation as $index => $value) {
  358. app("CacheShelfService")->lightUp($value, '3', '0', ["title" => "机器人取箱中,禁止操作"]);
  359. Cache::forever("CACHE_SHELF_OCCUPANCY_{$stations[$index]->id}", true);
  360. }
  361. app("StationService")->locationOccupyMulti($toLocation->toArray());
  362. }
  363. public function update_order_packages_is_manual_update()
  364. {
  365. $descriptions = Log::query()
  366. ->select('description')
  367. ->whereBetween('created_at', ['2021-08-31 10:30:00', '2021-08-31 10:35:00'])
  368. ->where('class', 'like', 'https://was.baoshi56.com/package/logistic/batchUpdate%')->pluck('description');
  369. foreach ($descriptions as $description) {
  370. $description = substr($description, 9);
  371. $description = \Illuminate\Support\Str::before($description, "}");
  372. $obj = json_decode($description . '}', true);
  373. OrderPackage::query()
  374. ->whereIn('logistic_number', $obj['logistic_numbers'])
  375. ->update([
  376. 'status' => '无',
  377. 'is_manual_update' => false,
  378. ]);
  379. }
  380. }
  381. public function testUpdateInv()
  382. {
  383. ini_set('max_execution_time', 0);
  384. ini_set('memory_limit', '4096M');
  385. $sql = <<<sql
  386. select FMLOTNUM,FMLOCATION,PLANTOLOCATION,CREATE_TRANSACTIONID,SKU,CUSTOMERID from TSK_TASKLISTS
  387. where CUSTOMERID=?
  388. AND FMLOCATION= ?
  389. AND OPENWHO = ?
  390. AND TASKPROCESS = ?
  391. AND DOCTYPE = ?
  392. AND TASKTYPE = ?
  393. AND LOTATT05=?
  394. AND LOTATT08=?
  395. AND PLANTOID = ?
  396. and OPENTIME>=TO_DATE(?,'yyyy-mm-dd hh24:mi:ss')
  397. and OPENTIME<=TO_DATE(?,'yyyy-mm-dd hh24:mi:ss')
  398. sql;
  399. $CUSTOMERID = 'JIANSHANG';
  400. $FMLOCATION = 'STAGEWH02';
  401. $OPENWHO = 'WCS';
  402. $TASKPROCESS = '00';
  403. $DOCTYPE = 'ASN';
  404. $TASKTYPE = 'PA';
  405. $LOTATT05 = 'MJ-CP';
  406. $LOTATT08 = 'ZP';
  407. $PLANTOID = '*';
  408. $traceid = 'JIANSHANG03';
  409. $start = '2021-09-02 23:59:59';
  410. $end = '2021-09-03 11:10:00';
  411. $res = DB::connection("oracle")->select(DB::raw($sql),
  412. [$CUSTOMERID, $FMLOCATION, $OPENWHO, $TASKPROCESS, $DOCTYPE, $TASKTYPE, $LOTATT05, $LOTATT08, $PLANTOID, $start, $end]);
  413. $resItems = array_chunk($res, 200);
  414. foreach ($resItems as $res) {
  415. DB::connection("oracle")->beginTransaction();
  416. try {
  417. foreach ($res as $re) {
  418. DB::connection("oracle")->table('INV_LOT_LOC_ID')
  419. ->where([
  420. 'LOTNUM' => $re->fmlotnum,
  421. 'LOCATIONID' => $re->fmlocation,
  422. 'CUSTOMERID' => $re->customerid,
  423. 'sku' => $re->sku,
  424. 'TRACEID' => '*',
  425. ])
  426. ->update([
  427. 'TRACEID' => $traceid,
  428. 'EDITWHO' => 'WCS_',
  429. ]);
  430. if ($re->fmlocation != $re->plantolocation) {
  431. DB::connection("oracle")->table('INV_LOT_LOC_ID')
  432. ->where([
  433. 'LOTNUM' => $re->fmlotnum,
  434. 'LOCATIONID' => $re->plantolocation,
  435. 'CUSTOMERID' => $re->customerid,
  436. 'sku' => $re->sku,
  437. 'TRACEID' => '*',
  438. ])
  439. ->update([
  440. 'TRACEID' => $traceid,
  441. 'EDITWHO' => 'WCS_',
  442. ]);
  443. }
  444. DB::connection("oracle")->commit();
  445. }
  446. dd(true);
  447. } catch (\Exception $e) {
  448. DB::connection("oracle")->rollBack();
  449. dd($e->getMessage());
  450. }
  451. }
  452. }
  453. /**
  454. * 重置15天内的数据
  455. */
  456. public function order_packages_init()
  457. {
  458. OrderPackage::query()
  459. ->whereBetween('created_at', [now()->subDays(15), now()])
  460. ->update([
  461. 'is_delay_deliver' => 0,
  462. 'sync_routes_flag' => 0,
  463. 'status' => 1,
  464. 'exception_status' => 0,
  465. ]);
  466. OrderPackage::query()
  467. ->whereBetween('created_at', [now()->subDays(15), now()])
  468. ->whereNotNull('received_at')
  469. ->update([
  470. 'status' => 7,
  471. ]);
  472. }
  473. public function syncOrderPackage(){
  474. ini_set('memory_limit','500M');
  475. ini_set('max_execution_time', 0);
  476. $orderPackingSummary = OracleDocOrderPackingSummary::query()
  477. ->where('editTime','>=','2021-09-11 12:00:00')
  478. ->where('editTime','<=','2021-09-12 12:40:00')
  479. ->get();
  480. $orderPackingSummary_chunk = $orderPackingSummary->chunk(200);
  481. foreach ($orderPackingSummary_chunk as $orderPackingSummarys) {
  482. foreach ($orderPackingSummarys as $orderPackingSummary) {
  483. $orderPackage = OrderPackage::query()->where('logistic_number',$orderPackingSummary->traceid)->first();
  484. $orderPackage->update([
  485. 'uploaded_to_wms'=> true,
  486. 'weight'=>$orderPackingSummary->grossweight,
  487. 'length'=>$orderPackingSummary->length,
  488. 'width'=>$orderPackingSummary->width,
  489. 'height'=>$orderPackingSummary->height,
  490. 'weighed_at'=>$orderPackingSummary->edittime
  491. ]);
  492. dispatch(new WeightUpdateInstantBill($orderPackage));
  493. }
  494. }
  495. return ['success' => true];
  496. }
  497. public function collectUpload()
  498. {
  499. /** @var OrderPackageService $service */
  500. $service = app('OrderPackageService');
  501. return $service->collectUpload([
  502. '75810646867541',
  503. '75810646197516'
  504. ]);
  505. }
  506. public function teset12313(){
  507. dd(route("discharge.storeApi"));
  508. }
  509. public function init_SettlementBillReportTask()
  510. {
  511. $this->dispatch(new SettlementBillReportJob('2021-08-01',[]));
  512. }
  513. public function test2(){
  514. $source_file ="E:\OneDrive\桌面\工作目录\文件库\\5.jpg";
  515. $img = ImageCreateFromJpeg($source_file);
  516. imagecolortransparent($img);//将某个颜色设置成透明色
  517. imagecolorstotal($img);
  518. header('Content-type:image/jpeg');
  519. imagejpeg($img);
  520. //dd($this->run('E:\OneDrive\桌面\工作目录\文件库\5.jpg', 'E:\OneDrive\桌面\工作目录\文件库\6.jpg'));
  521. }
  522. const FILE_NOT_FOUND = '-1';
  523. const FILE_EXTNAME_ILLEGAL = '-2';
  524. public function run($src1, $src2) {
  525. if(!is_file($src1) || !is_file($src2)) exit(self::FILE_NOT_FOUND);
  526. $hash1 = $this->getHashValue($src1);
  527. $hash2 = $this->getHashValue($src2);
  528. if(strlen($hash1) !== strlen($hash2)) return false;
  529. $count = 0;
  530. $len = strlen($hash1);
  531. for($i = 0; $i < $len; $i++) if($hash1[$i] !== $hash2[$i]){
  532. $count++;
  533. }
  534. dd($count);
  535. return $count <= 10 ? true : false;
  536. }
  537. public function getImage($file) {
  538. $extname = pathinfo($file, PATHINFO_EXTENSION);
  539. if(!in_array($extname, ['jpg','jpeg','png','gif'])) exit(self::FILE_EXTNAME_ILLEGAL);
  540. $img = call_user_func('imagecreatefrom'. ( $extname == 'jpg' ? 'jpeg' : $extname ) , $file);
  541. return $img;
  542. }
  543. public function getHashValue($file) {
  544. $w = 32;
  545. $h = 32;
  546. $img = imagecreatetruecolor($w, $h);
  547. list($src_w, $src_h) = getimagesize($file);
  548. $src = $this->getImage($file);
  549. imagecopyresampled($img, $src, 0, 0, 0, 0, $w, $h, $src_w, $src_h);
  550. imagedestroy($src);
  551. $total = 0;
  552. $array = array();
  553. for( $y = 0; $y < $h; $y++) {
  554. for ($x = 0; $x < $w; $x++) {
  555. $rgb = imagecolorat($img, $x, $y);
  556. $gray = $rgb & 0xFF;
  557. /*dump([
  558. ($rgb >> 8) & 0xFF,
  559. ($rgb >> 16) & 0xFF,
  560. $rgb & 0xFF,
  561. ]);*/
  562. if(!isset($array[$y])) $array[$y] = array();
  563. $array[$y][$x] = $gray;
  564. $total += $gray;
  565. }
  566. }
  567. imagedestroy($img);
  568. $average = intval($total / ($w * $h * 2));
  569. $hash = '';
  570. for($y = 0; $y < $h; $y++) {
  571. for($x = 0; $x < $w; $x++) {
  572. $hash .= ($array[$y][$x] >= $average) ? '1' : '0';
  573. }
  574. }
  575. dump($hash);
  576. return $hash;
  577. }
  578. }