public function redactorUploadImage() { $file = Input::file('file'); $uploadPath = public_path('uploads'); if (Input::hasFile('file')) { $fileName = str_random(16) . '.' . Input::file('file')->getClientOriginalExtension(); if (!File::exists($uploadPath . '/thumbnail')) { File::makeDirectory($uploadPath . '/thumbnail', 0777, TRUE); } /* ImageManipulation::make(Input::file('file')->getRealPath())->resize(100, 100)->save($uploadPath . '/thumbnail/thumb_' . $fileName); ImageManipulation::make(Input::file('file')->getRealPath())->resize(600, 600)->save($uploadPath . '/' . $fileName); */ ## Pathes $uploadPath = Config::get('site.uploads_photo_dir'); $thumbsPath = Config::get('site.uploads_thumb_dir'); $uploadPathPublic = Config::get('site.uploads_photo_public_dir'); $thumbsPathPublic = Config::get('site.uploads_thumb_public_dir'); if (!File::exists($uploadPath)) { File::makeDirectory($uploadPath, 0777, TRUE); } if (!File::exists($thumbsPath)) { File::makeDirectory($thumbsPath, 0777, TRUE); } ## Resize thumb image $thumb_upload_success = ImageManipulation::make($file->getRealPath())->resize(200, 200, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($thumbsPath . '/' . $fileName); ## Resize full-size image $image_upload_success = ImageManipulation::make($file->getRealPath())->resize(1280, 1280, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($uploadPath . '/' . $fileName); #$file = array('filelink'=>url('uploads/'.$fileName)); $file = array('filelink' => $uploadPathPublic . '/' . $fileName); #return Response::json($file); echo stripslashes(json_encode($file)); die; } else { exit('Нет файла для загрузки!'); } }
private function uploadImage($input_file_name = 'file') { $result = array('result' => 'error'); ## Check data if (is_string($input_file_name)) { if (!Input::hasFile($input_file_name)) { $result['desc'] = 'No input file.'; return $result; } $file = Input::file($input_file_name); } elseif (is_object($input_file_name)) { $file = $input_file_name; } $rules = array('file' => 'image'); $validation = Validator::make(array('file' => $file), $rules); if ($validation->fails()) { $result['desc'] = 'This extension is not allowed.'; return $result; } ## Check upload & thumb dir $uploadPath = Config::get('site.galleries_photo_dir'); $thumbsPath = Config::get('site.galleries_thumb_dir'); if (!File::exists($uploadPath)) { File::makeDirectory($uploadPath, 0777, TRUE); } if (!File::exists($thumbsPath)) { File::makeDirectory($thumbsPath, 0777, TRUE); } ## Generate filename srand((double) microtime() * 1000000); $fileName = time() . "_" . rand(1000, 1999) . '.' . $file->getClientOriginalExtension(); ## Get images resize parameters from config $thumb_size = Config::get('site.galleries_thumb_size'); $photo_size = Config::get('site.galleries_photo_size'); ## Get image width & height $image = ImageManipulation::make($file->getRealPath()); $w = $image->width(); $h = $image->height(); if ($thumb_size > 0) { ## Normal resize $thumb_resize_w = $thumb_size; $thumb_resize_h = $thumb_size; } else { ## Resize "by the smaller side" $thumb_size = abs($thumb_size); ## Resize thumb & full-size images "by the smaller side". ## Declared size will always be a minimum. $thumb_resize_w = $w > $h ? null : $thumb_size; $thumb_resize_h = $w > $h ? $thumb_size : null; } ## Resize thumb image $thumb_upload_success = ImageManipulation::make($file->getRealPath())->resize($thumb_resize_w, $thumb_resize_h, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($thumbsPath . '/' . $fileName); if ($photo_size > 0) { ## Normal resize $image_resize_w = $photo_size; $image_resize_h = $photo_size; } else { ## Resize "by the smaller side" $photo_size = abs($photo_size); ## Resize full-size images "by the smaller side". ## Declared size will always be a minimum. $image_resize_w = $w > $h ? null : $photo_size; $image_resize_h = $w > $h ? $photo_size : null; } ## Resize full-size image $image_upload_success = ImageManipulation::make($file->getRealPath())->resize($image_resize_w, $image_resize_h, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($uploadPath . '/' . $fileName); if (!$thumb_upload_success || !$image_upload_success) { $result['desc'] = 'Error on the saving images.'; return $result; } $result['result'] = 'success'; $result['filename'] = $fileName; return $result; }
public static function upload($url, $gallery = NULL, $title = '') { $img_data = @file_get_contents($url); if (!$img_data) { return false; } $tmp_path = storage_path(md5($url)); try { file_put_contents($tmp_path, $img_data); } catch (Exception $e) { echo 'Error #' . $e->getCode() . ':' . $e->getMessage() . "<br/>\n"; echo 'In file: ' . $e->getFile() . ' (' . $e->getLine() . ')'; die; } $file = new \Symfony\Component\HttpFoundation\File\UploadedFile($tmp_path, basename($url)); ## Check upload & thumb dir $uploadPath = Config::get('site.galleries_photo_dir'); $thumbsPath = Config::get('site.galleries_thumb_dir'); if (!File::exists($uploadPath)) { File::makeDirectory($uploadPath, 0777, TRUE); } if (!File::exists($thumbsPath)) { File::makeDirectory($thumbsPath, 0777, TRUE); } ## Generate filename $fileName = time() . "_" . rand(1000, 1999) . '.' . $file->getClientOriginalExtension(); #echo $fileName; ## Get images resize parameters from config $thumb_size = Config::get('site.galleries_thumb_size'); $photo_size = Config::get('site.galleries_photo_size'); ## Get image width & height $image = ImageManipulation::make($file->getRealPath()); $w = $image->width(); $h = $image->height(); if ($thumb_size > 0) { ## Normal resize $thumb_resize_w = $thumb_size; $thumb_resize_h = $thumb_size; } else { ## Resize "by the smaller side" $thumb_size = abs($thumb_size); ## Resize thumb & full-size images "by the smaller side". ## Declared size will always be a minimum. $thumb_resize_w = $w > $h ? null : $thumb_size; $thumb_resize_h = $w > $h ? $thumb_size : null; } ## Resize thumb image $thumb_upload_success = ImageManipulation::make($file->getRealPath())->resize($thumb_resize_w, $thumb_resize_h, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($thumbsPath . '/' . $fileName); if ($photo_size > 0) { ## Normal resize $image_resize_w = $photo_size; $image_resize_h = $photo_size; } else { ## Resize "by the smaller side" $photo_size = abs($photo_size); ## Resize full-size images "by the smaller side". ## Declared size will always be a minimum. $image_resize_w = $w > $h ? null : $photo_size; $image_resize_h = $w > $h ? $photo_size : null; } ## Resize full-size image $image_upload_success = ImageManipulation::make($file->getRealPath())->resize($image_resize_w, $image_resize_h, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($uploadPath . '/' . $fileName); ## Delete original file unlink($file->getRealPath()); ## Gallery - none, existed, new $gallery_id = 0; if (is_int($gallery) && $gallery > 0) { $gallery_id = $gallery; } elseif (is_string($gallery)) { $gal = Gallery::create(['name' => $gallery]); $gallery_id = $gal->id; } ## Create MySQL record $photo = Photo::create(array('name' => $fileName, 'gallery_id' => $gallery_id, 'title' => $title)); return $photo; }
public static function createImageInBase64String($input = 'file') { $base62string = Input::get($input); if (!empty($base62string)) { $fileName = time() . "_" . Auth::user()->id . "_" . rand(1000, 1999) . '.png'; ImageManipulation::make($base62string)->resize(110, 110)->save(Config::get('site.uploads_thumb_dir') . '/thumb_' . $fileName); ImageManipulation::make($base62string)->resize(157, 157)->save(Config::get('site.uploads_photo_dir') . '/' . $fileName); return array('main' => Config::get('site.uploads_photo_public_dir') . '/' . $fileName, 'thumb' => Config::get('site.uploads_thumb_public_dir') . '/thumb_' . $fileName); } return FALSE; }
/** * Ссылка на изображение, подвергнутое кропу или ресайзу * * URL::route('image.resize', [$photo->id, 200, 200]) * URL::route('image.resize', [$photo->id, 200, 200, 'r']) * * См. также /app/config/site.php * - galleries_cache_public_dir * - galleries_cache_allowed_sizes * * @param string $method Method of resize - crop or resize */ public function getImageResize($image_id, $w, $h, $method = 'crop') { $image = $image_id ? Photo::find($image_id) : null; #Helper::tad($method); /** * Костылек-с */ if (is_numeric($method)) { $h = $h * 10 + $method; #$method = 'crop'; } if ($method == 'r') { $method = 'resize'; } else { $method = 'crop'; } #Helper::tad($method); /** * Соблюдены ли все правила? */ if (!$image_id || !$image || !is_object($image) || !@file_exists($image->fullpath()) || !$w || !$h || $w < 0 || $h < 0 || !in_array($w . 'x' . $h, (array) Config::get('site.galleries_cache_allowed_sizes')) || !in_array($method, ['crop', 'resize'])) { App::abort(404); } /* Helper::ta($image_id . '_' . $w . 'x' . $h . $method . '.jpg'); Helper::ta($image->fullpath()); Helper::ta($image->fullcachepath($w, $h, $method)); Helper::ta($image->cachepath($w, $h, $method)); #Helper::tad($image->full()); */ if (!File::exists(Config::get('site.galleries_cache_dir'))) { File::makeDirectory(Config::get('site.galleries_cache_dir'), 0777, TRUE); } $img = ImageManipulation::make($image->fullpath()); if ($method == 'resize') { /** * Resize + Resize Canvas */ $img->resize($w, $h, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); $img->resizeCanvas($w, $h); /* $newimg = ImageManipulation::canvas($w, $h); $newimg->insert($img, 'center'); $img = $newimg; #$newimg->destroy(); #*/ } else { /** * Resize + Crop */ /** * Текущие значения ширины и высоты */ $rw = $img->width(); $rh = $img->height(); /** * Находим требуемые коэффициенты */ $c1 = $rw / $w; ## Делим реальную ширину на желаемую $c2 = $rh / $h; ## Делим реальную высоту на желаемую /** * Если c1 < c2 - то ресайзить нужно по ширине * Если c1 > c2 - то ресайзить нужно по высоте */ if ($c1 < $c2) { /** * Ресайзим по меньшей стороне, по ширине */ $nw = $w; $nh = null; } else { /** * Ресайзим по меньшей стороне, по высоте */ $nw = null; $nh = $h; } #Helper::tad($nw . 'x' . $nh); $img->resize($nw, $nh, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); /** * Новые значения ширины и высоты, после ресайза по меньшей стороне */ $nnw = $img->width(); $nnh = $img->height(); /** * Кроп по центру по заданным размерам */ $img->crop($w, $h, ceil(($nnw - $w) / 2), ceil(($nnh - $h) / 2)); } /** * Сохраняем изображение */ $img->save($image->fullcachepath($w, $h, $method), 90); /** * Отдаем изображение в браузер */ header('Debug-ImageSource: onthefly'); return $img->response(); }