/** * Gives an image tag for an asset * * @param sfAsset $asset * @param string $thumbType * @param array $options * @param string $relativePath * @return string */ function asset_image_tag($asset, $thumbType = 'full', $options = array(), $relativePath = null) { $options = array_merge(array('alt' => $asset->getDescription() . ' ' . $asset->getCopyright(), 'title' => $asset->getDescription() . ' ' . $asset->getCopyright()), $options); if ($asset->isImage() || $asset->isPdf()) { $src = $asset->getUrl($thumbType, $relativePath, $asset->isPdf()); if ($asset->isPdf() && !is_readable(sfConfig::get('sf_web_dir') . $src)) { $src = '/sfDoctrineAssetsLibraryPlugin/images/pdf.png'; } } elseif ($thumbType == 'full') { throw new sfAssetException('Impossible to render a non-image asset in an image tag'); } else { switch ($asset->getType()) { case 'txt': $src = '/sfDoctrineAssetsLibraryPlugin/images/txt.png'; break; case 'xls': $src = '/sfDoctrineAssetsLibraryPlugin/images/xls.png'; break; case 'doc': $src = '/sfDoctrineAssetsLibraryPlugin/images/doc.png'; break; case 'pdf': $src = '/sfDoctrineAssetsLibraryPlugin/images/pdf.png'; break; case 'html': $src = '/sfDoctrineAssetsLibraryPlugin/images/html.png'; break; case 'archive': $src = '/sfDoctrineAssetsLibraryPlugin/images/archive.png'; break; case 'bin': $src = '/sfDoctrineAssetsLibraryPlugin/images/bin.png'; break; default: $src = '/sfDoctrineAssetsLibraryPlugin/images/unknown.png'; } } return image_tag($src, $options); }
$t->is($sfAsset->getUrl('small'), '/' . sfConfig::get('app_sfAssetsLibrary_upload_dir') . '/thumbnail/small_filename.jpg', 'getUrl(\'small\') returns the small thumbnail url'); $t->is($sfAsset->getUrl('large'), '/' . sfConfig::get('app_sfAssetsLibrary_upload_dir') . '/thumbnail/large_filename.jpg', 'getUrl(\'large\') returns the large thumbnail url'); $t->diag('sfAsset::create()'); $assets_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR; $test_asset = $assets_path . 'raikkonen.jpg'; $folder1 = new sfAssetFolder(); $folder1->getNode()->insertAsFirstChildOf($root); $folder1->setName('Test_Directory'); $folder1->save(); $asset1 = new sfAsset(); $asset1->setFolder($folder1); $asset1->create($test_asset, false); $asset_file_size = (int) (filesize($test_asset) / 1024); $t->is($asset1->getFilename(), 'raikkonen.jpg', 'create() sets the object filename according to the given asset'); $t->is($asset1->getFilesize(), 18, 'create() sets the object filesize according to the given asset'); $t->ok($asset1->isImage(), 'create() sets the object type according to the given asset'); $t->is($asset1->getUrl(), $folder1->getUrl() . '/' . $asset1->getFilename(), 'create() sets the object url according to the given asset and folder object'); $t->ok(is_file($asset1->getFullPath()), 'create() physically copies asset'); $t->ok(is_file($asset1->getFullPath('large')), 'create() physically creates thumbnail'); $t->diag('sfAsset::move()'); $old_path = $asset1->getFullPath(); $old_thumb_path = $asset1->getFullPath('large'); $asset1->move($root, 'raikkonen2.jpg'); $t->is($asset1->getFilename(), 'raikkonen2.jpg', 'move() changes filename'); $t->is($asset1->getUrl(), $root->getUrl() . '/' . $asset1->getFilename(), 'move() changes the object url according to the new folder'); $t->ok(is_file($asset1->getFullPath()), 'move() physically moves asset to the new location'); $t->ok(!is_file($old_path), 'move() physically removes asset from the previous location'); $t->ok(is_file($asset1->getFullPath('large')), 'move() physically moves thumbnail to the new location'); $t->ok(!is_file($old_thumb_path), 'move() physically removes thumbnail from the previous location'); $t->diag('sfAsset::delete()'); $old_path = $asset1->getFullPath();
$t->is($sfAsset->getRelativePath(), sfConfig::get('app_sfAssetsLibrary_upload_dir', 'media') . DIRECTORY_SEPARATOR . 'filename.jpg', 'getRelativePath() gives correct result'); // sfConfig::set('sf_web_dir', 'var/www/myproject'); sfConfig::set('app_sfAssetsLibrary_upload_dir', 'medias'); $t->is($sfAsset->getFullPath(), sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . sfConfig::get('app_sfAssetsLibrary_upload_dir', 'media') . DIRECTORY_SEPARATOR . 'filename.jpg', 'getFullPath() gives complete path'); $t->is($sfAsset->getFullPath('large'), sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . sfConfig::get('app_sfAssetsLibrary_upload_dir', 'media') . DIRECTORY_SEPARATOR . 'thumbnail/large_filename.jpg', 'getFullPath() gives correct thumbnail path'); $t->is($sfAsset->getUrl(), DIRECTORY_SEPARATOR . sfConfig::get('app_sfAssetsLibrary_upload_dir', 'media') . DIRECTORY_SEPARATOR . 'filename.jpg', 'getUrl() gives correct url'); $t->is($sfAsset->getUrl('small'), DIRECTORY_SEPARATOR . sfConfig::get('app_sfAssetsLibrary_upload_dir', 'media') . DIRECTORY_SEPARATOR . 'thumbnail/small_filename.jpg', 'getUrl() gives correctthumbnail url'); $assets_path = dirname(__FILE__) . '/../assets/'; $test_asset = $assets_path . 'raikkonen.jpg'; $t->ok(is_file($test_asset), 'test asset found'); $sfAsset = new sfAsset(); $sfAsset->setsfAssetFolder(sfAssetFolderPeer::getRoot()); $sfAsset->create($test_asset, false); $t->is($sfAsset->getFilename(), 'raikkonen.jpg', 'create() gives correct filename'); $t->is($sfAsset->getFilesize(), 18, 'create() gives correct size'); $t->ok($sfAsset->isImage(), 'create() gives correct type'); $t->ok(is_file($sfAsset->getFullPath()), 'create() physically copies asset'); $t->ok(is_file($sfAsset->getFullPath('large')), 'create() physically creates thumbnail'); $old_path = $sfAsset->getFullPath(); $old_thumb_path = $sfAsset->getFullPath('large'); $sfAsset->move(sfAssetFolderPeer::getRoot(), 'raikkonen2.jpg'); $t->is($sfAsset->getFilename(), 'raikkonen2.jpg', 'move() changes filename'); $t->ok(is_file($sfAsset->getFullPath()), 'move() physically moves asset'); $t->ok(!is_file($old_path), 'move() physically moves asset'); $t->ok(is_file($sfAsset->getFullPath('large')), 'move() physically moves thumbnail'); $t->ok(!is_file($old_thumb_path), 'move() physically moves thumbnail'); $old_path = $sfAsset->getFullPath(); $old_thumb_path = $sfAsset->getFullPath('large'); $old_id = $sfAsset->getId(); $sfAsset->delete(); $t->ok(!is_file($old_path), 'delete() physically removes asset');