PHP code example of cesurapp / media-bundle

1. Go to this page and download the library: Download cesurapp/media-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cesurapp / media-bundle example snippets


use Cesurapp\MediaBundle\Entity\MediaSuperClass;
use Cesurapp\MediaBundle\Entity\Traits\MediaTrait;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\HasLifecycleCallbacks]
class Post extends MediaSuperClass
{
    use MediaTrait;

    // Columns whose media are removed together with the entity
    public function getMediaColumns(): array
    {
        return ['media'];
    }
}

use Cesurapp\MediaBundle\Manager\MediaManager;

// HTTP multipart files, optional key filter and allowed types per key
$medias = $manager->uploadHttpFile($request, ['photos'], [], ['photos' => ['image/jpeg', 'image/png']]);

// Base64 fields
$medias = $manager->uploadHttpBase64($request, ['image'], ['image' => ['image/png', 'image/jpeg']]);

// Remote links (private networks refused, 20 MB default limit)
$medias = $manager->uploadHttpLink($request, ['imageUrl'], ['imageUrl' => ['image/png']]);

// Single sources
$media = $manager->uploadFromUploadedFile($file, ['image/png']);
$media = $manager->uploadFromBase64($base64, ['image/png']);
$media = $manager->uploadFromUrl($url, ['image/png']);
$media = $manager->uploadFromContent($content, 'image/png', 'png');
$media = $manager->uploadFromData($dto->validated('avatar')); // from Base64FileValidator

$manager->save($medias, $em); // persist + flush
$post->addMedia($medias);

$media = $post->getMedia();          // array<string, Media>
$first = $user->getAvatarFirst();

$media->toString($storage);          // public or signed URL
$media->getResponse($storage);       // HTTP response (nosniff, private files never shared-cached)
$media->getContent($storage);

// Media columns hold lazy references: load a whole list in one query
$mediaRepository->preload(array_map(fn (User $u) => $u->getAvatar(), $users));

$em->remove($media);
$em->flush(); // object deleted from storage once the transaction commits

$em->remove($post);
$em->flush(); // MediaSuperClass removes the post's media in the same flush