Я сделал загрузку изображения в CodeIgniter
. Но я столкнулся с проблемой, когда хочу обрезать свои загруженные изображения, используя jcrop
с codeigniter
. Стараюсь следующие коды, которые не работают:Обрезка изображений с использованием jcrop с CodeIgniter
Контроллер(image_upload.php)
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Image_upload extends CI_Controller
{
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function __construct()
{
parent::__construct();
$this->load->model('upload_model');
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->helper('url');
}
public function index()
{
$this->load->view('index_view');
}
public function upload()
{
if($this->input->post('submit'))
{
$config['upload_path'] = './assets/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = 'test';
$this->load->library('upload', $config);
$fiels_name = "images";
$this->upload->do_upload($fiels_name);
$image_path = $this->upload->data();
$images = $image_path['file_name'];
// var_dump($images);
// die();
$this->load->view('crop_view');
$this->crop($image_path);
$this->upload_model->upload($images);
// redirect('image_upload');
}
else
{
redirect('image_upload');
}
}
public function crop($image_path)
{
//crop it
$data['x'] = $this->input->post('x');
$data['y'] = $this->input->post('y');
$data['w'] = $this->input->post('w');
$data['h'] = $this->input->post('h');
$images = $image_path['file_name'];
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
// $config['library_path'] = '/usr/X11R6/bin/';
$config['source_image'] = './assets/images/' . $images;
$config['new_image'] = './assets/images/' . $images.'_'.time();
die($data['x']);
$config['width'] = $data['w'];
$config['height'] = $data['h'];
$config['x_axis'] = $data['x'];
$config['y_axis'] = $data['y'];
$config['maintain_ratio'] = FALSE;
$config['dynamic_output'] = TRUE;
$this->image_lib->initialize($config);
if(!$this->image_lib->crop()) {
echo $this->image_lib->display_errors();
} else {
echo "Success";
}
}
}
/* End of file image_upload.php */
/* Location: ./application/controllers/image_upload.php */
Просмотр(index_view.php)
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-md" data-toggle="modal" data-target="#myModal">
Image Upload
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Crop the image</h4>
</div>
<div class="modal-body">
<?php echo form_open_multipart('image_upload/upload'); ?>
<div class="form-group">
<label for="images">Upload a profile picture for you</label>
<input type="file" id="images" name='images' onchange="loadFile(event)">
<p class="help-block">Image must be less than 1MB</p>
<img id="image_preview"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success" name="submit" value="submit">Upload</button>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/bootstrap.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/jquery.Jcrop.js"></script>
<script>
var loadFile = function(event) {
var reader = new FileReader();
reader.onload = function(){
var output = document.getElementById('image_preview');
output.src = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
};
</script>
<script>
$(document).ready(function(){
$("#images").change(function(){
$("#image_preview").css({"height": "300px","width": "300px;"});
});
});
</script>
</body>
Просмотр(crop_view.php)
<script>
$('#jcrop_target').Jcrop({
onChange: showPreview,
onSelect: showCoords,
aspectRatio: 1,
addClass: 'custom',
maxSize: [90,60]
});
function showPreview(coords)
{
var rx = 100/coords.w;
var ry = 100/coords.h;
$('#preview').css({
width: Math.round(rx * 500) + 'px',
height: Math.round(ry * 370) + 'px',
marginLeft: '-' + Math.round(rx * coords.x) + 'px',
marginTop: '-' + Math.round(ry * coords.y) + 'px'
});
};
function showCoords(c)
{
$('#x').val(c.x);
$('#y').val(c.y);
$('#x2').val(c.x2);
$('#y2').val(c.y2);
$('#w').val(c.w);
$('#h').val(c.h);
};
</script>
<!-- Bootstrap core CSS -->
<link href="<?php echo base_url(); ?>assets/css/bootstrap.min.css" rel="stylesheet">
<link href="<?php echo base_url(); ?>assets/css/main.css" rel="stylesheet">
<link href="<?php echo base_url(); ?>assets/css/jquery.Jcrop.css" rel="stylesheet">
</head>
<body style="background-color: #efe;">
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" id="upload_images">
<h1>Crop
<small>Images</small>
</h1>
<?php echo form_open_multipart('image_upload/crop'); ?>
<div id="uploaded_image">
<img src="<?php echo base_url();?>assets/images/<?php echo $image_path['file_name'];?>" id="jcrop_target"/>
</div>
<div id="uploaded_preview">
<img src="<?php echo base_url();?>assets/images/<?php echo $image_path['file_name'];?>" id="preview"/>
</div>
<input type="hidden" name="x" id="x" />
<input type="hidden" name="y" id="y" />
<input type="hidden" name="x2" id="x2" />
<input type="hidden" name="y2" id="y2" />
<input type="text" name="w" id="w" />
<input type="text" name="h" id="h" />
<input type="hidden" name="image_name" value="<?php echo $image_path['file_name'];?>"/>
<input type="submit" name="crop_image" value="Crop" />
<?php echo form_close(); ?>
</div>
</body>
Просто подумал, спросили бы вы, если вы попытались использовать Codeigniter Image Manipulation Class вместо http://www.codeigniter.com/user_guide/libraries/image_lib.html – user4419336