public function getImage($width_height, $file_name) { $media_id = (int) \Input::get('mid'); $canvas_color = 'FFFFFF'; if ($media_id) { $media_model = \App\Models\Media::findOrFail($media_id); $canvas_color = $media_model->canvas_color; if (empty($media_model->file_name) and !empty($media_model->source_url)) { $file_extention = 'jpg'; $file_name = md5(microtime() . rand(0, 1000)) . '.' . $file_extention; $source_url = $media_model->source_url; if (substr($source_url, 0, 7) != 'http://' and substr($source_url, 0, 8) != 'https://') { $source_url = 'http://' . $source_url; } \Storage::put('media/images/' . $file_name, file_get_contents($source_url)); $media_model->file_name = $file_name; $media_model->save(); } } $width_height_arr = explode('x', $width_height); $width = $width_height_arr[0]; $height = $width_height_arr[1]; $image_path = $this->getOriginDir() . $file_name; if (!file_exists($image_path)) { $img = Image::canvas($width, $height); $img->text('Image not found ;{', 110, 110, function ($font) { $font->size(48); $font->align('center'); }); return $img->response('jpg'); } $img = Image::make($image_path); $source_width = $img->width(); $source_height = $img->height(); if ($source_width < $source_height) { if ($width = $height or $width > $height) { // resize H $img->heighten($height); } } elseif ($source_width > $source_height) { if ($width = $height or $width < $height) { // resize V $img->widen($width); } } else { if ($width > $height) { // resize H $img->heighten($height); } if ($width < $height) { // resize V $img->widen($width); } } $img->resizeCanvas($width, $height, 'center', false, $canvas_color); //$img->fit($width, $height); $storage_dir = base_path() . '/public/media/images/' . $width_height; $storage_file = $storage_dir . '/' . $file_name; if (!file_exists($storage_dir)) { mkdir($storage_dir); } $img->save($storage_file); return $img->response('jpg'); }