public function cropPhoto(Photo $photo)
    {
        $photoable = input('photoable_type')::find(input('photoable_id'));

        // if relationship not found
        if (!$photoable) {
            return json_response_error('Whoops', 'We could not find the photoable.');
        }

        // get the large and thumb sizes
        if (isset($photoable::$LARGE_SIZE)) {
            $largeSize = $photoable::$LARGE_SIZE;
            $thumbSize = $photoable::$THUMB_SIZE;
        }
        else {
            $largeSize = $this->LARGE_SIZE;
            $thumbSize = $this->THUMB_SIZE;
        }

        // open file image resource
        $path = upload_path('photos');
        $originalImage = Image::make($photo->original_url);

        // get the crop data
        $x = intval(input('x'));
        $y = intval(input('y'));
        $width = intval(input('width'));
        $height = intval(input('height'));

        // generate new name (bypass cache)
        $photo->update([
            'filename' => token() . "{$photo->extension}"
        ]);

        // save original image with new name
        $originalImage->save($path . $photo->original_filename);

        // crop image on cropped area
        $imageTmp = $originalImage->crop($width, $height, $x, $y);

        // resize the image to large size
        $imageTmp->resize($largeSize[0], null, function ($constraint) {
            $constraint->aspectRatio();
        })->save($path . $photo->filename);

        // resize the image to thumb size
        $imageTmp->resize($thumbSize[0], null, function ($constraint) {
            $constraint->aspectRatio();
        })->save($path . $photo->thumb);

        return json_response('success');
    }
public function cropPhoto(Photo $photo)
    {
        $photoable = input('photoable_type')::find(input('photoable_id'));

        // if relationship not found
        if (!$photoable) {
            return json_response_error('Whoops', 'We could not find the photoable.');
        }

        // get the large and thumb sizes
        if (isset($photoable::$LARGE_SIZE)) {
            $largeSize = $photoable::$LARGE_SIZE;
            $thumbSize = $photoable::$THUMB_SIZE;
        }
        else {
            $largeSize = $this->LARGE_SIZE;
            $thumbSize = $this->THUMB_SIZE;
        }

        // open file image resource
        $path = upload_path('photos');
        $originalImage = Image::make($photo->original_url);

        // get the crop data
        $x = intval(input('x'));
        $y = intval(input('y'));
        $width = intval(input('width'));
        $height = intval(input('height'));

        // generate new name (bypass cache)
        $photo->update([
            'filename' => token() . "{$photo->extension}"
        ]);

        // save original image with new name
        $originalImage->save($path . $photo->original_filename);

        // crop image on cropped area
        $imageTmp = $originalImage->crop($width, $height, $x, $y);

        // resize the image to large size
        $imageTmp->resize($largeSize[0], null, function ($constraint) {
            $constraint->aspectRatio();
        })->save($path . $photo->filename);

        // resize the image to thumb size
        $imageTmp->resize($thumbSize[0], null, function ($constraint) {
            $constraint->aspectRatio();
        })->save($path . $photo->thumb);

        return json_response('success');
    }
public function cropPhoto ( Photo $ photo ) { $ photoable = input ( 'photoable_type' ) :: find ( input ( 'photoable_id' ) ) ; // if relationship not found if ( ! $ photoable ) { return json_response_error ( 'Whoops' , 'We could not find the photoable.' ) ; } // get the large and thumb sizes if ( isset ( $ photoable :: $ LARGE_SIZE ) ) { $ largeSize = $ photoable :: $ LARGE_SIZE ; $ thumbSize = $ photoable :: $ THUMB_SIZE ; } else { $ largeSize = $ this -> LARGE_SIZE ; $ thumbSize = $ this -> THUMB_SIZE ; } // open file image resource $ path = upload_path ( 'photos' ) ; $ originalImage = Image :: make ( $ photo -> original_url ) ; // get the crop data $ x = intval ( input ( 'x' ) ) ; $ y = intval ( input ( 'y' ) ) ; $ width = intval ( input ( 'width' ) ) ; $ height = intval ( input ( 'height' ) ) ; // generate new name (bypass cache) $ photo -> update ( [ 'filename' => token ( ) . "{$photo->extension}" ] ) ; // save original image with new name $ originalImage -> save ( $ path . $ photo -> original_filename ) ; // crop image on cropped area $ imageTmp = $ originalImage -> crop ( $ width , $ height , $ x , $ y ) ; // resize the image to large size $ imageTmp -> resize ( $ largeSize [ 0 ] , null , function ( $ constraint ) { $ constraint -> aspectRatio ( ) ; } ) -> save ( $ path . $ photo -> filename ) ; // resize the image to thumb size $ imageTmp -> resize ( $ thumbSize [ 0 ] , null , function ( $ constraint ) { $ constraint -> aspectRatio ( ) ; } ) -> save ( $ path . $ photo -> thumb ) ; return json_response ( 'success' ) ; }
Crop a photo
@param Photo $photo
@return \Illuminate\Http\JsonResponse
Crop a photo