Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TImageCropper.php

Documentation is available at TImageCropper.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Core\AdiantiApplicationConfig;
  5. use Adianti\Widget\Form\AdiantiWidgetInterface;
  6. use Adianti\Core\AdiantiCoreTranslator;
  7. use Adianti\Widget\Base\TElement;
  8. use Adianti\Widget\Base\TScript;
  9. use Adianti\Widget\Form\TEntry;
  10. use Adianti\Widget\Container\THBox;
  11. use Adianti\Widget\Util\TImage;
  12.  
  13. /**
  14.  * Image uploader with cropper
  15.  *
  16.  * @version    7.4
  17.  * @package    widget
  18.  * @subpackage form
  19.  * @author     Lucas Tomasi
  20.  * @author     Pablo Dall'Oglio
  21.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  22.  * @license    http://www.adianti.com.br/framework-license
  23.  */
  24. class TImageCropper extends TField implements AdiantiWidgetInterface
  25. {
  26.     protected $height;
  27.     protected $width;
  28.     protected $value;
  29.     private $extensions;
  30.     private $fileHandling;
  31.     private $base64;
  32.     private $webcam;
  33.     private $uploaderClass;
  34.     private $seed;
  35.     private $title;
  36.     private $buttonText;
  37.     private $cropWidth;
  38.     private $cropHeight;
  39.     private $aspectRatio;
  40.     private $buttonRotate;
  41.     private $buttonDrag;
  42.     private $buttonScale;
  43.     private $buttonReset;
  44.     private $buttonZoom;
  45.  
  46.     private $imagePlaceholder;
  47.     
  48.     // defaults aspect ratios
  49.     const CROPPER_RATIO_16_9 = 16/9;
  50.     const CROPPER_RATIO_4_3 = 4/3;
  51.     const CROPPER_RATIO_1_1 = 1/1;
  52.     const CROPPER_RATIO_2_3 = 2/3;
  53.     
  54.     public function __construct($name)
  55.     {
  56.         parent::__construct($name);
  57.         $this->id   = 'timagecropper_' mt_rand(10000000001999999999);
  58.         $this->tag->{'type'}   'hidden';
  59.         $this->tag->{'widget''timagecropper';
  60.         $this->tag->{'name'$name;
  61.  
  62.         $this->buttonText 'Ajustar';
  63.         $this->title 'Ajustar imagem';
  64.  
  65.         $this->uploaderClass 'AdiantiUploaderService';
  66.         $ini AdiantiApplicationConfig::get();
  67.         $this->seed APPLICATION_NAME !empty($ini['general']['seed']$ini['general']['seed''s8dkld83kf73kf094' );
  68.         
  69.         $this->extensions ['gif''png''jpg''jpeg'];
  70.         
  71.         $this->cropWidth null;
  72.         $this->cropHeight null;
  73.         $this->buttonDrag true;
  74.         $this->buttonZoom true;
  75.         $this->aspectRatio null;
  76.         $this->buttonScale true;
  77.         $this->buttonReset true;
  78.         $this->buttonRotate true;
  79.         $this->fileHandling false;
  80.         $this->base64 false;
  81.         $this->webcam false;
  82.         $this->setSize('100%'100);
  83.  
  84.         $this->imagePlaceholder new TImage('fa:image placeholder');
  85.     }
  86.  
  87.     /**
  88.      * Set image placeholder
  89.      *
  90.      * @param TImage $image image placeholder
  91.      */
  92.     public function setImagePlaceholder(TImage $image)
  93.     {
  94.         $image->{'class'.= ' placeholder';
  95.  
  96.         $this->imagePlaceholder $image;
  97.     }
  98.  
  99.     /**
  100.      * Set window title
  101.      *
  102.      * @param String $title Window title
  103.      */
  104.     public function setWindowTitle($title)
  105.     {
  106.         $this->title $title;
  107.     }
  108.  
  109.     /**
  110.      * Set text button crop
  111.      *
  112.      * @param String $text Text button confirm
  113.      */
  114.     public function setButtonLabel($text)
  115.     {
  116.         $this->buttonText $text;
  117.     }
  118.  
  119.     /**
  120.      * Define initial aspect ratio
  121.      *
  122.      * @param double $aspectRatio Aspect ratio crop image
  123.      * @return void 
  124.      */
  125.     public function setAspectRatio($aspectRatio)
  126.     {
  127.         $this->aspectRatio $aspectRatio;
  128.     }
  129.  
  130.     /**
  131.      * Define usage base64
  132.      */
  133.     public function enableBase64()
  134.     {
  135.         $this->base64 true;
  136.     }
  137.     
  138.     public function enableWebCam()
  139.     {
  140.         $this->webcam true;
  141.     }
  142.  
  143.     /**
  144.      * Define to file handling
  145.      */
  146.     public function enableFileHandling()
  147.     {
  148.         $this->fileHandling true;
  149.     }
  150.  
  151.     /**
  152.      * Disable buttons drag move | resize
  153.      */
  154.     public function disableButtonsDrag()
  155.     {
  156.         $this->buttonDrag false;
  157.     }
  158.  
  159.     /**
  160.      * Disable buttons zoom
  161.      */
  162.     public function disableButtonsZoom()
  163.     {
  164.         $this->buttonZoom false;
  165.     }
  166.  
  167.     /**
  168.      * Disable buttons scale
  169.      */
  170.     public function disableButtonsScale()
  171.     {
  172.         $this->buttonScale false;
  173.     }
  174.  
  175.     /**
  176.      * Disable button reset
  177.      */
  178.     public function disableButtonReset()
  179.     {
  180.         $this->buttonReset false;
  181.     }
  182.  
  183.     /**
  184.      * Disable buttons rotates
  185.      */
  186.     public function disableButtonsRotate()
  187.     {
  188.         $this->buttonRotate false;
  189.     }
  190.  
  191.     /**
  192.      * Define image initial
  193.      *
  194.      * @param String $data Image url or image base64
  195.      */
  196.     public function setValue($value)
  197.     {
  198.         if ($this->fileHandling)
  199.         {
  200.             if (strpos($value'%7B'=== false)
  201.             {
  202.                 if (!empty($value))
  203.                 {
  204.                     $this->value urlencode(json_encode(['fileName'=>$value]));
  205.                 }
  206.             }
  207.             else
  208.             {
  209.                 $value_object json_decode(urldecode($value));
  210.                 
  211.                 if (!empty($value_object->{'delFile'}AND $value_object->{'delFile'== $value_object->{'fileName'})
  212.                 {
  213.                     $value '';
  214.                 }
  215.                 
  216.                 parent::setValue($value);
  217.             }
  218.         }
  219.         else
  220.         {
  221.             parent::setValue($value);
  222.         }
  223.     }
  224.  
  225.     /**
  226.      * Define the allowed extensions
  227.      */
  228.     public function setAllowedExtensions($extensions)
  229.     {
  230.         $this->extensions $extensions;
  231.     }
  232.  
  233.     /**
  234.      * Get the allowed extensions
  235.      */
  236.     public function getAllowedExtensions()
  237.     {
  238.         return $this->extensions;
  239.     }
  240.  
  241.     /**
  242.      * Define the service class for response
  243.      */
  244.     public function setService($service)
  245.     {
  246.         $this->uploaderClass $service;
  247.     }
  248.  
  249.     /**
  250.      * Define the Field's width
  251.      * @param double $width Field's width in pixels
  252.      * @param double $height Field's heigth in pixels
  253.      */
  254.     public function setSize($width$height NULL)
  255.     {
  256.         $width (strstr($width'%'!== FALSE$width "{$width}px";
  257.         $height (strstr($height'%'!== FALSE$height "{$height}px";
  258.  
  259.         $this->width $width;
  260.         $this->height $height;
  261.     }
  262.  
  263.     /**
  264.      * Set image size after crop
  265.      *
  266.      * @param px $width 
  267.      * @param px $height 
  268.      * @return void 
  269.      */
  270.     public function setCropSize($width$height)
  271.     {
  272.         $this->cropWidth $width;
  273.         $this->cropHeight $height;
  274.  
  275.         $this->setAspectRatio($this->cropWidth $this->cropHeight);
  276.     }
  277.  
  278.     /**
  279.      * Return component specific options
  280.      */
  281.     public function getOptions()
  282.     {
  283.         return json_encode([
  284.             'cropWidth' => $this->cropWidth,
  285.             'cropHeight' => $this->cropHeight,
  286.             'aspectRatio' => $this->aspectRatio,
  287.             'enableButtonDrag' => $this->buttonDrag,
  288.             'enableButtonScale' => $this->buttonScale,
  289.             'enableButtonReset' => $this->buttonReset,
  290.             'enableButtonZoom' => $this->buttonZoom,
  291.             'enableButtonRotate' => $this->buttonRotate,
  292.             'labels' =>  [
  293.                 'reset'       => AdiantiCoreTranslator::translate('Reset'),
  294.                 'scalex'      => AdiantiCoreTranslator::translate('Scale horizontal'),
  295.                 'scaley'      => AdiantiCoreTranslator::translate('Scale vertical'),
  296.                 'move'        => AdiantiCoreTranslator::translate('Move'),
  297.                 'crop'        => AdiantiCoreTranslator::translate('Crop'),
  298.                 'zoomin'      => AdiantiCoreTranslator::translate('Zoom in'),
  299.                 'zoomout'     => AdiantiCoreTranslator::translate('Zoom out'),
  300.                 'rotateright' => AdiantiCoreTranslator::translate('Rotate right'),
  301.                 'rotateleft'  => AdiantiCoreTranslator::translate('Rotate left'),
  302.             ]
  303.         ]);
  304.     }
  305.     
  306.     /**
  307.      * Show
  308.      */
  309.     public function show()
  310.     {
  311.         $label new TElement("label");
  312.         $label->{'id''timagecropper_container_' $this->name;
  313.         $label->{'class''label_timagecropper';
  314.         $label->{'style'"width: {$this->width}; height: {$this->height};";
  315.  
  316.         $remover new TElement('i');
  317.         $remover->{'class''fa fa-trash-alt';
  318.  
  319.         $editar new TElement('i');
  320.         $editar->{'class''fa fa-pen';
  321.         
  322.         $actions new THBox('div');
  323.         $actions->{'class''timagecropper_actions';
  324.  
  325.         if($this->value{
  326.             $actions->{'style''display: none';
  327.         }            
  328.         
  329.         $actions->add($editar)->{'action''edit';
  330.         $actions->add($remover)->{'action''remove';
  331.         
  332.         $img new TElement('img');
  333.         $img->{'id'}    'timagecropper_' $this->name;
  334.         $img->{'class''img_imagecropper rounded timagecropper';
  335.         $img->{'style'"max-width: {$this->width}; max-height: {$this->height};margin: auto;";
  336.  
  337.         $src '';
  338.         $fileName '';
  339.         $fileExtension '';
  340.  
  341.         if ($this->fileHandling && $this->value)
  342.         {
  343.             $dados_file json_decode(urldecode($this->value));
  344.             
  345.             if (!empty($dados_file->fileName))
  346.             {
  347.                 // Get name and extension img
  348.                 $fileName basename($dados_file->fileName);
  349.                 $fileExtension pathinfo($dados_file->fileName)['extension'];
  350.                 
  351.                 // Set src img
  352.                 $src $dados_file->fileName '?v=' uniqid();
  353.             }
  354.         }
  355.         else if ($this->base64 && $this->value)
  356.         {
  357.             $encodedImgString explode(','$this->value2)[1];
  358.             $decodedImgString base64_decode($encodedImgString);
  359.             $info getimagesizefromstring($decodedImgString);
  360.             $ext explode('/'$info['mime'])[1];
  361.             
  362.             // Get name and extension img
  363.             $fileName uniqid().".{$ext}";
  364.             $fileExtension $ext;
  365.             
  366.             // Set src img
  367.             $src $this->value;
  368.         }
  369.         else if ($this->value)
  370.         {
  371.             // Get name and extension img
  372.             $fileName empty($this->value'' basename($this->value);
  373.             $fileExtension empty($this->value'' pathinfo($this->value)['extension'];
  374.             
  375.             // Set src img
  376.             $src $this->value;
  377.         }
  378.         
  379.         if ($src)
  380.         {
  381.             $img->{'src'$src;
  382.         }            
  383.  
  384.         $this->tag->{'value'$this->value;
  385.  
  386.         $file new TEntry('tfile_timagecropper_' $this->name);
  387.         $file->{'accept'=  '.' implode(',.'$this->extensions);
  388.         $file->{'type'}   'file';
  389.         $file->{'class' "sr-only";
  390.         $file->{'id' }    $file->getName();
  391.         
  392.         $hash md5("{$this->seed}{$this->name}".base64_encode(serialize($this->extensions)));
  393.         $action "engine.php?class={$this->uploaderClass}&name={$this->name}&hash={$hash}&extensions=".base64_encode(serialize($this->extensions));
  394.  
  395.         if(parent::getEditable())
  396.         {
  397.             $label->add($file);
  398.         }
  399.         
  400.         $label->add($img);
  401.         $label->add($actions);
  402.         $label->add($this->tag);
  403.         $label->add($this->imagePlaceholder);
  404.  
  405.         $label->show();
  406.  
  407.         $options $this->getOptions();
  408.  
  409.         $fileHandling $this->fileHandling '1' '0';
  410.         $base64 $this->base64 '1' '0';
  411.         $webcam $this->webcam '1' '0';
  412.  
  413.         TScript::create("timagecropper_start('{$this->name}', '{$this->title}', '{$this->buttonText}', '{$action}', {$fileHandling}, {$base64}, {$webcam}, {$options}, '{$fileName}', '{$fileExtension}');");
  414.     }
  415. }