Jonjie
1 month ago
312
7
Laravel

How to add scale in Laravel Image Intervention package?

Posted 1 month ago by Jonjie

I'm doing a crop functionality using Laravel Image Intervention and jQuery Guillotine but it crops the different part of the image. Please see the code and screenshot below.

Controller

$data = explode('|', $image_details);

$data = [
            'angle' => $data[0],
            'h' => $data[1],
            'scale' => $data[2],
            'w' => $data[3],
            'x' => $data[4],
            'y' => $data[5]
        ];

        // Create image instance from source
        $image = Image::make($request_file);

        // we get the image width then multiply it by the scale factor, it will also scale the height automatically
        $image->widen(intval(floatval($image->width()) * floatval($data['scale'])));

        // File details
        $extension = explode('/', mime_content_type($request_file))[1];
        $name = md5(uniqid()) . time() . '.' . $extension; // as6d57a9sd7a5sd67856a9s.jpg

        // Temp details
        $key = md5(uniqid());
        $tmp_file_name = "{$key}.{$extension}"; // et7e98r7t9e79rt9e098g9e0.jpg
        $tmp_file_path = base_path() . "/" . $server_path . '/'. $tmp_file_name; // Server path

        // Image Cropper
        $image->crop(
        	$data['w'],
        	$data['h'],
        	$data['x'],
        	$data['y']
        );
        
        // Upload image to filesystem
        $image->save($tmp_file_path);

Guillotine

picture.on('guillotinechange', function(e, data, action){
                showData(data)
                $('#imageDetails').val(data.angle + '|' + data.h + '|' + data.scale + '|' + data.w + '|' + data.x + '|' + data.y);
              })

Screenshot

[Before Upload] enter image description here

[After Upload] enter image description here

Please sign in or create an account to participate in this conversation.