PHP code example of okaybueno / images

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

    

okaybueno / images example snippets


    /**
     * Finds an Image via the given ID, and returns an Image instance or an array with the errors.
     *
     * @param int $imageId ID of the image to find.
     * @return mixed Image instance if success, array otherwise.
     */
    public function findImageById( $imageId );
    
    /**
    * Creates a new Image in the system.
    *
    * @param mixed $imageDataOrImageURL Anything that can create an image on the Intervention's make() function.
    * @param array $options Array of options when creating the image. The options can be:
    *                  - path => Path where the image needs to be stored.
    *                  - sizes => Associative array of sizes that this image needs to be resized to.
    *                  - type => If the image belongs to a certain type, add it here. Useful to segregate.
    *                  - maintain_aspect_ratio => If set to true, it will respect the aspect ratio of the image. Default: TRUE.
    *                  - prevent_upsizing => Id set to true, the image won't be upsized (no quality loss). Default: TRUE.
    *                  - crop => You can crop the image instead of resize it. To do so, set this to true. Default: FALSE.
    *                  - extension => Extension you want to save the processed files with. Default: 'jpg',
    *                  - quality => Quality of the generated images after processing. Default: 90
    * @return mixed array or false in case of error, instance of the Image, in case of success.
    */
    public function createImage( $imageB64OrUploadedFile, array $options = [] );
    
    /**
     * Deletes (soft deletes) an image from the database
     *
     * @param int $imageId Id of the image to delete.
     * @param bool|true $skipValidation If this is set to true, the validation will be skipped and the function won't
     * check if the entity exists in the DB.
     * @return mixed true if success, array with error otherwise.
     */
    public function deleteImage( $imageId, $skipValidation = TRUE);
    
    /**
     * Processes an image stored in the local disk, and resizes it to the given sizes.
     *
     * @param mixed $imageIdOrImage ID of the image, or instance of the image to process.
     * @param array $sizes associative array with the sizes that the image needs to be resized to.
     * @return mixed Image instance.
     */
    public function processImage( $imageIdOrImage, array $sizes = [] );
    
    /**
     * Destroy an image (and its thumbs) from the disks and from the DB. It cannot be reverted.
     *
     * @param mixed $imageIdOrImage ID or instance of the image to destroy.
     * @return mixed true if success, array with errors otherwise.
     */
    public function destroyImage( $imageIdOrImage );



public function profile_picture()
{
    return $this->hasOne( \OkayBueno\Images\Models\Image::class, 'id', 'profile_picture_id' );
}



protected function createAndSaveProfilePictureForUser( $image, User $user )
{
    if ( $image )
    {
        $prefix = substr( $user->uuid , 0, 3);
        $options = [
            'sizes' => [
                'large' => '600x600',
                'medium' => '300x300',
                'small' => '100x100'
            ],
            'path' => sprintf( 'users/%s/%s/%s/', $prefix, $user->uuid, (string)date('dmY') ),
            'crop' => TRUE,
            'extension' => 'png'
        ];

        $imageResult = $this->imageService->createImage( $image , $options );

        if ( $imageResult instanceof Image )
        {
            $saveData = [
                'picture_id' => $imageResult->id
            ];

            // Remove other image from post.
            $userPictureId = $user->picture_id;
            if ( $userPictureId ) $this->imageService->deleteImage( $userPictureId, TRUE );

            $this->usersRepository->updateBy( $saveData, $user->id );

            $user = $this->usersRepository->findOneBy( $user->id );

            return $user;
        }

        return $imageResult;
    }

    return $user;
}


public function updateUser( User $user, array $newUserData )
{
    ... 
    
    ...
    
    if ( array_key_exists( 'profile_picture', $newUserData ) )
    {
        $user = $this->createAndSaveProfilePictureForUser( $newUserData['profile_picture'], $user );
        unset( $newUserData['profile_picture'] );
    }
    
    ... 
        
    ...
    
    return $user;
}


    protected $listen = [
        'OkayBueno\Images\Events\ImageWasCreated' => [
            'OkayBueno\Images\Listeners\ProcessImageSync'
        ],
        'OkayBueno\Images\Events\ImageWasProcessed' => [
            'OkayBueno\Images\Listeners\MoveProcessedImagesToCloudImageSync'
        ],
        'OkayBueno\Images\Events\ImageWasMovedToCloud' => [
            'OkayBueno\Images\Listeners\RemoveLocalImageSync'
        ]
    ];

    protected $listen = [
        'OkayBueno\Images\Events\ImageWasCreated' => [
            'OkayBueno\Images\Listeners\ProcessImageAsync'
        ]
    ];