aliamjid/img-cropper

CropperJs集成到PHP / Nette框架

dev-master 2019-04-21 16:36 UTC

This package is auto-updated.

Last update: 2024-09-23 09:46:12 UTC


README

gumlet/php-image-resizecropperjs集成到Nette Forms

看起来如何?

用法示例 该库可以用作Nette控件来选择/裁剪图像,例如用于个人资料图片等。github flavored markdown - 例如。

<?php  
  
  
namespace App\Model\Components\UserForm;  
  
use Gumlet\ImageResize;  
use MS\Entity\UserEntity;  
use MS\Forms\BaseForm;  
  
  
class UserForm extends BaseForm {  
  public $id;  
  
  /** Your constructor etc... */  
  
  public function defineForm() {  
  $this->addImage('profile_picture','Zvolte obrázek',$this->imageService->getRealPathsFromId($user->profile_picture)->low)  
 ->setSize(500,500)  
 ->setScaleMode(1,1)  
 ->ignoreAscpectRatioWhileValidate() ->setThumbnailRatio(0.3)  
 ->setLabel('ss');  
  $this->addSubmit('submit');  
 }  
  public function save($values) {  
  /** @var UserEntity $user */  
  $user = $this->userMapper->loadEntityById($this->id);  
  /** Other stuff */  
 //Check if picture is ok  
 if($values->profile_picture instanceof ImageResize) {  
  //Save picture  
  //$values->profile_picture is [gumlet/php-image-resize] see library for more info
  $user->profile_picture = $this->imageService->saveProfilePicture($values->profile_picture);  
 }  //Do other stuff like updateing database
  $this->userMapper->updateEntity($user);  
 }}

如何安装?

您需要将javascript文件添加到您的应用中

<link rel="stylesheet" href="cropper.min.all.js">

文件路径是img-cropper/dist/cropper.min.all.js 您还需要bootstrap和jQuery。

我建议您将其也添加到您的BaseForm中

protected function addImage($name, $label ='',$value = '') {  
return $this[$name] = (new ImageControl($value,$label));  
}

现在您可以开始使用了

$this->addImage();

不使用Nette?

它也可以仅使用普通PHP使用。查看example.php