2017 © Pedro Peláez
 

symfony-bundle mobile-image-resizer

Mobile Image Resizer

image

oylabs/mobile-image-resizer

Mobile Image Resizer

  • Wednesday, June 24, 2015
  • by Zolotar1988
  • Repository
  • 1 Watchers
  • 0 Stars
  • 228 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Olabs/MIRBundle

Example

/app/config/parameters.yml

images_gallery_sizes:
    Product:
      logo:
        main_thumbnail:
          width: 50
          height: 50
        iphone:
          standart:
            width: 840
            height: 620
          thumbnail:
            width: 320
            height: 240
        android:
          standart:
            width: 840
            height: 620
          thumbnail:
            width: 320
            height: 240
      images:
        main_thumbnail:
          width: 50
          height: 50
        iphone:
          standart:
            width: 840
            height: 620
          thumbnail:
            width: 320
            height: 240
        android:
          standart:
            width: 840
            height: 620
          thumbnail:
            width: 320
            height: 240

AppKernel.php

new Olabs\MIRBundle\OlabsMIRBundle(),

/app/config/config.yml

image_resize:
    image_loader:
        imagine_class: 'Imagine\Gd\Imagine'
        type: 'file'

Product.php

id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return Product
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set logo
     *
     * @param ProductImage $logo
     * @return Product
     */
    public function setLogo($logo)
    {
        if ($logo) { $logo->setProduct($this); }
        $this->logo = $logo;

        return $this;
    }

    /**
     * Get logo
     *
     * @return string
     */
    public function getLogo()
    {
        return $this->logo;
    }

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add images
     *
     * @param ProductImage $image
     * @return Product
     */
    public function addImage(ProductImage $image)
    {
        $image->setProduct($this);

        $this->images[] = $image;

        return $this;
    }

    /**
     * @param ProductImage $image
     */
    public function removeImage(ProductImage $image)
    {
        $this->images->removeElement($image);
    }

    /**
     * Get images
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getImages()
    {
        return $this->images;
    }
    /**
     * Set image
     *
     * @param \Olabs\MirBundle\Entity\Image $image
     * @return ProductImage
     */
    public function setImages(\Doctrine\Common\Collections\Collection $images)
        {
        $this->images = $images;
        return $this;
        }
    }
ProductImage.php    
=========     
    children[] = $children;

        return $this;
    }

    /**
     * Remove children
     *
     * @param \you_project\Entity\ProductImage $children
     */
    public function removeChild(\you_project\Entity\ProductImage $children)
    {
        $this->children->removeElement($children);
    }

    /**
     * Get children
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * Set parent
     *
     * @param \you_project\Entity\ProductImage $parent
     * @return ProductImage
     */
    public function setParent(\you_project\Entity\ProductImage $parent = null)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent
     *
     * @return \you_project\Entity\ProductImage
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Set product
     *
     * @param \you_project\Entity\Product $product
     * @return ProductImage
     */
    public function setProduct(\you_project\Entity\Product $product = null)
    {
        $this->product = $product;

        return $this;
    }
    /**
     * Get product
     *
     * @return \you_project\Entity\Product
     */
    public function getProduct()
    {
        return $this->product;
    }
    }
    
you_project\Form\Type\ProductType.php    
=========   
    add('title', 'text', array('required' => true, 'label' => 'Title'));
        $builder->add('logo', new ProductImageEditType(), array('required' => true, 'label' => 'Image'));
        $builder->add('images', 'collection', array(
            'label' => 'Images',
            'type' => new ProductImageEditType(),
            'allow_delete' => true,
            'allow_add' => true,
            'by_reference' => false,
        ));



    }

    public function getName()
    {
        return 'product_form';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'you_project\Entity\Product',
                'csrf_protection' => false,
            )
        );
    }
    }
    
you_project/Resources/config/services.xml    
=========      
    

<container xmlns="http://symfony.com/schema/dic/services"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://symfony.com/schema/dic/services
       http://symfony.com/schema/dic/services/services-1.0.xsd">


<services>
    <service id="project.main.product_manager" class="you_project\Service\ProductManager">
        <argument type="service" id ="doctrine.orm.entity_manager"/>
        <argument type="service" id ="olabs.main.image_handler"/>
        <argument type="service" id ="olabs.main.image_gallery_manager"/>
    </service>
</services>
</container>

you_project/Service/ProductManager.php

<?php

namespace you_project\Service;


use you_project\Entity\Product;
use Doctrine\ORM\EntityManager;
use you_project\Entity\ProductImage;

class ProductManager
{
/**
 * @var \Doctrine\ORM\EntityManager
 */
protected $em;

/**
 * @var \you_project\Entity\ProductRepository
 */
protected $productRepo;

/**
 * @param Product $product
 */
public function delete($product)
{
    $product->setIsDeleted(true);
}
public function recover($product)
{
    $product->setIsDeleted(false);
}

public function __construct(EntityManager $em, ImageHandler $imageHandler, ImageGalleryManager $imageGalleryManager)
{
    $this->em = $em;
    $this->imageHandler = $imageHandler;
    $this->imageGalleryManager = $imageGalleryManager;
}

public function saveProduct(Product $product)
{
    $this->imageGalleryManager->processGallerySaveImage($product, $product->getLogo(), 'logo');
    $product->setLogo($product->getLogo());
    $this->imageGalleryManager->processEntityGallerySave($product);

}

}

you_project/Controller/ProductController.php

public function editAction()
{
.............

    if ($form->isValid()) {
        /** @var ProductManager $productManager */
        $productManager = $this->get('project.main.product_manager');
        $productManager->saveProduct($product);
.............
}

you_project/Resources/views/Product/edit.html.twig

.............
{% block javascripts %}
{{ parent() }}

{% javascripts output='js/compiled/subformImages.js'
'@YouProjectBundle/Resources/public/js/subform/images.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
.............
{% if product.logo %}
    <img src="{{ product.logo.getThumbnail.image.getWebPath }}">
{% endif %}
{{ form_widget(form.logo.image.file, {'attr': {
'class': 'file' }}) }}
{{ form_errors(form.logo) }}
.............

{{ include('YouProjectBundle::imagesForm.html.twig', {'images':form.images, 'entityName':'product'}) }}
.............

you_project/Resources/views/imagesForm.html.twig

{% set subformProto = '
' %}

<div class="imageSubforms fluid clearfix">
<span class="input-label">{{ images.vars.label }}</span>
<div class="fluid clearfix">
    {% for entity_image in images %}
        <div class="grid1" id="existed_image_{{ loop.index }}">
            <div style="display: none">
                {{ form_row(entity_image.image.file) }}
            </div>

            <div>

                <img src="{{ entity_image.vars.data.getThumbnail.image.getWebPath }}">

            </div>
            <div>
                <a href="#" onclick="$('#existed_image_{{ loop.index }}').remove();return false;"
                style="text-decoration:none; border-bottom:1px dashed;">Remove</a>
            </div>
        </div>
    {% endfor %}
</div>

<div class="subformsCollection fluid clearfix" data-prototype="{{ subformProto | e}}"  style="margin-top: 15px;">
</div>

<div class="fluid">
    <a class="addSubform" href="#" style="text-decoration:none; border-bottom:1px dashed;">Add image</a>
</div>
</div>

The Versions

24/06 2015

dev-master

9999999-dev

Mobile Image Resizer

  Sources   Download

MIT

image resize mobile