/** * * @Route("/admin/category/edit/{id}", name="admin_category_edit") */ public function editAction(Request $request, $id) { $category = $this->getDoctrineRepo('AppBundle:Category')->find($id); if (!$category) { throw $this->createNotFoundException('No category found for id ' . $id); } $form = $this->createFormBuilder($category)->add('id', 'hidden')->add('name', 'text', array('constraints' => array(new NotBlank(), new Length(array('max' => 256)))))->add('slug', 'text', array('constraints' => array(new NotBlank(), new Length(array('max' => 256)), new Regex(array('pattern' => '/^[a-z][-a-z0-9]*$/', 'htmlPattern' => '/^[a-z][-a-z0-9]*$/', 'message' => 'This is not a valid slug')))))->add('position', 'integer', array('required' => false, 'constraints' => array(new Type(array('type' => 'integer')))))->getForm(); //when the form is posted this method prefills entity with data from form $form->handleRequest($request); if ($form->isValid()) { //check if there is file $file = $request->files->get('upl'); $em = $this->getDoctrine()->getManager(); if ($file != null && $file->isValid()) { //remove old Image (both file from filesystem and entity from db) $this->getDoctrineRepo('AppBundle:Image')->removeImage($category, $this->getParameter('image_storage_dir')); // save file $uuid = Utils::getUuid(); $image_storage_dir = $this->getParameter('image_storage_dir'); //$destDir = sprintf("%scategory\\",$image_storage_dir); $destDir = $image_storage_dir . DIRECTORY_SEPARATOR . 'category' . DIRECTORY_SEPARATOR; $destFilename = sprintf("%s.%s", $uuid, $file->getClientOriginalExtension()); $file->move($destDir, $destFilename); // create object $img = new Image(); $img->setUuid($uuid); $img->setName($destFilename); $img->setExtension($file->getClientOriginalExtension()); $img->setOriginalPath($file->getClientOriginalName()); $img->setPath('category'); $em->persist($img); $em->flush(); $category->setImage($img); } // save to db $em->persist($category); $em->flush(); return $this->redirectToRoute("admin_category_list"); } return $this->render('admin/category/edit.html.twig', array('form' => $form->createView(), 'category' => $category)); }
private function handleImages($eqFiles, $eq, $em) { foreach ($eqFiles as $file) { // store the original, and image itself $origFullPath = $this->getParameter('image_storage_dir') . DIRECTORY_SEPARATOR . 'equipment' . DIRECTORY_SEPARATOR . 'original' . DIRECTORY_SEPARATOR . $file[0] . '.' . $file[2]; $imgFullPath = $this->getParameter('image_storage_dir') . DIRECTORY_SEPARATOR . 'equipment' . DIRECTORY_SEPARATOR . $file[0] . '.' . $file[2]; rename($file[3], $origFullPath); // check image size $imgInfo = getimagesize($origFullPath); $ow = $imgInfo[0]; // original width $oh = $imgInfo[1]; // original height $r = $ow / $oh; // ratio $nw = $ow; // new width $nh = $oh; // new height $scale = False; if ($r > 1) { if ($ow > 1024) { $nw = 1024; $m = $nw / $ow; // multiplier $nh = $oh * $m; $scale = True; } } else { if ($oh > 768) { $nh = 768; $m = $nh / $oh; // multiplier $nw = $ow * $m; $scale = True; } } // scale the image if ($scale) { if ($file[2] == 'png') { $img = imagecreatefrompng($origFullPath); } else { $img = imagecreatefromjpeg($origFullPath); } $sc = imagescale($img, intval(round($nw)), intval(round($nh)), IMG_BICUBIC_FIXED); if ($file[2] == 'png') { imagepng($sc, $imgFullPath); } else { imagejpeg($sc, $imgFullPath); } } else { copy($origFullPath, $imgFullPath); } // store entry in database $img = new Image(); $img->setUuid($file[0]); $img->setName($file[1]); $img->setExtension($file[2]); $img->setPath('equipment'); $img->setOriginalPath('equipment' . DIRECTORY_SEPARATOR . 'original'); $em->persist($img); $em->flush(); $eq->addImage($img); $em->flush(); } }