D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
tattooscyy
/
test
/
app
/
Http
/
Controllers
/
Filename :
PhotoController.php
back
Copy
<?php namespace App\Http\Controllers; use File; use Auth; use Response; use Log; use App\Picture; use Intervention\Image\ImageManager; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Validator; use App\Http\Requests; class PhotoController extends Controller { protected $upload_path; protected $avatar_path; public function __construct() { $this->site_path = '/public/uploader'; $this->server_path = base_path() . '/public/uploader'; } public function getIndex($id = null) { $pic = Picture::find($id); return view('picture', compact('pic')); } public function upload() { $data = Input::all(); $validator = Validator::make($data, Pictures::$rules, Image::$messages); if ($validator->fails()) { return Response::json([ 'status' => 'error', 'message' => $validator->messages()->first(), ], 200); } $photo = $data['img']; $original_name = $photo->getClientOriginalName(); $original_name_without_ext = substr($original_name, 0, strlen($original_name) - 4); $filename = $this->sanitize($original_name_without_ext); $allowed_filename = $this->createUniqueFilename($filename); $filename_ext = $allowed_filename . '.jpg'; $manager = new ImageManager(); $image = $manager->make($photo) ->resize(300, null, function ($constraint) { $constraint->aspectRatio(); }) ->encode('jpg') ->save($this->upload_path . '/' . $filename_ext); if (!$image) { return Response::json([ 'status' => 'error', 'message' => 'Server error while uploading', ], 200); } return Response::json([ 'status' => 'success', 'url' => env('UPLOAD_URL') . '/' . $filename_ext, 'width' => $image->width(), 'height' => $image->height() ], 200); } public function crop() { $data = Input::all(); $image_url = $data['imgUrl']; // resized sizes $imgW = $data['imgW']; $imgH = $data['imgH']; // offsets $imgY1 = $data['imgY1']; $imgX1 = $data['imgX1']; // crop box $cropW = $data['cropW']; $cropH = $data['cropH']; // rotation angle // dd($data); $angle = $data['rotation']; $filename_array = explode('/', $image_url); $filename = $filename_array[sizeof($filename_array) - 1]; if (!Auth::guest()) { $pic_obj = Picture::where('user_id', Auth::user()->id)->where('showhide', 'show')->orderBy('id', 'DESC')->first(); if (isset($pic_obj)) { $filename = $pic_obj->picture; } $filepath = $this->server_path . '/' . Auth::user()->id . '/' . $filename; $dir = $this->server_path . '/' . Auth::user()->id; $auth = Auth::user()->id; if (!file_exists($dir)) { mkdir($dir, 0777, true); } //dd($filepath); if (file_exists($filepath)) { $site_puth = $this->site_path . '/' . Auth::user()->id; } elseif (file_exists($this->server_path . '/' . $filename)) { $filepath = $this->server_path . '/' . $filename; $dir = $this->server_path; $site_puth = $this->site_path; } } else { $pic_obj = Picture::where('ip', $_SERVER['REMOTE_ADDR'])->where('showhide', 'show')->orderBy('id', 'DESC')->first(); if (isset($pic_obj)) { $filename = $pic_obj->picture; } else { $filename = '1502341206-realism.jpg'; } $filepath = $this->server_path . '/' . $filename; $dir = $this->server_path; $site_puth = $this->site_path; $auth = 0; } $manager = new ImageManager(); $image = $manager->make($filepath); $image->resize($imgW, $imgH) ->rotate(-$angle) ->crop($cropW, $cropH, $imgX1, $imgY1) ->save($dir . '/' . $filename); if (!$image) { return Response::json([ 'status' => 'error', 'message' => 'Server error while cropping', ], 200); } $obj = new Picture; $obj->picture = $filename; $obj->showhide = 'show'; $obj->user_id = $auth; $obj->ip = $_SERVER['REMOTE_ADDR']; $obj->save(); setcookie("picture", $filename, time() + 3600, '/'); return Response::json([ 'status' => 'success', 'url' => $site_puth . '/' . $filename ], 200); } private function sanitize($string, $force_lowercase = true, $anal = false) { $strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]", "}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—", "—", "–", ",", "<", ".", ">", "/", "?"); $clean = trim(str_replace($strip, "", strip_tags($string))); $clean = preg_replace('/\s+/', "-", $clean); $clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean; return ($force_lowercase) ? (function_exists('mb_strtolower')) ? mb_strtolower($clean, 'UTF-8') : strtolower($clean) : $clean; } private function createUniqueFilename($filename) { $upload_path = env('UPLOAD_PATH'); $full_image_path = $upload_path . '/' . $filename . '.jpg'; if (File::exists($full_image_path)) { // Generate token for image $image_token = substr(sha1(mt_rand()), 0, 5); return $filename . '-' . $image_token; } return $filename; } }