Skip to content

Commit

Permalink
fix: catchImage #19
Browse files Browse the repository at this point in the history
  • Loading branch information
eddy8 committed Feb 7, 2021
1 parent 7619d40 commit 4e692e2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
62 changes: 58 additions & 4 deletions app/Http/Controllers/Admin/NEditorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Intervention\Image\Exception\NotReadableException;

class NEditorController extends Controller
{
Expand Down Expand Up @@ -78,13 +80,17 @@ public function catchImage(Request $request)
return call_user_func([new $class, 'catchImage'], $request);
}

$files = (array) $request->post('file');
$files = array_unique((array) $request->post('file'));
$urls = [];
foreach ($files as $v) {
$extention = pathinfo(parse_url($v, PHP_URL_PATH), PATHINFO_EXTENSION);
$path = date('Ym') . '/' . md5($v) . '.' . ($extention == '' ? 'jpg' : $extention);
$image = $this->fetchImageFile($v);
if (!$image || !$image['extension'] || !$this->isAllowedImageType($image['extension'])) {
continue;
}

$path = date('Ym') . '/' . md5($v) . '.' . $image['extension'];
Storage::disk(config('light.neditor.disk'))
->put($path, file_get_contents($v));
->put($path, $image['data']);
$urls[] = [
'url' => Storage::disk(config('light.neditor.disk'))->url($path),
'source' => $v,
Expand Down Expand Up @@ -222,4 +228,52 @@ protected function isValidUploadedFile(UploadedFile $file, array $config)

return true;
}

protected function fetchImageFile($url)
{
try {
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return false;
}

if (extension_loaded('curl')) {
$ch = curl_init();
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2'
];
curl_setopt_array($ch, $options);
$data = curl_exec($ch);
curl_close($ch);
if (!$data) {
return false;
}

if (isWebp($data)) {
$image = Image::make(imagecreatefromwebp($url));
$extension = 'webp';
} else {
$image = Image::make($data);
}
} else {
$image = Image::make($url);
}
} catch (NotReadableException $e) {
return false;
}

$mime = $image->mime();
return [
'extension' => $extension ?? ($mime ? strtolower(explode('/', $mime)[1]) : ''),
'data' => $data
];
}

protected function isAllowedImageType($extension)
{
$c = config('light.neditor.upload');

return in_array('.' . $extension, $c['imageAllowFiles'], true);
}
}
9 changes: 9 additions & 0 deletions app/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,12 @@ function checkSensitiveWords(string $text, $type = 'join', $mode = null)
}
return array_merge($return, $result['exclusive']);
}

function isWebp($data)
{
if (strncmp(substr($data, 8, 7), "WEBPVP8", 7) === 0) {
return true;
}

return false;
}

0 comments on commit 4e692e2

Please sign in to comment.