/**
  * Upload the image, resize and store request in DB
  *
  * @param  \Illuminate\Http\Request  $request
  */
 public function upload(Request $request)
 {
     $validator = Validator::make($request->all(), ['image' => 'required|mimes:jpeg,bmp,png', 'width' => 'required', 'height' => 'required']);
     if ($validator->fails()) {
         return redirect('/')->withErrors($validator)->withInput();
     } else {
         $_image = $request->file('image');
         $_width = $request->input('width');
         $_height = $request->input('height');
         $extension = $_image->getClientOriginalExtension();
         $fileName = time() . '_' . md5(rand(0, 9999)) . '.' . $extension;
         if ($_image->isValid()) {
             // Uploading file to given path
             $_image->move($this->upload_path, $fileName);
             // Resize the uploaded image
             $image = new ImageResize($this->upload_path . '/' . $fileName);
             $image->resize($_width, $_height);
             $image->save($this->upload_path . '/' . $fileName);
             // Save the request info
             $model = new ImageModel();
             $model->filename = $_image->getClientOriginalName();
             $model->width = $_width;
             $model->height = $_height;
             $model->save();
             // Delete the uploaded file
             $file_contents = file_get_contents($this->upload_path . '/' . $fileName);
             File::delete($this->upload_path . '/' . $fileName);
             return response($file_contents)->header('Content-Type', 'image/jpg')->header('Content-Transfer-Encoding', 'Binary')->header('Content-disposition', 'attachment; filename="' . $_image->getClientOriginalName() . '"');
         } else {
             Session::flash('error', 'Something went wrong while uploading the image.');
             return redirect('/');
         }
     }
 }
 private function getBrazzifiedImage($url, $logo_loc)
 {
     $valid_logo_locations = ['top-left', 'top', 'top-right', 'left', 'center', 'right', 'bottom-left', 'bottom', 'bottom-right'];
     try {
         $img = Image::make($url);
     } catch (\Exception $e) {
         return ['error' => 'Image or URL is not valid.'];
     }
     if (!in_array($logo_loc, $valid_logo_locations)) {
         return ['error' => 'Not a valid logo location.'];
     }
     $img->insert(base_path() . '/resources/images/brazzers-logo.png', $logo_loc, 10, 10);
     $finished_image = $img->encode('png');
     $image_hash = hash('sha256', $finished_image . $logo_loc);
     $existing_image = ImageModel::where('hash', $image_hash)->first();
     if ($existing_image) {
         $imgur_id = $existing_image->image_host_id;
     } else {
         $client = new Client(['base_uri' => 'https://api.imgur.com', 'timeout' => 10, 'headers' => ['Authorization' => 'Client-ID ' . env('IMGUR_CLIENT_ID')]]);
         $response = $client->request('POST', '3/image', ['form_params' => ['image' => base64_encode($finished_image), 'type' => 'base64', 'description' => 'Image generated by http://brazzify.me']]);
         if ($response->getStatusCode() != 200) {
             return ['error' => 'Error uploading image to imgur.'];
         }
         $imgur_response = json_decode($response->getBody()->getContents());
         $imgur_id = $imgur_response->data->id;
         $image_model = new ImageModel();
         $image_model->hash = $image_hash;
         $image_model->image_host = 1;
         $image_model->image_host_id = $imgur_id;
         $image_model->save();
     }
     return ['imgur_id' => $imgur_id];
 }
 /**
  * Delete Image From Session folder, based on original filename
  */
 public function delete($originalFilename)
 {
     $full_size_dir = Config::get('images.full_size');
     $icon_size_dir = Config::get('images.icon_size');
     $sessionImage = ImageModel::where('original_name', 'like', $originalFilename)->first();
     if (empty($sessionImage)) {
         return Response::json(['error' => true, 'code' => 400], 400);
     }
     $full_path1 = $full_size_dir . $sessionImage->filename . '.jpg';
     $full_path2 = $icon_size_dir . $sessionImage->filename . '.jpg';
     if (File::exists($full_path1)) {
         File::delete($full_path1);
     }
     if (File::exists($full_path2)) {
         File::delete($full_path2);
     }
     if (!empty($sessionImage)) {
         $sessionImage->delete();
     }
     return Response::json(['error' => false, 'code' => 200], 200);
 }
Example #4
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     $model = new ImageModel();
     $_items = $model->orderBy('created_at', 'desc')->get();
     return view('admin')->with(array('items' => $_items));
 }
 public function modal_images()
 {
     $images = ImageModel::orderBy('id', 'desc')->limit(40)->get();
     return view('admin.modal_images', compact('images'));
 }