Site icon Hip-Hop Website Design and Development

Cropping, Re-sizing and Saving images from one folder to another folder in wordpress

I’m adding an upgrade for the WordPress plugin which I have developed ages ago. This plugin is just a product catalog, so just show products and their images. Products can have more than one images.

I was re-sizing images in the older version of the plugin by CSS, assigning them width and height. Which was working but images look stretched but users were happy. Now I have added a new feature in the plugin which is to crop and re-size from the uploaded image and save it with a different name, like a thumbnail.jpg.
The new feature is working remarkably for the new users who upload images, but the thing is about old users who upgraded to newer version.

The issue is old users already has products and images. When I try to get all the products and images via foreach loop, it works perfectly on 200 – 250 images but breaks on more than 250+ images – No Error 🙁

Many of my old users has 600+ images so I want a way to crop and re-size the existing images and saves them with a new name and save the file names in the DB.

I’m using wordpress’s default wp_get_image_editor(); function.

Here’s my query to get old products which has images:

$wpc_product_images_sql = "Select wpc_posts.*, wpc_meta.* From " . $wpdb->posts . " As wpc_posts Inner Join " . $wpdb->postmeta . " As wpc_meta On wpc_posts.ID = wpc_meta.post_id Where wpc_meta.meta_key = 'product_images' Order By wpc_posts.post_title;

And here’s my foreach loops (I’m using two loops. First one is getting the products which has images and the second loop is for get images from each post, as I’m mentioned earlier in my question that products can have more than one images. So have to use two loops)

foreach ($wpc_images_qry as $wpc_prod_images) {

                echo '<div class="wpc_image_body">'
                . '<h3>' . $wpc_prod_images->post_title . '</h3>'
                . '<div class="wpc_images">';

                $wpc_post_id = $wpc_prod_images->ID;
                $wpc_product_images = get_post_meta($wpc_post_id, 'product_images', true);

                $big_img_name = array();
                foreach ($wpc_product_images as $wpc_prod_img) {
                    /// For Big
                    $big_resize_img = wp_get_image_editor($wpc_prod_img['product_img']);
                    if (!is_wp_error($big_resize_img)) {
                        $product_big_img = $wpc_prod_img['product_img'];

                        $product_img_explode = explode('/', $product_big_img);
                        $product_img_name = end($product_img_explode);
                        $product_img_name_explode = explode('.', $product_img_name);

                        $product_img_name = $product_img_name_explode[0];
                        $product_img_ext = $product_img_name_explode[1];

                        $big_crop = array('center', 'center');
                        $big_resize_img->resize($wpc_image_width, $wpc_image_height, $big_crop);
                        $big_filename = $big_resize_img->generate_filename('big-' . $wpc_image_width . 'x' . $wpc_image_height, $upload_dir['path'], NULL);
                        $big_resize_img->save($big_filename);

                        $big_img_name[]['wpc_big_img'] = $upload_dir['url'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext;


                        if (file_exists($upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext)) {

                            echo $upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext . ' - <strong style="color: #7ad03a;">OK</strong><br>';

                        } else {
                            echo $upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext . ' <strong style="color: red">:(</strong><br>';
                        }
                    }

                }
                update_post_meta($wpc_post_id, 'wpc_big_images', $big_img_name);

                echo '</div>'
            . '</div>';
            }