getdave / wp-intervention

On-demand image manipulation for WordPress via the Intervention Library.
26 stars 7 forks source link

Adding a constraint to resize #6

Closed davidrichied closed 5 years ago

davidrichied commented 5 years ago

Hi, Thanks for the plugin! It works great. Is it possible to include a callback function to constrain the aspect ratio? I am trying to resize the image to a height of 400px while maintaining the aspect ratio for the width. Here's how it's done with plain Intervention: http://image.intervention.io/api/resize Thanks.

davidrichied commented 5 years ago

I found out how to do it. Here's what I did for anyone else looking for the solution. I just had to add a function as the third item in the $intervention_args array.

`$imageUrl = $image['url'];

$imagePath = wp_make_link_relative($imageUrl);

$imageAbsPath = ABSPATH . substr($imagePath, 1);

// An array of args to be invoked on Intervention $intervention_args = array( 'resize' => array( null, '400', function($constraint) { $constraint->aspectRatio(); } ),

); $thumbnail = wp_intervention( $imageAbsPath, $intervention_args ); `

getdave commented 5 years ago

Hi @davidrichied. Glad you found a solution. Can I ask was it clear enough in the README docs as to how to achieve your solution?

davidrichied commented 5 years ago

Hi @getdave , I think I got lucky by playing around with adding the function in different places.