You are here

function shrinktheweb_getCachedThumbnail in ShrinkTheWeb 8

Same name and namespace in other branches
  1. 6 shrinktheweb.api.inc \shrinktheweb_getCachedThumbnail()
  2. 7 shrinktheweb.api.inc \shrinktheweb_getCachedThumbnail()

Get a thumbnail, caching it first if possible.

Parameters

null $aArgs:

Return value

bool|string

Throws

\Exception

1 call to shrinktheweb_getCachedThumbnail()
shrinktheweb_getThumbnail in ./shrinktheweb.api.inc
Gets the thumbnail for the specified website, stores it in the cache, and then returns the relative path to the cached image.

File

./shrinktheweb.api.inc, line 353

Code

function shrinktheweb_getCachedThumbnail($aArgs = NULL) {
  $aArgs = is_array($aArgs) ? $aArgs : array();

  // Use arguments to work out the target filename.
  $sFilename = shrinktheweb_generateHash($aArgs) . '.jpg';
  $sFile = THUMBNAIL_DIR . $sFilename;
  $sReturnName = FALSE;

  // Work out if we need to update the cached thumbnail.
  $iForceUpdate = $aArgs['stwredo'] ? TRUE : FALSE;
  if ($iForceUpdate || shrinktheweb_cacheFileExpired($sFile)) {

    // If bandwidth limit has reached return the BANDWIDTH_IMAGE
    if (shrinktheweb_checkLimitReached(THUMBNAIL_DIR . BANDWIDTH_IMAGE)) {
      $sFilename = BANDWIDTH_IMAGE;
    }
    elseif (shrinktheweb_checkLimitReached(THUMBNAIL_DIR . QUOTA_IMAGE)) {
      $sFilename = QUOTA_IMAGE;
    }
    elseif (shrinktheweb_checkLimitReached(THUMBNAIL_DIR . NO_RESPONSE_IMAGE)) {
      $sFilename = NO_RESPONSE_IMAGE;
    }
    else {

      // Check if the thumbnail was captured.
      $aImage = shrinktheweb_checkWebsiteThumbnailCaptured($aArgs);
      switch ($aImage['status']) {
        case 'save':

          // Download the image to local path.
          shrinktheweb_downloadRemoteImageToLocalPath($aImage['url'], $sFile);
          break;
        case 'nosave':

          // Don't save the image but return the url.
          return $aImage['url'];
          break;
        case 'quota_exceed':

          // Download the image to local path for locking requests.
          $sFilename = QUOTA_IMAGE;
          $sFile = THUMBNAIL_DIR . $sFilename;
          shrinktheweb_downloadRemoteImageToLocalPath($aImage['url'], $sFile);
          break;
        case 'bandwidth_exceed':

          // Download the image to local path for locking requests.
          $sFilename = BANDWIDTH_IMAGE;
          $sFile = THUMBNAIL_DIR . $sFilename;
          shrinktheweb_downloadRemoteImageToLocalPath($aImage['url'], $sFile);
          break;
        default:

          // Otherwise return the status.
          return $aImage['status'];
      }
    }
  }
  $sFile = THUMBNAIL_DIR . $sFilename;

  // Check if file exists.
  if (file_exists($sFile)) {
    $sReturnName = THUMBNAIL_URI . $sFilename;
  }
  if ($iForceUpdate) {
    return $aImage['captured_on'];
  }
  else {
    return $sReturnName;
  }
}