Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkounay committed Oct 7, 2021
0 parents commit 9d2a695
Show file tree
Hide file tree
Showing 3,174 changed files with 228,448 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
14 changes: 14 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor/
.phpunit.result.cache
.php_cs.cache
composer.lock
Resources/assets/node_modules
11 changes: 11 additions & 0 deletions ArkounayUxMediaBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php


namespace Arkounay\Bundle\UxMediaBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ArkounayUxMediaBundle extends Bundle
{

}
111 changes: 111 additions & 0 deletions Controller/CropController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Arkounay\Bundle\UxMediaBundle\Controller;

use Artgris\Bundle\FileManagerBundle\Service\FilemanagerService;
use Artgris\Bundle\FileManagerBundle\Service\FileTypeService;
use Gregwar\Image\Image;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class CropController extends AbstractController
{

/**
* @var FilemanagerService
*/
private $filemanagerService;

public function __construct(FilemanagerService $filemanagerService)
{
$this->filemanagerService = $filemanagerService;
}

/**
* @Route("/crop/", name="ux_media_ajax_crop")
*/
public function ajaxCrop(Request $request): JsonResponse
{
$post = $request->request;
$src = $post->get('src');
$src = strtok($src, '?');
$x = $post->getInt('x');
$y = $post->getInt('y');
$width = $post->getInt('width');
$height = $post->getInt('height');
$scaleX = $post->getInt('scaleX', 1);
$scaleY = $post->getInt('scaleY', 1);
$rotate = $post->getInt('rotate');
$conf = $post->get('conf');
$extra = $post->get('extra');

$fileManager = $this->getParameter('artgris_file_manager');

$destinationFolder = null;
if ($conf !== null) {
$artgrisConf = $this->filemanagerService->getBasePath(['conf' => $conf, 'extra' => $extra ?? []]);
$destinationFolder = $artgrisConf['dir'];
}

$flipX = $scaleX !== 1;
$flipY = $scaleY !== 1;

if ($flipX) {
$rotate = -$rotate;
}
if ($flipY) {
$rotate = -$rotate;
}

$pathinfo = pathinfo(parse_url($src, PHP_URL_PATH));
$extension = $pathinfo['extension'];

if ($src[0] === '/') {
$src = urldecode($this->getParameter('kernel.project_dir').'/'.$fileManager['web_dir'].$src);
}

if (!file_exists($src)) {
return new JsonResponse('');
}

$image = Image::open($src)
->rotate(-$rotate)
->flip($flipY, $flipX)
->crop($x, $y, $width, $height);

$savedPath = '/';

if ($destinationFolder !== null) {

if (substr($destinationFolder, -1) !== DIRECTORY_SEPARATOR) {
$destinationFolder .= DIRECTORY_SEPARATOR;
}
$rootdir = $this->getParameter('kernel.project_dir') . '/src';

$baseUrl = $rootdir . ' ../' . $fileManager['web_dir'];
$cropStrAdd = '_crop_';
$filename = $pathinfo['filename'];
$cropPos = mb_strpos($filename, $cropStrAdd);
if ($cropPos !== false) {
$filename = mb_substr($filename, 0, $cropPos);
}
$croppedPath = $this->getParameter('ux_media')['cropped_path'];
$savedPath = $image->save($rootdir . DIRECTORY_SEPARATOR . $destinationFolder . $croppedPath . urldecode($filename) . $cropStrAdd . uniqid() . '.' . $extension, 'guess', 85);

$savedPath = mb_substr($savedPath, mb_strlen($baseUrl));
if ($savedPath[0] !== '/') {
$savedPath = $src;
}
} else {
if ($extension === 'png') {
$savedPath .= $image->png();
} else {
$savedPath .= $image->jpeg();
}
}

return new JsonResponse($savedPath);
}
}
42 changes: 42 additions & 0 deletions DependencyInjection/ArkounayUxMediaExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Arkounay\Bundle\UxMediaBundle\DependencyInjection;

use Arkounay\Bundle\UxMediaBundle\Form\UxMediaType;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;

class ArkounayUxMediaExtension extends Extension implements PrependExtensionInterface
{

public function prepend(ContainerBuilder $container)
{
$bundles = $container->getParameter('kernel.bundles');

if (!isset($bundles['TwigBundle'])) {
return;
}

$container->prependExtensionConfig('twig', ['form_themes' => ['@ArkounayUxMedia/ux_media_form_theme.html.twig']]);
}

public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('ux_media', $config);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');

$container
->setDefinition('form.ux-media', new Definition(UxMediaType::class))
->addTag('form.type')
->setPublic(false)
;
}
}
22 changes: 22 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Arkounay\Bundle\UxMediaBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('ux_media');
$rootNode = $treeBuilder->getRootNode();
$rootNode
->children()
->scalarNode('cropped_path')->defaultValue('cropped/')->end()
->end();

return $treeBuilder;
}

}
96 changes: 96 additions & 0 deletions Form/UxMediaCollectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Arkounay\Bundle\UxMediaBundle\Form;

use Arkounay\Bundle\UxCollectionBundle\Form\UxCollectionType;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UxMediaCollectionType extends UxCollectionType implements DataTransformerInterface
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->addModelTransformer($this);
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);

$entryOptionsNormalizer = function (Options $options, $value) {
$value['conf'] = $options['conf'];
$value['block_name'] = 'entry';

return $value;
};

$resolver->setDefaults([
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_data' => null,
'prototype_name' => '__name__',
'entry_type' => UxMediaType::class,
'entry_options' => [],
'delete_empty' => false,
'by_reference' => false,
'required' => false,
'min' => 0,
'max' => null,
'tree' => 0,
'error_bubbling' => false,
'extra' => [],
]);

$resolver->setRequired('conf');

$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
}

public function buildView(FormView $view, FormInterface $form, array $options)
{
parent::buildView($view, $form, $options);

$view->vars = array_replace($view->vars, [
'conf' => $options['conf'],
'tree' => $options['tree'],
'extra' => $options['extra'],
'collection' => 'true',
]);
}

public function getBlockPrefix(): string
{
return 'ux_media_collection';
}

/**
* {@inheritdoc}
*/
public function transform($value)
{
return $value;
}

/**
* {@inheritdoc}
*/
public function reverseTransform($value)
{
if (\count($value) === 0) {
return null;
}

return array_values(array_filter(($value instanceof Collection) ? $value->toArray() : $value));
}
}
59 changes: 59 additions & 0 deletions Form/UxMediaType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Arkounay\Bundle\UxMediaBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UxMediaType extends AbstractType
{
public function buildView(FormView $view, FormInterface $form, array $options)
{
parent::buildView($view, $form, $options);

$view->vars = array_replace($view->vars, [
'conf' => $options['conf'],
'tree' => $options['tree'],
'extra' => $options['extra'],
'readonly' => $options['readonly'],
'allow_crop' => $options['allow_crop'],
'crop_options' => $options['crop_options'],
'display_file_manager' => $options['display_file_manager'],
]);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'required' => false,
'by_reference' => false,
'allow_crop' => true,
'crop_options' => [
'display_crop_data' => true,
'allow_flip' => true,
'allow_rotation' => true,
'ratio' => false,
],
'readonly' => false,
'tree' => 0,
'error_bubbling' => false,
'display_file_manager' => true,
'extra' => [],
]);

$resolver->setRequired('conf');
}

public function getBlockPrefix()
{
return 'ux_media';
}

public function getParent()
{
return TextType::class;
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Arkounay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 9d2a695

Please sign in to comment.