Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
used yii2-httpclient
Browse files Browse the repository at this point in the history
  • Loading branch information
himiklab committed Mar 15, 2018
1 parent 045703f commit 96776b1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 45 deletions.
7 changes: 0 additions & 7 deletions EasyThumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,15 @@
*/
class EasyThumbnail extends BaseObject
{
const GRABBER_PHP = 1;
const GRABBER_CURL = 2;

/** @var string $cacheAlias path alias relative with @web where the cache files are kept */
public $cacheAlias = 'assets/thumbnails';

/** @var integer $cacheExpire seconds */
public $cacheExpire = 0;

/** @var integer */
public $grabberType = self::GRABBER_PHP;

public function init()
{
EasyThumbnailImage::$cacheAlias = $this->cacheAlias;
EasyThumbnailImage::$cacheExpire = $this->cacheExpire;
EasyThumbnailImage::$grabberType = $this->grabberType;
}
}
50 changes: 13 additions & 37 deletions EasyThumbnailImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use yii\base\InvalidConfigException;
use yii\helpers\FileHelper;
use yii\helpers\Html;
use yii\httpclient\Client;
use yii\imagine\Image;

/**
Expand All @@ -37,8 +38,6 @@ class EasyThumbnailImage
/** @var int $cacheExpire */
public static $cacheExpire = 0;

public static $grabberType = EasyThumbnail::GRABBER_PHP;

/**
* Creates and caches the image thumbnail and returns ImageInterface.
*
Expand Down Expand Up @@ -248,29 +247,14 @@ protected static function errorHandler($error, $filename)
*/
protected static function fileFromUrlDate($url)
{
if (static::$grabberType === EasyThumbnail::GRABBER_PHP) {
$streamContextDefaults = stream_context_get_options(stream_context_get_default());
stream_context_set_default(['http' => ['method' => 'HEAD']]);
if (($headers = @get_headers($url, 1)) === false || strpos($headers[0], '200') === false) {
stream_context_set_default($streamContextDefaults);
throw new FileNotFoundException("URL {$url} doesn't exist");
}
stream_context_set_default($streamContextDefaults);

return isset($headers['Last-Modified']) ? $headers['Last-Modified'] : '';
}

// curl
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_FILETIME => true,
CURLOPT_NOBODY => true,
]);
if (curl_exec($curl) === false) {
$response = (new Client())
->head($url)
->send();
if (!$response->isOk) {
throw new FileNotFoundException("URL {$url} doesn't exist");
}

return curl_getinfo($curl, CURLINFO_FILETIME);
return $response->headers['Last-Modified'];
}

/**
Expand All @@ -280,23 +264,15 @@ protected static function fileFromUrlDate($url)
*/
protected static function fileFromUrlContent($url)
{
if (static::$grabberType === EasyThumbnail::GRABBER_PHP) {
if (($result = @file_get_contents($url)) === false) {
throw new FileNotFoundException("URL {$url} doesn't exist");
}

return $result;
}

// curl
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
]);
if (!($result = curl_exec($curl))) {
$response = (new Client())
->createRequest()
->setMethod('GET')
->setUrl($url)
->send();
if (!$response->isOk) {
throw new FileNotFoundException("URL {$url} doesn't exist");
}

return $result;
return $response->content;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
],
"require": {
"yiisoft/yii2": "~2.0.13",
"yiisoft/yii2-imagine": "*"
"yiisoft/yii2-imagine": "*",
"yiisoft/yii2-httpclient": "*"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 96776b1

Please sign in to comment.