Hook for custom crop in Wordpress

4

I have a function that detects the face position of the person in an image and returns the X, Y, W, H or it can return the cut image of the face.

I need to display a photo of a person and I'm currently doing:

<?= wp_get_attachment_image( 1497, 'custom-small-thumb' ) ?>

Right, it displays the photo using the classic Wordpress resize / crop function.

BUT what I REALLY NEED is to display only the face that appears in this image, and I did not want to do any gambiarra, I would use the maximum resources of Wordpress for this purpose.

I have a function faceDetector($imagesrc) and I would like to know if you have any hook that I can do to run faceDetector before wp_get_attachment_image in> .

In fact I'd like a lot of something like

<?= wp_get_attachment_image ( 1497, 'face-small-thumb' ) ?>

But WP does not let us pass a custom function on add_image_size .

    
asked by anonymous 27.07.2016 / 18:19

1 answer

1

I see two possibilities, one is the filter wp_get_attachment_image_src and the other wp_get_attachment_image_attributes . The first one is applied as soon as the image references are fetched in the database (that is, in all image calls), the second just before the end of the markup that will be returned by wp_get_attachment_image() .

In both the first parameter is an array where one of the elements is src (index 0 or 'src') that can be manipulated and returned in a new array.

Example:

<?php 
    add_filter( 'wp_get_attachment_image_src', 'detectFace' );

    function detectFace( $image ) {
        if ( is_array( $image ) ) { // $image pode ser false em caso de erro
            $novo_src = faceDetector( $image[0] ); // $image[0] é o src
            $image[0] = $novo_src;
        }

        return $image;
    }

ref: link

    
19.10.2016 / 14:55