Ejemplo n.º 1
1
 public function actionPhoto($id)
 {
     $model = $this->loadModel($id);
     $model->scenario = 'photo';
     Yii::trace("FC.actionPhoto called", 'application.controllers.FamilyController');
     if (Yii::app()->params['photoManip']) {
         if (isset($_POST['x1'])) {
             $x1 = $_POST['x1'];
             $y1 = $_POST['y1'];
             $width = $_POST['width'];
             $height = $_POST['height'];
             $pfile = $_POST['pfile'];
             $sdir = './images/uploaded/';
             $size = getimagesize($sdir . $pfile);
             if ($size) {
                 list($w, $h, $t) = $size;
             } else {
                 Yii::trace("FR.actionPhoto crop call to getimagesize failed for image " . $sdir . $pfile . " returned {$size}", 'application.controllers.FamilyController');
             }
             Yii::trace("FC.actionPhoto crop received {$x1}, {$y1}, {$width}, {$height}, {$w}, {$h}, {$t}", 'application.controllers.FamilyController');
             switch ($t) {
                 case 1:
                     $img = imagecreatefromgif($sdir . $pfile);
                     break;
                 case 2:
                     $img = imagecreatefromjpeg($sdir . $pfile);
                     break;
                 case 3:
                     $img = imagecreatefrompng($sdir . $pfile);
                     break;
                 case IMAGETYPE_BMP:
                     $img = ImageHelper::ImageCreateFromBMP($sdir . $pfile);
                     break;
                 case IMAGETYPE_WBMP:
                     $img = imagecreatefromwbmp($sdir . $pfile);
                     break;
                 default:
                     Yii::trace("FC.actionPhoto crop unknown image type {$t}", 'application.controllers.FamilyController');
             }
             if (function_exists('imagecrop')) {
                 # untested
                 $cropped = imagecrop($img, array('x1' => $x1, 'y1' => $y1, 'width' => $width, 'height' => $height));
                 $scaled = imagescale($cropped, 400);
             } else {
                 $h = $height * 400 / $width;
                 $scaled = imagecreatetruecolor(400, $h);
                 imagecopyresized($scaled, $img, 0, 0, $x1, $y1, 400, $h, $width, $height);
             }
             $dir = './images/families/';
             $fname = preg_replace('/\\.[a-z]+$/i', '', $pfile);
             $fext = ".jpg";
             if (file_exists($dir . $pfile)) {
                 $fname .= "_01";
                 while (file_exists($dir . $fname . $fext)) {
                     ++$fname;
                 }
             }
             $dest = $dir . $fname . $fext;
             imagejpeg($scaled, $dest, 90);
             imagedestroy($scaled);
             imagedestroy($img);
             unlink($sdir . $pfile);
             $model->photo = $fname . $fext;
             $model->save(false);
             Yii::trace("FC.actionPhoto saved to {$pfile}", 'application.controllers.FamilyController');
             $this->redirect(array('view', 'id' => $model->id));
             return;
         } elseif (isset($_FILES['Families'])) {
             Yii::trace("FC.actionPhoto _FILES[Families] set", 'application.controllers.FamilyController');
             $files = $_FILES['Families'];
             $filename = $files['name']['raw_photo'];
             if (isset($filename) and '' != $filename) {
                 Yii::trace("FC.actionPhoto filename {$filename}", 'application.controllers.FamilyController');
                 $tmp_path = $files['tmp_name']['raw_photo'];
                 if (isset($tmp_path) and '' != $tmp_path) {
                     Yii::trace("FC.actionPhoto tmp_path {$tmp_path}", 'application.controllers.FamilyController');
                     $dir = "./images/uploaded/";
                     $dest = $dir . $filename;
                     list($width, $height) = getimagesize($tmp_path);
                     if ($width < 900) {
                         $w = $width;
                         $h = $height;
                         $zoom = 1;
                     } else {
                         $w = 900;
                         $h = $height * 900 / $width;
                         $zoom = $w / $width;
                     }
                     $w = $width < 900 ? $width : 900;
                     move_uploaded_file($tmp_path, $dest);
                     $this->render('crop', array('model' => $model, 'pfile' => $filename, 'width' => $w, 'height' => $h, 'zoom' => $zoom));
                     return;
                 } else {
                     $errors = array(1 => "Size exceeds max_upload", 2 => "FORM_SIZE", 3 => "No tmp dir", 4 => "can't write", 5 => "error extension", 6 => "error partial");
                     $error = $errors[$files['error']['raw_photo']];
                     Yii::trace("FC.actionPhoto file error {$error}", 'application.controllers.FamilyController');
                 }
             }
         }
     } elseif (isset($_FILES['Families'])) {
         $files = $_FILES['Families'];
         $filename = $files['name']['photo'];
         if (isset($filename) and '' != $filename) {
             $tmp_path = $files['tmp_name']['photo'];
             if (isset($tmp_path) and '' != $tmp_path) {
                 $dir = "./images/families/";
                 $fname = preg_replace('/\\.[a-z]+$/i', '', $filename);
                 preg_match('/(\\.[a-z]+)$/i', $filename, $matches);
                 $fext = $matches[0];
                 if (file_exists($dir . $filename)) {
                     $fname .= "_01";
                     while (file_exists($dir . $fname . $fext)) {
                         ++$fname;
                     }
                 }
                 $dest = $dir . $fname . $fext;
                 $model->photo = $fname . $fext;
                 if ($model->save()) {
                     move_uploaded_file($tmp_path, $dest);
                     $this->redirect(array('view', 'id' => $model->id));
                     return;
                 }
             } else {
                 $model->addError('photo', $files['error']['photo']);
             }
         }
     }
     $this->render('photo', array('model' => $model));
 }