Mobile Image Resizer
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
image_resize: image_loader: imagine_class: 'Imagine\Gd\Imagine' type: 'file'
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>
<?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); } }
public function editAction() { ............. if ($form->isValid()) { /** @var ProductManager $productManager */ $productManager = $this->get('project.main.product_manager'); $productManager->saveProduct($product); ............. }
............. {% 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'}) }} .............
{% 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>