public function newlocAction()
 {
     $request = $this->getRequest();
     $query = $request->getParam('q');
     /*$size = [
     			'square' => 'crop_99_103', //legalpanelsidecontent
     			'thumbnail' => 'resize_100_53', //terbaru
     			'multimedia' => 'resize_245_169', //multimediabxslider
     			'small' => 'resize_213_142', //klinik
     			'headsmall' => 'resize_213_160', //header berita
     			'crop' => 'crop_324_169', //ijt
     			'cropnext' => 'crop_325_183', //nextevent
     			'mainhead' => 'resize_462_309', //utama
     			'medium' => 'resize_646_431' //detailberita
     		];*/
     $tool = 'gd';
     $size = new Zend_Config_Ini(APPLICATION_PATH . '/configs/image.ini', 'size');
     $sizes = array();
     foreach ($size->toArray() as $key => $value) {
         list($method, $width, $height) = explode('_', $value);
         $sizes[$key] = array('method' => $method, 'width' => $width, 'height' => $height);
     }
     /**
      * Generate thumbnails
      */
     $thumbnailSizes = array_keys($sizes);
     $db = $this->db;
     $db->setFetchMode(Zend_Db::FETCH_OBJ);
     $select = $db->select();
     $select->from('KutuCatalog', '*');
     if (isset($query) && !empty($query)) {
         $select->where($query);
     }
     $select->order('createdDate ASC');
     $rowsFound = $db->fetchAll($select);
     echo 'There are ' . count($rowsFound) . " catalog(s)\n";
     $rowCount = count($rowsFound);
     for ($iCount = 0; $iCount < $rowCount; $iCount++) {
         $row = $rowsFound[$iCount];
         $guid = $row->guid;
         $fileImage = null;
         $rowsetRelatedItem = $this->getDocumentById($guid, 'RELATED_IMAGE', true, "relatedGuid DESC");
         if ($rowsetRelatedItem) {
             $g = 0;
             foreach ($rowsetRelatedItem as $related) {
                 //get image url
                 $rowDocSystemName = $this->getCatalogAttribute($related->itemGuid, 'docSystemName');
                 if ($rowDocSystemName) {
                     $fileName = pathinfo($rowDocSystemName, PATHINFO_FILENAME);
                     $ext = pathinfo($rowDocSystemName, PATHINFO_EXTENSION);
                     $ext = strtolower($ext);
                     if (substr($fileName, 0, 2) !== 'lt') {
                         $fileName = $related->itemGuid;
                     }
                     $ig = $this->getItemRelated($fileName, 'RELATED_IMAGE');
                     if ($ig) {
                         $guid = $ig->relatedGuid;
                     }
                     $cdn = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application-cli.ini', 'cdn');
                     //$dir = $cdn->static->dir->images . DIRECTORY_SEPARATOR . 'upload';
                     $dir = $cdn->static->dir->images;
                     //$catalogDb = $this->getCatalog($related->itemGuid, ['createdBy','createdDate']);
                     //$path = implode(DS, array(strip_tags(trim($catalogDb->createdBy)), date('Y',strtotime($catalogDb->createdDate)), date('m',strtotime($catalogDb->createdDate)), date('d',strtotime($catalogDb->createdDate))));
                     $path = implode(DS, [$guid]);
                     Pandamp_Utility_File::createDirs($dir, $path);
                     //$fileName  = uniqid('lt');
                     //$fileName = $related->itemGuid;
                     // taruh file original nya di folder tujuan baru
                     $fileku = $dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $fileName . '.' . $ext;
                     //file_put_contents($fileku, file_get_contents($image));
                     $this->log()->info('Path image:' . $fileku);
                     $service = null;
                     $service = new Pandamp_Image_GD();
                     $baseUrl = $cdn->static->url->images;
                     if ($image = $this->giu($guid, $fileName, $ext, null, "local")) {
                         $fileImage[$g]['original'] = $image;
                     }
                     if ($th = $this->giu($guid, $fileName, $ext, "tn_", "local")) {
                         $fileImage[$g]['thumbnail'] = $th;
                     }
                     //mulai optimasi
                     foreach ($thumbnailSizes as $s) {
                         $service->setFile($image);
                         $method = $sizes[$s]['method'];
                         $width = $sizes[$s]['width'];
                         $height = $sizes[$s]['height'];
                         $f = $s . '_' . $fileName . '.' . $ext;
                         $newFile = $dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $f;
                         /**
                          * Create thumbnail
                          */
                         switch ($method) {
                             case 'resize':
                                 $service->resizeLimit($newFile, $width, $height);
                                 break;
                             case 'crop':
                                 $service->crop($newFile, $width, $height);
                                 break;
                         }
                         if ($img = $this->giu($guid, $fileName, $ext, $s . '_', "local")) {
                             $fileImage[$g][$s] = $img;
                         }
                     }
                     // end foreach optimasi
                     if ($caption = $this->getCatalogAttribute($fileName, "fixedTitle")) {
                         $fileImage[$g]['caption'] = strip_tags(trim($caption));
                     }
                     //beritahu nama file baru catalogAttribute
                     $db->update('KutuCatalogAttribute', ['value' => $fileName . '.' . $ext], "catalogGuid='{$related->itemGuid}' AND attributeGuid='docSystemName'");
                     $this->log()->info('update KutuCatalogAttribute dengan value:' . $fileName . '.' . $ext . ' dimana catalogGuid=' . $related->itemGuid . ' dan attributeGuid=docSystemName');
                     $this->log()->info('upindex id=' . $related->itemGuid . ' ubah systemName:' . $fileName . '.' . $ext);
                     try {
                         //update document
                         $this->addHitsBySolr(json_encode([["id" => $related->itemGuid, "systemName" => ["set" => $fileName . '.' . $ext], "modifiedDate" => ["set" => date("Y-m-d\\TH:i:s\\Z")]]]));
                     } catch (Zend_Exception $e) {
                         $this->log()->err($e->getMessage());
                     }
                 }
                 $g++;
             }
             //end foreach
             //update catalog
             $this->addHitsBySolr(json_encode([["id" => $guid, "fileImage" => ["set" => Zend_Json::encode($fileImage)], "modifiedDate" => ["set" => date("Y-m-d\\TH:i:s\\Z")]]]));
             $this->log()->info('upindex id=' . $guid . ' ubah fileImage:' . Zend_Json::encode($fileImage));
             //$this->log()->info(Zend_Json::encode($fileImage));
         }
     }
     sleep(1);
     echo "Images optimizing completed\n";
 }
