PHP code example of artesaos / attacher

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

    

artesaos / attacher example snippets



// file START ommited
    'providers' => [
        // other providers ommited
        \Artesaos\Attacher\Providers\AttacherServiceProvider::class,
    ],
// file END ommited


# config/app.php

// file START ommited
    'aliases' => [
        // other Facades ommited
        'Attacher'   => \Artesaos\Attacher\Facades\Attacher::class,
    ],
// file END ommited

Attacher::process(Model $model);
Attacher::getPath();
Attacher::setPath($path);
Attacher::setBaseURL($url);
Attacher::getProcessor();
Attacher::getInterpolator();

return [
    'model'    => 'Artesaos\Attacher\AttacherModel', # You can customize the model for your needs.
    'base_url' => '', # The url basis for the representation of images.
    'path'     => '/uploads/images/:id/:style/:filename', # Change the path where the images are stored.

    'style_guides'   => [
        'default' => [
            # If you set the original style all other styles used his return to base
            'original'=> function($image)
            {
                return $image->insert('public/watermark.png');
            },

            # Generate thumb (?x500)
            'thumb' => function ($image) {
                $image->resize(null, 500, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });

                return $image;
            },
        ],
        'my_custom_style' => [
            # Generate thumb (460x120)
            'cover' => function ($image) {
                $image->fit(460, 120);

                return $image;
            }
        ],
    ]
];


$upload = Input::file('image');

$image = new \Artesaos\Attacher\AttacherModel();
$image->setupFile($upload); # attach image
$image->save(); # now attacher process file (generate styles and save in your provider configured in flysystem)

echo $image->url('original');
echo $image->url('thumb'); // your style

$upload = Input::file('image');

$image = new \Artesaos\Attacher\AttacherModel();
$image->setupFile($upload, 'custom_style'); # attach image using the "custom_style"
$image->save();

echo $image->url('cover'); // The "cover" setted in "my_custom_style" of the config/attacher.php file

$upload = Input::file('image');

$image = new \Artesaos\Attacher\AttacherModel();
$image->setupFile($upload, [
    'my_custom_style' => [
        # Generate thumb (30x30)
        'cover' => function ($image) {
            $image->fit(30, 30);

            return $image;
        }
    ]
]); # attach image using the "my_custom_style" changed by Closure
$image->save();

echo $image->url('cover'); // Now, the "cover" generates a resized image of 30 by 30 pixels

$upload = Input::file('image');

$image = new \Artesaos\Attacher\AttacherModel();
$image->setupFile($upload, [
    'my_custom_style.cover' => function ($image) {
        $image->fit(30, 30);

        return $image;
    }
]); # attach image using the "my_custom_style" changed by Closure
$image->save();

echo $image->url('cover'); // Now, the "cover" generates a resized image of 30 by 30 pixels

#app/Project.php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Artesaos\Attacher\Traits\HasImage;

class Projects extends Model
{
    use HasImages;

    protected $table = 'projects';
}

////

$upload = Input::file('image');

$project = Projects::find(73);

$image = $project->addImage($upload); # Create a new image, save model and save image file with your styles

echo $image->url('thumbnail');

////

$project = Projects::find(73);

# Collection of images
$images = $project->images;


$model->addImage(UploadedFile $image, $styleGuide = null, $type = null);


#app/People.php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Artesaos\Attacher\Traits\HasImage;

class People extends Model
{
    use HasImage;

    protected $table = 'people';
}

////

$upload = Input::file('image');

$people = People::find(73);

$image = $people->addImage($upload); # Create a new image, save model and save image file with your styles

echo $image->url('thumbnail');

////

$people = People::find(73);

echo $people->image->url('original');


$model->addImage(UploadedFile $image, $styleGuide = null, $type = null);

$people = People::find(73);

$upload = Input::file('image');
$people->addImage($upload, 'default', 'listing'); # attach image using the "listing" custom guide style

$upload2 = Input::file('image2');
$people->addImage($upload2, 'default', 'gallery'); # attach image using the "gallery" custom guide style

$listingImages = $people->images->ofType('listing'); // Get images of the listing
$galleryImages = $people->images->ofType('gallery'); // Get images of the gallery