thetigerduck/cropfield

Imegeupload with JCrop functionallity in frontend

Installs: 13

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Language:JavaScript

Type:silverstripe-vendormodule

dev-main 2023-11-30 23:06 UTC

This package is auto-updated.

Last update: 2024-03-30 00:27:06 UTC


README

CropField creates a ImageUploadField, allowing to select a CropArea in Frontend using Jcrop This new Version works with SS5 and does not depend on unclecheese/dropzone anymore. But it forks part of unclecheese's code.

@author TheTigerDuck janosch@spleen.de

Usage

function ImageFrom(){
    $fields = new FieldList(
        $cf = new CropField("Image", "Image")
    );
    $cf->setBoxWidth(600);
    $cf->setBoxHeight(6000);
    $cf->setMaxWidth(0);
    $cf->setMaxHeight(0);
    $cf->setAspectRatio(1);
    $actions = new FieldList(
        new FormAction("submit", "submit")
    );
    return new Form($this, "ImageFrom", $fields, $actions);
}

function submit($data,$form){
    $original = Image::get()->byID($data['Image']['ID']);
    $cropped = $original->CroppedFromPos($data['Image']['width'], $data['Image']['height'], $data['Image']['posX'], $data['Image']['posY']);
    //overwrite the Original Image by the Cropped one
    $file = File::create();
    // Save file into backend
    $config = [
        'conflict' => AssetStore::CONFLICT_OVERWRITE,
        'visibility' => AssetStore::VISIBILITY_PUBLIC
    ];
    //genarate a unique filename to force regenerating image versions
    $hash = md5(rand().$profil->Email.$data['ProfilBild']['width'].$data['ProfilBild']['height'].$data['ProfilBild']['posX'].$data['ProfilBild']['posY'].$data['ProfilBild']['rotate']);
    $file->setFromLocalFile(Director::baseFolder() . "/public/" . $cropped->getSourceURL(), "Profilbilder/cropped/ProfileImage{$profil->ID}-{$hash}.".$cropped->getExtension(), null, null, $config);
    $file->write();
    $file->ClassName = Image::class;
    $file->write();
    $file->publishFile();
    $image = Image::get()->byID($file->ID);
    $image->publishSingle();
    $original->deleteFile();
    $original->doArchive();
    $profil->ImageID = $file->ID;
    $profil->write();
}