BingBilder.php 3.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace app\Models\parserSkripte;

use App\Models\Searchengine;
use Log;

class BingBilder extends Searchengine
{
    public $results = [];

12
    public function __construct($name, \stdClass $engine, \App\MetaGer $metager)
13
    {
14
        parent::__construct($name, $engine, $metager);
15
16
17
18
19
20
    }

    public function loadResults($result)
    {
        try {
            $results = json_decode($result);
21
            $this->totalResults = $results->totalEstimatedMatches;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
            $results = $results->value;

            foreach ($results as $result) {
                $title = $result->name;
                $link = $result->hostPageUrl;
                $anzeigeLink = $link;
                $descr = "";
                $image = $result->thumbnailUrl;
                $this->counter++;
                $this->results[] = new \App\Models\Result(
                    $this->engine,
                    $title,
                    $link,
                    $anzeigeLink,
                    $descr,
37
38
                    $this->engine->{"display-name"},
                    $this->engine->homepage,
39
                    $this->counter,
40
41
42
43
44
45
46
                    [
                        'image' => $image,
                        'imagedimensions' => [
                            "width" => $result->width,
                            "height" => $result->height
                        ]
                    ]
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
                );
            }
        } catch (\Exception $e) {
            Log::error("A problem occurred parsing results from $this->name:");
            Log::error($e->getMessage());
            return;
        }
    }

    public function getNext(\App\MetaGer $metager, $result)
    {
        try {
            $results = json_decode($result);

            $totalMatches = $results->totalEstimatedMatches;
            $nextOffset = $results->nextOffset;

            if ($nextOffset >= $totalMatches) {
                return;
            }

68
69
70
            $newEngine = unserialize(serialize($this->engine));
            $newEngine->{"get-parameter"}->offset = $nextOffset;
            $next = new BingBilder($this->name, $newEngine, $metager);
71
72
73
74
75
76
            $this->next = $next;
        } catch (\Exception $e) {
            Log::error("A problem occurred parsing results from $this->name:");
            Log::error($e->getMessage());
            return;
        }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    }

    public static function generateThumbnailUrl(\App\Models\Result $result)
    {
        $url = $result->image;

        // all images get cropped to a width of 400 px
        // We will calculate the height after cropping
        // All Images in a row get 250px 
        // If the height after cropping is a multiple of that plus the gap between rows
        // we will allow the image to span up to three rows
        $newWidth = 400;
        $newHeight = 250;
        $heightMultiplier = 1; // Can be 1..3 in the end
        $gapPixels = 8;

        $width = $result->imageDimensions["width"];
        $height = $result->imageDimensions["height"];

        $heightAfterCrop = $height * (400 / $width);
        $heightMultiplier = max(1, min(3, floor($heightAfterCrop / 250)));

        $newHeight = $newHeight * $heightMultiplier + (($heightMultiplier - 1) * $gapPixels);

        $requestDataBing = [
            "w" => $newWidth,
            "h" => $newHeight,
            "c" => 7, // Smart Cropping
        ];

        $requestDataBing = http_build_query($requestDataBing, "", "&", PHP_QUERY_RFC3986);
        $url .= "&" . $requestDataBing;
109

110
111
112
113
114
115
116
        $requestData = [];
        $requestData["url"] = $url;
        $link = action('Pictureproxy@get', $requestData);
        return [
            "link" => $link,
            "height-multiplier" => $heightMultiplier
        ];
117
118
    }
}