/** * Take screenshot * * @return */ function take_screenshot($hyperlink_id, $screenshot_id = FALSE) { //** //** Get info about link //** $query = $this->EE->db->select('*')->from('exp_hyperlink')->where('hyperlink_id', $hyperlink_id)->where('hyperlink_http_status >=', 200)->where('hyperlink_http_status <', 400)->get(); if ($query->num_rows == 0) { return FALSE; } $hyperlink = $query->row_array(); //** //** Load settings //** $this->_load_settings($hyperlink["site_id"]); //** //** No Dir, No Screenshot //** if (!$this->settings['screenshot_dir']) { return FALSE; } //** //** No Service, No Screenshot //** if (!$this->settings['screenshot_service']) { return FALSE; } //** --------------------------------------------- //** GrabzIt //** --------------------------------------------- if ($this->settings['screenshot_service'] == 'GrabzIt') { include_once "grabzit/GrabzItClient.class.php"; //if EXPIRED, get new request if ($hyperlink['screenshot_status'] == 'expired') { $screenshot_id = false; } try { //one connect is ok global $__GRABZIT_CLIENT__; if (!isset($__GRABZIT_CLIENT__)) { $__GRABZIT_CLIENT__ = new GrabzItClient($this->settings['screenshot_services']['GrabzIt']['api_key'], $this->settings['screenshot_services']['GrabzIt']['api_secret']); } if (!$screenshot_id) { $screenshot_id = (string) $__GRABZIT_CLIENT__->TakePicture($hyperlink['hyperlink_url'], null, null, $this->settings['screenshot_services']['GrabzIt']['browser_width'], $this->settings['screenshot_services']['GrabzIt']['browser_height'], $this->settings['screenshot_services']['GrabzIt']['image_width'], $this->settings['screenshot_services']['GrabzIt']['image_height']); if ($screenshot_id) { $data = array('screenshot_id' => $screenshot_id, 'screenshot_status' => 'send'); } else { $data = array('screenshot_error' => 'GrabzIt request failed', 'screenshot_status' => 'error'); } $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id); } $screenshotTaked = 0; //get status if ($screenshot_id) { $status = $__GRABZIT_CLIENT__->GetStatus($screenshot_id); if ($status->Processing) { $data = array('screenshot_id' => $screenshot_id, 'screenshot_status' => 'processing'); $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id); } if ($status->Cached) { $data = array('screenshot_id' => $screenshot_id, 'screenshot_status' => 'cached'); $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id); $screenshotTaked = 1; } if ($status->Expired) { $data = array('screenshot_status' => 'expired', 'screenshot_error' => $status->Message); $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id); //log errors $this->errors[] = $status->Message; } } if ($screenshotTaked) { $screenshot = $__GRABZIT_CLIENT__->GetPicture($screenshot_id); $screenshot_name = 'screenshot-grabzit-' . uniqid() . '.jpg'; $screenshot_dir = $this->settings['screenshot_dir']; if ($this->save_image($screenshot_name, $screenshot, $screenshot_dir)) { $data = array('screenshot_name' => $screenshot_name, 'screenshot_dir' => $screenshot_dir, 'screenshot_status' => 'ok', 'screenshot_error' => null); $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id); } } } catch (Exception $e) { $this->errors[] = 'Caught exception: ' . $e->getMessage(); $data = array('screenshot_status' => 'error', 'screenshot_error' => $e->getMessage()); $this->EE->db->update('exp_hyperlink', $data, 'hyperlink_id = ' . (int) $hyperlink_id); return FALSE; } } }