Ejemplo n.º 1
0
 public function testBasic()
 {
     $assetsRoot = __DIR__ . '/assets/';
     $flower = $assetsRoot . 'flower.jpg';
     $im = new ImageProcessor($flower);
     $im->fitOut(100, 100, ImageProcessor::GRAVITY_EAST);
     $im->saveAs($assetsRoot . 'flower-100x100.jpg');
     $info = ImageProcessor::getImageInfo($assetsRoot . 'flower-100x100.jpg');
     $this->assertEquals(100, $info['width'], 'width of fitOut is ok');
     $this->assertEquals(100, $info['height'], 'height of fitOut is ok');
     $im->fitIn(100, 100);
     $im->saveAs($assetsRoot . 'flower-100x62.jpg');
 }
Ejemplo n.º 2
0
 /**
  * @param Model|ModelCollection $caller
  * @param $alias
  * @param $filePath
  * @param array $sourceFileInfo
  * @throws \Exception
  */
 public function attachFileFromPath($caller, $alias, $filePath, $sourceFileInfo = array())
 {
     $this->requireModeSingle($caller);
     if (!is_file($filePath)) {
         throw new \Exception('File specified is not readable');
     }
     if (!array_key_exists($alias, $this->_config)) {
         throw new \Exception('There is no such alias ' . $alias);
     }
     if (!is_array($this->_config[$alias])) {
         $this->_config[$alias] = array();
     }
     $storeLocation = $this->_getAliasFolder($caller, $alias) . '/';
     FSService::makeWritable($storeLocation);
     if (empty($sourceFileInfo)) {
         $sourceFileInfo = FSService::getFileInfo($filePath);
     }
     $newFileExtension = $sourceFileInfo['ext'];
     $newFileName = md5($sourceFileInfo['name'] . time());
     if (!empty($this->_config[$alias]['name'])) {
         if ($this->_config[$alias]['name'] == 'original') {
             $newFileName = $sourceFileInfo['name'];
             while (is_file($storeLocation . $newFileName . $newFileExtension)) {
                 $newFileName .= '_1';
             }
         }
     }
     if (empty($this->_config[$alias]['multiple'])) {
         $filesToDelete = GLOB($storeLocation . '*.*');
         foreach ($filesToDelete as $file) {
             unlink($file);
         }
     }
     copy($filePath, $storeLocation . $newFileName . $newFileExtension);
     if (!empty($this->_config[$alias]['sizes'])) {
         $ip = new ImageProcessor($storeLocation . $newFileName . $newFileExtension);
         foreach ($this->_config[$alias]['sizes'] as $sizeAlias => $sizeInfo) {
             if (!is_array($sizeInfo)) {
                 $sizeInfo = array('size' => $sizeInfo);
             }
             $method = empty($sizeInfo['method']) ? 'fitOut' : $sizeInfo['method'];
             $params = explode('x', $sizeInfo['size']);
             if (!empty($sizeInfo['geometry'])) {
                 $params[] = $sizeInfo['geometry'];
             }
             call_user_func_array(array($ip, $method), $params);
             $ip->saveAs($storeLocation . $sizeAlias . '/' . $newFileName . $newFileExtension);
         }
     }
     $caller->_setRawFieldValue($alias, $this->getModelValue($storeLocation, $this->_config[$alias]));
 }