0

I have a console script which imports woocommerce products and creates images and its thumbnails for products.

The problem is that thumbnails are cropped. And I need to compress all generated thumbnails to keep original image.

Function to put an image to DB and server:

private function generateFeaturedImage($imageUrl, $postId, $isCategory = false)
    {

        $imageUrl   = $this->escapefileUrl($imageUrl);
        $imageUrl   = str_replace("%26", "&", $imageUrl);
        $uploadDir  = wp_upload_dir();
        $filename   = str_replace("%20", "-", basename($imageUrl));


        if(wp_mkdir_p($uploadDir['path'])) {
            $file = $uploadDir['path'] . '/' . $filename;
        } else {
            $file = $uploadDir['basedir'] . '/' . $filename;
        }
        file_put_contents($file, file_get_contents($imageUrl));

        $fileType = wp_check_filetype($filename, null );
        $attachment = array(
            'post_mime_type' => $fileType['type'],
            'post_title' => sanitize_file_name($filename),
            'post_content' => '',
            'post_status' => 'inherit'
        );
        $attachId = wp_insert_attachment( $attachment, $file, $postId );
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attachData = wp_generate_attachment_metadata( $attachId, $file );
        wp_update_attachment_metadata( $attachId, $attachData );

        set_post_thumbnail( $postId, $attachId );

    }
0

1) check your options_table for *_crop values. If we talking about standard wp thumbnails search for thumbnail_crop. Delete or change the value to 0 when find it.

2) Test.

  • Unfortunately it's not working, it's still cropping the images – Zhi V Apr 24 '18 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.