Пример #1
0
 /**
  * @Route("/upload/{t}", name="_upload")
  */
 public function uploadAction(Request $request, $t = 'b')
 {
     if ($t == 'a') {
         $title = '';
         $type = 0;
         $num = (int) $request->get('num');
         $file_name = 'photoA' . $num . '.png';
     } else {
         $uploadedFile = $request->files->get('image');
         if ($uploadedFile == null || !in_array(strtolower($uploadedFile->getClientOriginalExtension()), array('png', 'jpg', 'jpeg', 'gif'))) {
             return new Response('只能上传照片喔~');
         } elseif (!$uploadedFile->isValid()) {
             return new Response($uploadedFile->getErrorMessage());
         } else {
             $file_path = preg_replace('/app$/si', 'web/uploads', $this->get('kernel')->getRootDir());
             //$file_path = $request->getBasePath().$this->path;
             $file_name = date('YmdHis') . rand(1000, 9999) . '.' . $uploadedFile->getClientOriginalExtension();
             $fs = new Filesystem();
             if (!$fs->exists($file_path)) {
                 try {
                     $fs->mkdir($file_path);
                 } catch (IOException $e) {
                     return new Response('服务器发生错误,无法创建文件夹:' . $e->getPath());
                     //$result['msg'] = '服务器发生错误,无法创建文件夹:'.$e->getPath();
                 }
             }
             $uploadedFile->move($file_path, $file_name);
             $scale = $request->get('scale');
             $pos_x = (int) $request->get('pos_x');
             $pos_y = (int) $request->get('pos_y');
             $num = $request->get('num');
             $title = trim($request->get('title'));
             $img_url = $file_path . '/' . $file_name;
             $imagine = new Imagine();
             $exif = @exif_read_data($img_url, 0, true);
             if (isset($exif['IFD0']['Orientation'])) {
                 $photo = $imagine->open($img_url);
                 if ($exif['IFD0']['Orientation'] == 6) {
                     $photo->rotate(90)->save($img_url);
                 } elseif ($exif['IFD0']['Orientation'] == 3) {
                     $photo->rotate(180)->save($img_url);
                 }
             }
             $photo = $imagine->open($img_url);
             $size = $photo->getSize();
             $width = 640;
             $height = 1039;
             $w1 = $width * $scale;
             $h1 = $w1 * $size->getHeight() / $size->getWidth();
             $imagine->open($img_url)->resize(new Box($w1, $h1))->save($img_url);
             $imagine->open($img_url)->resize(new Box($w1, $h1))->save($img_url);
             #左移裁切
             if ($pos_x < 0) {
                 $size = $imagine->open($img_url)->getSize();
                 $imagine->open($img_url)->crop(new Point(abs($pos_x), 0), new Box($size->getWidth() - abs($pos_x), $size->getHeight()))->save($img_url);
             }
             #上移裁切
             if ($pos_y < 0) {
                 $size = $imagine->open($img_url)->getSize();
                 $imagine->open($img_url)->crop(new Point(0, abs($pos_y)), new Box($size->getWidth(), $size->getHeight() - abs($pos_y)))->save($img_url);
             }
             #右移拼贴
             if ($pos_x > 0) {
                 $photo = $imagine->open($img_url);
                 $size = $photo->getSize();
                 $collage_width = $pos_x + $size->getWidth() > $width ? $pos_x + $size->getWidth() : $width;
                 $collage_height = $size->getHeight();
                 $collage = $imagine->create(new Box($collage_width, $collage_height));
                 $collage->paste($photo, new Point($pos_x, 0))->save($img_url);
             }
             #下移拼贴
             if ($pos_y > 0) {
                 $photo = $imagine->open($img_url);
                 $size = $photo->getSize();
                 $collage_width = $size->getWidth();
                 $collage_height = $pos_y + $size->getHeight() > $width ? $pos_y + $size->getHeight() : $height;
                 $collage = $imagine->create(new Box($collage_width, $collage_height));
                 $collage->paste($photo, new Point(0, $pos_y))->save($img_url);
             }
             #超出剪切
             $photo = $imagine->open($img_url);
             $size = $photo->getSize();
             $_width = $size->getWidth();
             if ($size->getWidth() > $width) {
                 $_width = $width;
                 $imagine->open($img_url)->crop(new Point(0, 0), new Box($_width, $size->getHeight()))->save($img_url);
             }
             if ($size->getHeight() > $height) {
                 $imagine->open($img_url)->crop(new Point(0, 0), new Box($_width, $height))->save($img_url);
             }
             $photo = $imagine->open($img_url);
             $collage = $imagine->create(new Box($width, $height));
             $collage->paste($photo, new Point(0, 0))->save($img_url);
             $collage = $imagine->open('bundles/app/default/images/photoB' . $num . '.png');
             $photo = $imagine->open($img_url);
             $photo->paste($collage, new Point(0, 0))->save($img_url);
             $type = 1;
             /*
             $photo = $imagine->open($img_url);
             $color = new \Imagine\Image\Color;
             $font = new \Imagine\Gd\Font('',30, $color);
             $photo->draw()->text('tesintg',$font,new Point(0,0),0);
             */
         }
     }
     $em = $this->getDoctrine()->getManager();
     $em->getConnection()->beginTransaction();
     try {
         $photo = new Entity\Photo();
         $user = $this->getUser();
         $photo->setImgUrl($file_name);
         $photo->setUser($user);
         $photo->setTitle($title);
         $photo->setType($type);
         $photo->setFavourNum(0);
         $photo->setCreateIp($request->getClientIp());
         $photo->setCreateTime(new \DateTime('now'));
         $em->persist($photo);
         $em->flush();
         $em->getConnection()->commit();
         return $this->redirect($this->generateUrl('_finish', array('id' => $photo->getId())));
     } catch (Exception $e) {
         $em->getConnection()->rollback();
         return new Response($e->getMessage());
         //$json['ret'] = 1001;
         //$json['msg']= $e->getMessage();
     }
 }