Viewing 6 replies - 1 through 6 (of 6 total)
  • Just a quick work-around that I’m using for the moment:

    function resize_image_uploads($data) {
        $field_id = $data->field_id;
        $field = new BP_XProfile_Field($field_id);
    
        if ($field->name === "Cover image") {
            $upload_dir = wp_upload_dir();
            $image_path = $upload_dir['basedir'] . $data->value;
    
            if ($size = getimagesize($image_path)) {
                $image = new Imagick($image_path);
                $image->resizeImage(400, 200, Imagick::FILTER_LANCZOS, 1);
                $image->writeImage();
                $image->destroy();
            }
        }
    }
    
    add_action('xprofile_data_before_save', __NAMESPACE__ . '\\resize_image_uploads', 15);

    I keep getting

    Fatal error: Class ‘Imagick’

    Can you tell me what I’m doing wrong?

    You probably don’t have ImageMagick installed on your server or the PHP module enabled.

    This link shows you how to install ImageMagick.
    https://php.net/manual/en/imagick.setup.php

    If you’re not able to install it, because you don’t have permission, you might be able to use PHP GD to process the thumbnails.
    https://php.net/manual/en/book.image.php

    I’ve actually updated the code since my previous post, as it had some errors:

    /**
     * Resize xprofile image uploads before saving them to the filesystem
     */
    function resize_image_uploads($data) {
        $field_id = $data->field_id;
        $field = new BP_XProfile_Field($field_id);
    
        if ($field->name === "Cover image" && !empty($data->value)) {
            $upload_dir = wp_upload_dir();
            $image_path = $upload_dir['basedir'] . $data->value;
            $image = new Imagick(realpath($image_path));
    
            if ($image->valid()) {
    
                $image->resizeImage(400, 210, Imagick::FILTER_LANCZOS, 1);
                $image->writeImage();
                $image->destroy();
            }
        }
    }

    Thanks! Now the error is gone but the images are not getting resized. Any thoughts to that, James?

    You might need to update the $field->name, mine is “Cover image”, but this needs to be the field you’re trying to resize. Is the $image->valid if statement returning true?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Hook for resizing image uploads’ is closed to new replies.