【发布时间】:2016-03-02 13:53:40
【问题描述】:
我对这个插件非常失望。我是 JS/Jquery 的菜鸟,但我的网站非常需要这个插件...... 所以我在这里找到了cropit:http://scottcheng.github.io/cropit/
我不知道如何在控制器中取回裁剪的图像并保存... 所以我的表格是:
<div class="form-group">
{{ form_label(form.image, 'Image', {'label_attr': {'class': 'col-sm-3 control-label'}})}}
<div class="image-cropper">
<!-- This is where the preview image is displayed -->
<div class="row">
<div class="col-sm-offset-2 col-sm-8">
<div class="cropit-image-preview-container">
<div class="cropit-image-preview"></div>
</div>
</div>
</div>
<!-- This range input controls zoom -->
<!-- You can add additional elements here, e.g. the image icons -->
<div class="row">
<div class="col-sm-offset-4 col-sm-4">
<input type="range" class="cropit-image-zoom-input" />
</div>
</div>
{{ form_errors(form.image) }}
<div class="row">
<div class="col-sm-4">
{{ form_widget(form.image) }}
<div class="select-image-btn">Select new image</div>
<div class="send_image">Get Cropped image.</div>
</div>
</div>
</div>
</div>
我的 Jquery 代码:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" src="{{ asset('js/cropit-master/dist/jquery.cropit.js') }}"></script>
<script>
$(function () {
$('.select-image-btn').click(function(){
$('.cropit-image-input').click();
});
var z = $('.image-cropper');
z.cropit({
exportZoom: 0.5,
imageBackground: true,
imageBackgroundBorderWidth: 15
});
$('.send_image').click(function() {
var h =z.cropit('export');
alert(h);
});
});
</script>
我的 Image.php 实体:
public function getFile()
{
return $this->file;
}
public function setFile(UploadedFile $file = null)
{
$decoded = urldecode($file);
$exp = explode(';', $decoded);
$exp = explode(':', $exp[0]);
$data = array_pop($exp);
$this->file = imagecreatefromstring($file);
if (null !== $this->url) {
$this->tempFilename = $this->url;
$this->url = null;
$this->alt = null;
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null === $this->file) {
return;
}
$this->url = $this->file->guessExtension();
$this->alt = $this->file->getClientOriginalName();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
if (null !== $this->tempFilename) {
$oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
if (file_exists($oldFile)) {
unlink($oldFile);
}
}
$this->file->move(
$this->getUploadRootDir(), // Le répertoire de destination
$this->id.'.'.$this->url // Le nom du fichier à créer, ici « id.extension »
);
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->url;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (file_exists($this->tempFilename)) {
// On supprime le fichier
unlink($this->tempFilename);
}
}
public function getUploadDir()
{
// On retourne le chemin relatif vers l'image pour un navigateur
return 'uploads/img';
}
protected function getUploadRootDir()
{
// On retourne le chemin relatif vers l'image pour notre code PHP
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
public function getWebPath()
{
return $this->getUploadDir().'/'.$this->getId().'.'.$this->getUrl();
}
我看到了很多帖子,但没有什么对我有用,我不太明白。 所以有人可以帮我解释一下吗? 谢谢
已编辑:我的第一个方法
我的表单使用隐藏输入来保存 base64 数据:
$('form').submit(function() {
// Move cropped image data to hidden input
var imageData = $('.image-cropper').cropit('export');
$('.hidden-image-data').val(imageData);
};``
我的 Imagetype 带有一个文件输入来加载原始图像和隐藏输入来保存 base64 图像。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', 'file', array(
'attr' => array('class' => 'cropit-image-input')))
->add('file', 'hidden', array('attr' => array('class' => 'hidden-image-data')))
;
}
我的控制器还是一样的。
【问题讨论】:
-
问题是,使用任何这种 js 裁剪脚本,您都不会在 setFile 字段中获得
UploadedFile对象。我使用模型数据转换器和自定义字段类型使其与 symfony 一起使用。 -
我知道我得到了 base64 图像,不是吗?你能告诉我你是怎么做到的吗?
-
是的。你会得到带有一些不相关前缀的base64。这个想法是使用
hidden字段和DataTransformer 将其转换为文件形式。抱歉,我不确定我可以分享源代码。 -
@Flushdrew,你能检查我的答案吗?
标签: php jquery symfony plugins crop