- if (!DBA::isResult($photo)) {
- // It shouldn't happen but it does - spaces in URL
- $_REQUEST['url'] = str_replace(' ', '+', $_REQUEST['url']);
- $redirects = 0;
- $fetchResult = Network::fetchUrlFull($_REQUEST['url'], true, $redirects, 10);
- $img_str = $fetchResult->getBody();
-
- $tempfile = tempnam(get_temppath(), 'cache');
- file_put_contents($tempfile, $img_str);
- $mime = mime_content_type($tempfile);
- unlink($tempfile);
-
- // If there is an error then return a blank image
- if ((substr($fetchResult->getReturnCode(), 0, 1) == '4') || (!$img_str)) {
- $img_str = file_get_contents('images/blank.png');
- $mime = 'image/png';
- $cachefile = ''; // Clear the cachefile so that the dummy isn't stored
- $valid = false;
- $image = new Image($img_str, 'image/png');
-
- if ($image->isValid()) {
- $image->scaleDown(10);
- $img_str = $image->asString();
- }
- } elseif ($mime != 'image/jpeg' && !$direct_cache && $cachefile == '') {
- $image = @imagecreatefromstring($img_str);
-
- if ($image === FALSE) {
- die();
- }
-
- $fields = ['uid' => 0, 'contact-id' => 0, 'guid' => System::createGUID(), 'resource-id' => $urlhash, 'created' => DateTimeFormat::utcNow(), 'edited' => DateTimeFormat::utcNow(),
- 'filename' => basename($_REQUEST['url']), 'type' => '', 'album' => '', 'height' => imagesy($image), 'width' => imagesx($image),
- 'datasize' => 0, 'data' => $img_str, 'scale' => 100, 'profile' => 0,
- 'allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '', 'desc' => $mime];
- DBA::insert('photo', $fields);
- } else {
- $image = new Image($img_str, $mime);
-
- if ($image->isValid() && !$direct_cache && ($cachefile == '')) {
- Photo::store($image, 0, 0, $urlhash, $_REQUEST['url'], '', 100);
- }
- }
- }