Exemple #2
0
 public function uploadAction()
 {
     $this->_helper->getHelper('viewRenderer')->setNoRender();
     $this->_helper->getHelper('layout')->disableLayout();
     $request = $this->getRequest();
     if (!$request->isPost()) {
         return;
     }
     $user = Zend_Auth::getInstance()->getIdentity();
     $userName = $user->username;
     $type = $request->getParam('type');
     $newguid = $request->getParam('guid');
     $cdn = Pandamp_Application::getOption('cdn');
     if ($type == 'file') {
         $dir = $cdn['static']['dir']['files'];
         $adapter = new Zend_File_Transfer_Adapter_Http();
         $files = $adapter->getFileInfo();
         foreach ($files as $file => $info) {
             $guid = (new Pandamp_Core_Guid())->generateGuid();
             $path = implode(DS, array($newguid));
             Pandamp_Utility_File::createDirs($dir, $path);
             $adapter->setDestination($dir . DS . $path);
             $name = $adapter->getFileName($file, false);
             $fileku = $dir . DS . $path . DS . strtoupper(str_replace(' ', '_', $name));
             $adapter->addFilter('rename', ['overwrite' => true, 'target' => $fileku]);
             // file uploaded & is valid
             if (!$adapter->isUploaded($file)) {
                 continue;
             }
             if (!$adapter->isValid($file)) {
                 continue;
             }
             // receive the files into the user directory
             $adapter->receive($file);
             /*
             $baseUrl = $cdn['static']['url']['files'];
             if (isset($_SERVER['SCRIPT_NAME']) && ($pos = strripos($baseUrl, basename($_SERVER['SCRIPT_NAME']))) !== false) {
             	$baseUrl = substr($baseUrl, 0, $pos);
             }
             $prefixUrl 		 = rtrim($baseUrl, '/') . '/' . $userName . '/' . date('Y') . '/' . date('m');
             $ret['original'] = array(
             	'title' => strtoupper(str_replace(' ','_',$name)),
             	'url'  => $prefixUrl . '/' . strtoupper(str_replace(' ','_',$name)),
             	'size' => $adapter->getFileSize($file),
             	'filetype' => $adapter->getMimeType($file),
             	'type' => $type	
             );
             */
             $ret['original'] = array('id' => $guid, 'title' => strtoupper(str_replace(' ', '_', $name)), 'size' => $adapter->getFileSize($file), 'filetype' => $adapter->getMimeType($file), 'type' => $type);
         }
     } else {
         /*$size = [
         			'square' => 'crop_99_103',
         			'thumbnail' => 'resize_100_53',
         			'multimedia' => 'resize_245_169',
         			'small' => 'resize_213_142', //klinik
         			'headsmall' => 'resize_213_160', //header berita
         			'crop' => 'crop_324_169', //ijt
         			'cropnext' => 'crop_325_183', //nextevent
         			'mainhead' => 'resize_462_309', //utama
         			'medium' => 'resize_646_431'
         		];*/
         $tool = 'gd';
         $size = new Zend_Config_Ini(APPLICATION_PATH . '/configs/image.ini', 'size');
         $sizes = array();
         foreach ($size->toArray() as $key => $value) {
             list($method, $width, $height) = explode('_', $value);
             $sizes[$key] = array('method' => $method, 'width' => $width, 'height' => $height);
         }
         /**
          * Prepare folders
          */
         //$dir  = $cdn['static']['dir']['images'] . DS . 'upload';
         $dir = $cdn['static']['dir']['images'];
         //$path = implode(DS, array($userName, date('Y'), date('m'), date('d')));
         $path = implode(DS, array($newguid));
         Pandamp_Utility_File::createDirs($dir, $path);
         /**
          * Upload file
          */
         $adapter = new Zend_File_Transfer_Adapter_Http();
         $adapter->setDestination($dir . DS . $path);
         $adapter->addValidator('Extension', false, 'jpg,png,gif');
         $adapter->addValidator('Size', false, 5242880);
         $files = $adapter->getFileInfo();
         foreach ($files as $file => $info) {
             $name = $adapter->getFileName($file);
             $ext = explode('.', $name);
             $extension = $ext[count($ext) - 1];
             $extension = strtolower($extension);
             //$fileName  = uniqid();
             $fileName = (new Pandamp_Core_Guid())->generateGuid();
             $fileku = $dir . DS . $path . DS . $fileName . '.' . $extension;
             $adapter->addFilter('rename', $fileku);
             // file uploaded & is valid
             if (!$adapter->isUploaded($file)) {
                 continue;
             }
             if (!$adapter->isValid($file)) {
                 continue;
             }
             // receive the files into the user directory
             $adapter->receive($file);
             // this has to be on top
             /**
              * Generate thumbnails
              */
             $thumbnailSizes = array_keys($sizes);
             $service = null;
             $service = new Pandamp_Image_GD();
             /**
              * Remove script filename from base URL
              */
             $baseUrl = $cdn['static']['url']['images'];
             /*
             if (isset($_SERVER['SCRIPT_NAME']) && ($pos = strripos($baseUrl, basename($_SERVER['SCRIPT_NAME']))) !== false) {
             	$baseUrl = substr($baseUrl, 0, $pos);
             }
             $prefixUrl 		 = rtrim($baseUrl, '/') . '/upload/' . $userName . '/' . date('Y') . '/' . date('m') . '/' . date('d');
             */
             $prefixUrl = $baseUrl . '/' . $newguid;
             $ret['original'] = array('id' => $fileName, 'title' => $fileName . '.' . $extension, 'url' => $prefixUrl . '/' . $fileName . '.' . $extension, 'size' => null, 'filetype' => $adapter->getMimeType($file), 'type' => $type);
             if ($thumbnailSizes) {
                 $service->setFile($fileku);
                 $ret['original']['size'] = $service->getWidth() . ' x ' . $service->getHeight();
                 foreach ($thumbnailSizes as $s) {
                     $service->setFile($fileku);
                     $method = $sizes[$s]['method'];
                     $width = $sizes[$s]['width'];
                     $height = $sizes[$s]['height'];
                     $f = $s . '_' . $fileName . '.' . $extension;
                     $newFile = $dir . DS . $path . DS . $f;
                     /**
                      * Create thumbnail
                      */
                     switch ($method) {
                         case 'resize':
                             $service->resizeLimit($newFile, $width, $height);
                             break;
                         case 'crop':
                             $service->crop($newFile, $width, $height);
                             break;
                     }
                     /*$ret[$s] = array(
                     			'title' => 	$f,
                     			'url'  => $prefixUrl . '/' . $f,
                     			'size' => $width . ' x ' . $height,
                     			'filetype' => $adapter->getMimeType($file),
                     			'type' => $type
                     		);*/
                 }
             }
         }
     }
     /**
      * Return the reponse
      */
     $ret = Zend_Json::encode($ret);
     $this->getResponse()->setBody($ret);
 }