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]; }
/** * 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('/'); } } }
public function upload($form_data) { $validator = Validator::make($form_data, ImageModel::$rules, ImageModel::$messages); if ($validator->fails()) { return Response::json(['error' => true, 'message' => $validator->messages()->first(), 'code' => 400], 400); } $photo = $form_data['file']; $originalName = $photo->getClientOriginalName(); $originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - 4); $filename = $this->sanitize($originalNameWithoutExt); $allowed_filename = $this->createUniqueFilename($filename); $filenameExt = $allowed_filename . '.jpg'; $uploadSuccess1 = $this->original($photo, $filenameExt); $uploadSuccess2 = $this->icon($photo, $filenameExt); if (!$uploadSuccess1 || !$uploadSuccess2) { return Response::json(['error' => true, 'message' => 'Server error while uploading ' . $filename . " " . $allowed_filename, 'code' => 500], 500); } $sessionImage = new ImageModel(); $sessionImage->filename = $allowed_filename; $sessionImage->original_name = $originalName; $sessionImage->save(); return Response::json(['error' => false, 'code' => 200], 200); }