D
D
Dmitry Yakovlev2018-07-07 15:01:43
PHP
Dmitry Yakovlev, 2018-07-07 15:01:43

How to solve the problem of partial loading of images during parsing?

Hello.
When parsing, it happens that the image is saved partially loaded, that is, half of the image is a gray background.
I solve the problem with sleep(1).
Is there any other way to make sure the image is fully loaded before downloading?
Thank you.
Data retrieval method:

public function parseProduct(array $page)
    {
        $this->page = $page;

        foreach ($this->page as $item) {
            $phpQuery = phpQuery::newDocument($item->getBody()->getContents());
            $catalog = $phpQuery->find('.img_big_img');
            $re = '~<td>Артикул: <span.*?">(?<articul>.*?)</span>.*?<td>Наименование: <span.*?">(?<name>.*?)</span>.*?<td>Цена: <span.*?">(?<price>.*?)</span>~s';
            preg_match_all($re, $phpQuery, $matches, PREG_SET_ORDER, 0);
            $imageExtension = pathinfo(pq($catalog)->attr('src'), PATHINFO_EXTENSION );
            for($i = 0; $i < count($matches); $i++){
                $productDataArr[] = [
                    'imageSrc' => pq($catalog)->attr('src'),
                    'imageName' => $matches[$i]['name'],
                    'productPrice' => $matches[$i]['price'],
                    'productArticul' => 'БЧ' . $matches[$i]['articul'],
                    'imageExtension' => $imageExtension,
                ];
            }
        }
        return $productDataArr;
    }

Preservation:
public function saveProduct($imageDir, array $imageInfo)
    {

        if (!file_exists('cover/' . $imageDir)) {
            mkdir('cover/' . $imageDir, 0777, true);
        }
        foreach ($imageInfo as $item) {
            copy($item['imageSrc'], 'cover/' . $imageDir . '/' . Helper::transliterate($item['productArticul']) . '.' . $item['imageExtension']);
           // sleep(rand(2, 4));
        }
      //  $this->saveProductDataToExcel($imageInfo, $imageDir);

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vism, 2018-07-13
@Gikelan

try file_get_contents

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question