PHP code example of digitaldream / photo

1. Go to this page and download the library: Download digitaldream/photo 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/ */

    

digitaldream / photo example snippets


 composer 

 php artisan migrate

 php artisan vendor:publish --provider="Photo/PhotoServiceProvider"

  namespace App\Policies;
  
  use Photo\Policies\PhotoPolicy as Policy;

   class PhotoPolicy extends Policy
   {
       /**
        * @param \App\Models\User $user
        *
        * @return bool
        */
       public function viewAny($user): bool
       {
           return $user->isTeacher();
       }
   }

$table->foreign('photo_id')->references('id')->on('photo_photos')->onDelete('set null');

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function photo()
    {
        return $this->belongsTo(\Photo\Models\Photo::class, 'photo_id');
    }

    /**
    * @var \Photo\Repositories\PhotoRepository
    */
    protected $photoRepository;
            
    public function __construct(PhotoRepository $photoRepository)
    {
        $this->photoRepository = $photoRepository;
    }
    
    public function store(StoreRequest $request)
    {
    //Your other code.
        $post->photo_id = $this->photoRepository->create($file, ['caption' => $data['title']])->id;
    }

    namesapce App\Repositories;
    
    class PostRepository
    {
       /**
        * @var \Photo\Services\PhotoService
        */
        protected PhotoService $photoService;
        
        public function __construct(PhotoService $photoService)
        {
            $this->photoService = $photoService;
        }
        
        public function store(Request $request)
        {
            $post = new Post();
            $post->fill($request->all());
            
            $mainImageFolder = "posts"
            $thumbnailWidth = 220;
            $thumbnailHheight= 200;
            $crop="no"; // "yes" will resize image automatically based on your maximum height,width.
            $thumbnailPath = "thumbnails"; // Thumbnails path are relative to main Image folder. 
                                            //In this case it will create a folder thumbnails under posts folder.
                        
            $post->image =  $this->photoService
                                    ->setDimension($thumbnailWidth, $thumbnailHheight, $thumbnailPath)
                                    ->store($mainImageFolder, $request->file('file'), $post->title, $crop)
                                    ->convert()
                                    ->getStoredImagePath();
            $post->save();
                         
        }
             
    }