Mari G Mari G - 4 months ago 81
PHP Question

Rotate, Crop image using Guillotine plugin and Laravel 5 Intervention

I work on image editor module, using guillotine plugin.

I get parameters from ajax.

{angle: 0,h: 600,scale: 6.7811,w: 800,x: 0,y: 485}


In laravel I have this code

$img = \Input::get('img');
$data = \Input::get('data');

$image = \Image::make($img)->rotate((int)$data['angle']);

// crop image
$image->crop((int)$data['w'], (int)$data['h'], (int)$data['x'], (int)$data['y']);
$image->save('uploads/tmp/img.png');


The code is working , but the result is not the same as the user selected area.
I guess I need to use 'scale' attribute too, but I don't know how.

For example : user selected area

enter image description here

Result

enter image description here

I appreciate your help! :)

Answer

you need to use the scale factor then multiply it by the image width by using the widen() function, it:

Resizes the current image to new width, constraining aspect ratio

so the height will also get scale

    $img = \Input::get('img');
$data = \Input::get('data');

list($type, $img) = explode(';', $img);
list(, $img)      = explode(',', $img);
$img = base64_decode($img);

// save image
$img = file_put_contents('uploads/tmp/img.png', $img); 
$image = \Image::make('uploads/tmp/img.png')->rotate((float)$data['angle']);

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


// crop image
$image->crop((int)$data['w'], (int)$data['h'], (int)$data['x'], (int)$data['y']);
$image->save('uploads/tmp/img.png');