Skip to content

Instantly share code, notes, and snippets.

@321zeno
Last active August 29, 2015 14:21
Show Gist options
  • Save 321zeno/1896a6a30d1d176a5806 to your computer and use it in GitHub Desktop.
Save 321zeno/1896a6a30d1d176a5806 to your computer and use it in GitHub Desktop.
Using Redactor WYSIWYG with Laravel 4.x
<?php
class AdminRedactorController extends AdminController {
private $publicFolder = 'redactor_uploads';
private $uploadFolder = 'public/' . $publicFolder;
public function fileUpload(){
$file = Input::file('file');
$name = Input::file('file')->getClientOriginalName();
if (Input::file('file')->move($this->uploadFolder, $name))
{
return Response::json(array('filelink' => '/uploads/' . $name, 'filename' => $name));
}
return FALSE;
}
public function imageUpload() {
$rules = array(
'file' => 'image|max:10000'
);
$validation = Validator::make(Input::all(), $rules);
$file = Input::file('file');
$name = Input::file('file')->getClientOriginalName();
if ($validation->fails())
{
return FALSE;
}
else
{
if (Input::file('file')->move($this->uploadFolder, $name))
{
$thumbsFolder = $this->uploadFolder . '/thumbs/';
$uploadedfile = $this->uploadFolder . '/' . $name;
$extension = File::extension($name);
$extension = ($extension == 'jpg') ? 'jpeg' : $extension;
$imagecreatefunction = 'imagecreatefrom' . $extension;
$src = call_user_func($imagecreatefunction, $uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth=100;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height);
$uploadedFile = $thumbsFolder . $name;
imagejpeg($tmp,$uploadedFile,100);
return Response::json(array('filelink' => '/'. $this->publicFolder .'/' . $name));
}
return FALSE;
}
}
public function finder(){
$thumbsFolder = $this->publicFolder . '/thumbs/';
$uploadsFolder = $this->publicFolder . '/';
$serverThumbs = public_path() . '/' . $thumbsFolder;
$files = glob($serverThumbs .'*', GLOB_BRACE);
$image_files = preg_grep("/(jpg|gif|png|bmp)/i", $files);
foreach($image_files as $file) {
$item['image'] = '/' . $uploadsFolder . basename($file);
$item['thumb'] = '/' . $thumbsFolder . basename($file);
$item['title'] = basename($file);
$uploadedfiles[] = $item;
}
return $uploadedfiles;
}
}
//in your view
<script src="/assets/admin/plugins/redactor/redactor.min.js"></script>
<script src="/assets/admin/plugins/redactor/imagemanager.js"></script>
<script>
//redactor init
$(document).ready(function(){
$('.wysiwyg').redactor({
focus: true,
imageUpload: '{{route('admin.redactor.image')}}',
fileUpload: "{{route('admin.redactor.file')}}",
iframe: true,
imageManagerJson: '{{route('admin.redactor.finder')}}',
plugins: ['imagemanager']
// css: '/site/stylesheets/typography.css'
});
});
</script>
<?php
// ...
Route::post('redactor/imageUpload', array('as' => 'admin.redactor.image', 'uses' => 'AdminRedactorController@imageUpload'));
Route::get('redactor/finder', array('as' => 'admin.redactor.finder', 'uses' => 'AdminRedactorController@finder'));
Route::post('redactor/fileUpload', array('as' => 'admin.redactor.file', 'uses' => 'AdminRedactorController@fileUpload'));
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment