public static function getImage($name) { $color = Color::where('name', '=', invert_snake_case(str_replace('_', ' _', $name)))->first(); return $color->image; }
public function loadImage() { $args = func_get_args(); if (count($args) == 0) { abort(404); } $model = invert_snake_case(array_shift($args), "-"); if ($model == 'Offline.jpg') { $data = file_get_contents(dirname(__FILE__) . '/offline.jpg'); return response($data, 200)->header('Content-Type', 'image/jpeg'); } // Find the model $existingModel = glob(dirname(dirname(dirname(dirname(__FILE__)))) . '/Models/*/' . $model . '.php'); if (count($existingModel) > 0) { $model = "App" . str_replace('/', '\\', substr($existingModel[0], strlen(dirname(dirname(dirname(dirname(__FILE__))))), -4)); } if (empty($model)) { abort(404); } if (!class_exists($model)) { abort(404); } if (!method_exists($model, 'getImage')) { abort(404); } // Remove profile from request $match = preg_match('/(.*?)(\\.[-a-z0-9]+)?(\\.[A-Za-z0-9]+)/', $args[count($args) - 1], $filenameParts); if (!$match) { abort(404); } $args[count($args) - 1] = $filenameParts[1]; $profile = $filenameParts[2]; $extension = substr($filenameParts[3], 1); if (!empty($profile)) { // The above preg_match will prefix profile with a . $profile = substr($profile, 1); } $image = forward_static_call_array(array($model, 'getImage'), $args); if ($image) { $path = urldecode(Request::path()); // Add known path $knownPaths = json_decode($image->known_paths); if ($knownPaths == null) { $knownPaths = []; } array_push($knownPaths, $path); $image->known_paths = json_encode($knownPaths); $image->save(); $data = $image->profile($profile)->data($extension); $dir = dirname(public_path($path)); if (!is_dir($dir)) { mkdir($dir, 0777, true); } file_put_contents(public_path($path), $data); if ($extension == 'jpg') { $extension = 'jpeg'; } // For MIME type reasons return response($data, 200)->header('Content-Type', 'image/' . strtolower($extension)); } else { abort(404); } }