コード例 #1
0
 function ajaxstoreimageAction()
 {
     $currentTime = date("Y-m-d H:i:s");
     $uploaddir = 'images/';
     $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
     $campaign_id = (int) $this->_request->getParam('campaign_id');
     $imgfile = $_FILES['userfile'];
     if (is_array($imgfile)) {
         $name = $imgfile['name'];
         $type = $imgfile['type'];
         $size = $imgfile['size'];
         if (!preg_match('/^image\\//i', $type) ? true : false) {
             $this->view->error = "璇蜂笂浼犳纭殑鍥剧墖";
         } else {
             if ($size > 2000000) {
                 $this->view->error = "鍥剧墖涓嶅緱瓒呰�?M";
             } else {
                 $tmpfile = $imgfile['tmp_name'];
                 if ($tmpfile && is_uploaded_file($tmpfile)) {
                     $file = fopen($tmpfile, "rb");
                     //$imgdata = bin2hex(fread($file,$size)); //bin2hex()灏嗕簩杩涘埗鏁版嵁杞崲鎴愬崄鍏繘鍒惰〃绀�?
                     $imgdata = fread($file, $size);
                     fclose($file);
                     // save to db
                     $imageReportModel = new ImageReport();
                     $imageReport = $imageReportModel->createRow();
                     $imageReport->campaign_id = $campaign_id;
                     $imageReport->consumer_id = $this->_currentUser->id;
                     $imageReport->state = 'NEW';
                     $imageReport->create_date = $currentTime;
                     $imageReport->file_name = $name;
                     $imageReport->type = $type;
                     $imageReport->image = $imgdata;
                     $maxwidth = 80;
                     $maxheight = 80;
                     $im = imagecreatefromstring($imgdata);
                     $width = imagesx($im);
                     $height = imagesy($im);
                     $newwidth = $width;
                     $newheight = $height;
                     if ($maxwidth && $width > $maxwidth || $maxheight && $height > $maxheight) {
                         if ($maxwidth && $width > $maxwidth) {
                             $widthratio = $maxwidth / $width;
                             $RESIZEWIDTH = true;
                         }
                         if ($maxheight && $height > $maxheight) {
                             $heightratio = $maxheight / $height;
                             $RESIZEHEIGHT = true;
                         }
                         if ($RESIZEWIDTH && $RESIZEHEIGHT) {
                             if ($widthratio < $heightratio) {
                                 $ratio = $widthratio;
                             } else {
                                 $ratio = $heightratio;
                             }
                         } elseif ($RESIZEWIDTH) {
                             $ratio = $widthratio;
                         } elseif ($RESIZEHEIGHT) {
                             $ratio = $heightratio;
                         }
                         $newwidth = $width * $ratio;
                         $newheight = $height * $ratio;
                     }
                     $imageReport->thumb_width = round($newwidth);
                     $imageReport->thumb_height = round($newheight);
                     $imageReport->save();
                     $this->view->imageReport = $imageReport;
                 }
             }
         }
     }
     $this->view->campaign_id = $campaign_id;
     $this->_helper->layout->disableLayout();
 }