mirror of
https://github.com/Oreolek/oreolek.ru.git
synced 2024-05-21 02:18:20 +03:00
Image uploading
This commit is contained in:
parent
0e86497da7
commit
acfe086018
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -22,3 +22,6 @@
|
|||
[submodule "modules/debug-toolbar"]
|
||||
path = modules/debug-toolbar
|
||||
url = git://github.com/biakaveron/debug-toolbar.git
|
||||
[submodule "modules/image"]
|
||||
path = modules/image
|
||||
url = git://github.com/kohana/image.git
|
||||
|
|
|
@ -112,10 +112,10 @@ Kohana::modules(array(
|
|||
'markdown' => MODPATH.'markdown', // Markdown module
|
||||
'less' => MODPATH.'less', // LEaner CSS
|
||||
'debug-toolbar' => MODPATH.'debug-toolbar', // Debug toolbar
|
||||
// 'cache' => MODPATH.'cache', // Caching with multiple backends
|
||||
// 'codebench' => MODPATH.'codebench', // Benchmarking tool
|
||||
// 'image' => MODPATH.'image', // Image manipulation
|
||||
// 'unittest' => MODPATH.'unittest', // Unit testing
|
||||
'image' => MODPATH.'image', // Image manipulation
|
||||
// 'cache' => MODPATH.'cache', // Caching with multiple backends
|
||||
// 'codebench' => MODPATH.'codebench', // Benchmarking tool
|
||||
// 'unittest' => MODPATH.'unittest', // Unit testing
|
||||
));
|
||||
|
||||
/**
|
||||
|
|
83
application/classes/Controller/Photo.php
Normal file
83
application/classes/Controller/Photo.php
Normal file
|
@ -0,0 +1,83 @@
|
|||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Photo management
|
||||
**/
|
||||
class Controller_Photo extends Controller_Layout {
|
||||
public $template = 'photo/view';
|
||||
protected $secure_actions = array(
|
||||
'upload' => array('login','admin'),
|
||||
'edit' => array('login','admin'),
|
||||
);
|
||||
/**
|
||||
* View photo by ID
|
||||
**/
|
||||
public function action_view()
|
||||
{
|
||||
$id = $this->request->param('id');
|
||||
$photo = ORM::factory('Photo', $id);
|
||||
if (!$photo->loaded()) $this->redirect('error/404');
|
||||
$title = $photo->name;
|
||||
$this->template->header = Request::factory('header/standard')->post('title',$title)->execute();
|
||||
$this->template->image_path = $photo->get_image_path();
|
||||
$this->template->thumbnail_path = $photo->get_thumbnail_path();
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a new photo
|
||||
**/
|
||||
public function action_upload()
|
||||
{
|
||||
$this->template = new View('photo/edit');
|
||||
$photo = ORM::factory('Photo');
|
||||
$title = 'Загрузка фотографии';
|
||||
$this->template->header = Request::factory('header/standard')->post('title',$title)->execute();
|
||||
$this->template->footer = Request::factory('footer/standard')->execute();
|
||||
$this->template->errors = array();
|
||||
if (HTTP_Request::POST == $this->request->method()) {
|
||||
$validation_post = Validation::factory($this->request->post())
|
||||
->rules('name', array(
|
||||
array('not_empty'),
|
||||
));
|
||||
$validation_files = Validation::factory( $_FILES )
|
||||
->rule( 'file', array( 'Upload', 'not_empty' ) )
|
||||
->rule( 'file', array( 'Upload', 'valid' ) )
|
||||
->rule( 'file', 'Upload::type', array(':value', array('jpg', 'png', 'gif')));
|
||||
$file = Arr::get($_FILES, 'file');
|
||||
if ($validation_post->check() AND $validation_files->check() AND isset($file))
|
||||
{
|
||||
$photo->filename = Arr::get($file, 'name');
|
||||
$filename = $photo->file_save($file);
|
||||
if ( $filename === false ) {
|
||||
throw new Exception( 'Unable to save uploaded file.' );
|
||||
}
|
||||
$photo->name = $this->request->post('name');
|
||||
try {
|
||||
if ($photo->check()) $photo->create();
|
||||
}
|
||||
catch (ORM_Validation_Exception $e)
|
||||
{
|
||||
$this->template->errors = $e->errors();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->template->errors = Arr::merge( $validation_post->errors(), $validation_files->errors() );
|
||||
}
|
||||
if (empty($this->template->errors)) $this->redirect('photo/view/' . $photo->id);
|
||||
}
|
||||
$this->template->photo = $photo;
|
||||
}
|
||||
|
||||
public function action_create()
|
||||
{
|
||||
$this->redirect('photo/upload');
|
||||
}
|
||||
|
||||
/**
|
||||
* Index all photos in the database.
|
||||
**/
|
||||
public function action_index()
|
||||
{
|
||||
}
|
||||
}
|
77
application/classes/Model/Photo.php
Normal file
77
application/classes/Model/Photo.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
class Model_Photo extends ORM {
|
||||
|
||||
/**
|
||||
* @return array validation rules
|
||||
**/
|
||||
public function rules()
|
||||
{
|
||||
return array(
|
||||
'name' => array(
|
||||
array('not_empty'),
|
||||
),
|
||||
'filename' => array(
|
||||
array('not_empty')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public $_labels = array(
|
||||
'name' => 'Название',
|
||||
);
|
||||
|
||||
protected function get_image_dir_path()
|
||||
{
|
||||
return Kohana::$config->load('common.uploads_dir').'/photos';
|
||||
}
|
||||
public function get_image_path()
|
||||
{
|
||||
if ($this->filename) return $this->get_image_dir_path().'/'.$this->filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function that adds suffix _thumb to file name: /home/dhawu.jpeg -> /home/dhawu_thumb.jpeg
|
||||
* @param integer $width
|
||||
* thumbnail width (manages the suffix)
|
||||
* @param integer $height
|
||||
* thumbnail height (manages the suffix)
|
||||
* @retval string
|
||||
*/
|
||||
public function get_thumbnail_path($width = NULL, $height = NULL)
|
||||
{
|
||||
if ($width == 0) $width = Kohana::$config->load('common.thumbnail_width');
|
||||
if ($height == 0) $height = Kohana::$config->load('common.thumbnail_height');
|
||||
$image_path = $this->get_image_path();
|
||||
if (!is_file(DOCROOT.$image_path))
|
||||
{
|
||||
throw new HTTP_Exception_404('File not found');
|
||||
return $image_path;
|
||||
}
|
||||
$parts = explode('.', $image_path);
|
||||
$count = count($parts) - 2;
|
||||
if ($count < 0) $count = 0;
|
||||
$suffix = 'thumb';
|
||||
if ($width) $suffix .= $width;
|
||||
if ($height) $suffix .= '_' . $height;
|
||||
$parts[$count] .= '_' . $suffix;
|
||||
$thumbnail_path = implode('.', $parts);
|
||||
|
||||
if (!is_file(DOCROOT.$thumbnail_path)) {
|
||||
$image = Image::factory(DOCROOT.$image_path);
|
||||
$image->resize($width, $height, Image::WIDTH);
|
||||
$image->crop($width, $height);
|
||||
$image->save(DOCROOT.$thumbnail_path);
|
||||
}
|
||||
return $thumbnail_path;
|
||||
}
|
||||
|
||||
protected function get_thumbnail_file_path($width = NULL, $height = NULL)
|
||||
{
|
||||
return DOCROOT.$this->get_thumbnail_path($width, $height);
|
||||
}
|
||||
|
||||
public function file_save($file)
|
||||
{
|
||||
return Upload::save($file, $this->filename, DOCROOT.$this->get_image_dir_path());
|
||||
}
|
||||
}
|
|
@ -3,4 +3,7 @@
|
|||
return array(
|
||||
'title' => 'Личный сайт Александра Яковлева',
|
||||
'author' => 'Александр Яковлев',
|
||||
'uploads_dir' => '/uploads',
|
||||
'thumbnail_width' => 200,
|
||||
'thumbnail_height' => 150,
|
||||
);
|
||||
|
|
|
@ -62,6 +62,15 @@ ALTER TABLE `posts_tags`
|
|||
ADD CONSTRAINT `posts_tags_fk_posts` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE,
|
||||
ADD CONSTRAINT `posts_tags_fk_tags` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE;
|
||||
|
||||
DROP TABLE IF EXISTS `photos`;
|
||||
|
||||
CREATE TABLE `photos` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` text NOT NULL,
|
||||
`filename` text NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
/* User management */
|
||||
|
||||
DROP TABLE IF EXISTS `roles_users`;
|
||||
|
|
21
application/views/photo/edit.php
Normal file
21
application/views/photo/edit.php
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php echo $header;
|
||||
|
||||
echo Form::open( null, array( 'enctype' => 'multipart/form-data' ) );
|
||||
if ($errors)
|
||||
{
|
||||
echo '<p class="message">При проверке формы были найдены ошибки:</p>';
|
||||
echo '<ul class="errors">';
|
||||
foreach ($errors as $message)
|
||||
{
|
||||
echo "<li> $message </li>";
|
||||
}
|
||||
echo '</ul>';
|
||||
}
|
||||
$image_path = $photo->get_image_path();
|
||||
if ($image_path) echo HTML::image($image_path);
|
||||
echo Form::orm_input($photo, 'name');
|
||||
echo Form::file('file');
|
||||
echo Form::submit('submit','Отправить');
|
||||
echo Form::close();
|
||||
|
||||
echo $footer;
|
5
application/views/photo/view.php
Normal file
5
application/views/photo/view.php
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?php echo $header;
|
||||
|
||||
echo HTML::image($image_path);
|
||||
|
||||
echo $footer;
|
1
modules/image
Submodule
1
modules/image
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit f94e3c1993662f378d27246b0ee1fd12ba85f9de
|
Loading…
Reference in a new issue