Created
January 6, 2017 00:50
-
-
Save jcorry/769aa4bf40474f13396708657fbd5311 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: jcorry | |
* Date: 1/2/17 | |
* Time: 1:16 PM | |
*/ | |
namespace App; | |
use GuzzleHttp\Client; | |
use Illuminate\Support\Facades\Log; | |
use Illuminate\Support\Facades\Storage; | |
use Illuminate\Support\Facades\File; | |
use Intervention\Image\ImageManagerStatic as Image; | |
class ImageSaver | |
{ | |
public function __construct() | |
{ | |
\Datadogstatsd::configure(env("DDOG_API_KEY"), 0, "https://app.datadoghq.com", "UDP", env("DDOG_AGENT_HOST")); | |
} | |
public function emptyStorageDir() | |
{ | |
$files = File::allFiles(storage_path('images/raw')); | |
\Datadogstatsd::increment('storage_file_remove_count', count($files)); | |
foreach($files as $file) { | |
Log::info('deleting file: ' . $file); | |
File::delete($file); | |
} | |
} | |
public function downloadImageToStorage($url) | |
{ | |
$path = parse_url($url, PHP_URL_PATH); | |
$fileName = pathinfo($path, PATHINFO_FILENAME) . '.' . pathinfo($path, PATHINFO_EXTENSION); | |
$startTime = microtime(true); | |
$client = new Client(); | |
try { | |
$response = $client->get($url, [ | |
'sink' => storage_path('images/raw/') . $fileName | |
]); | |
\Datadogstatsd::timing('image_download_time', microtime(true) - $startTime); | |
$file = storage_path('images/raw/') . $fileName; | |
// register the number of bytes downloaded | |
\Datadogstatsd::histogram('image_download_bytes', filesize($file)); | |
\Datadogstatsd::increment('image_download_count', 1); | |
echo('Filesize: ' . filesize($file)) . '<br />'; | |
echo('Download time: ' . (microtime(true) - $startTime) . 's' . '<br />'); | |
echo('Filename: ' . $fileName . '<br />'); | |
return $fileName; | |
} catch (\Exception $e) { | |
\Datadogstatsd::event($e->getMessage(), [ | |
'alert_type' => 'error', | |
'aggregation_key' => 'download_failed' | |
]); | |
echo $e->getMessage(); | |
} | |
} | |
public function resizeImage($filename) | |
{ | |
$file = storage_path('images/raw/') . $filename; | |
$startTime = microtime(true); | |
$image = Image::make($file)->resize(300)->save(storage_path('images/resized/') . $filename); | |
\Datadogstatsd::timing('image_resized', microtime(true) - $startTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment