duanguoping2016 2015-07-09 08:41
浏览 124

在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>';
            }
  • 写回答

2条回答 默认 最新

  • drebew5059 2015-07-12 08:36
    关注

    You might try adding the new image size with add_image_size() and then run https://wordpress.org/plugins/regenerate-thumbnails/ which surely has a solution to the timeout problem. There is a way to display a message for your plugin users upon update, ask them to install and run the plugin if they have old images.

    Hope that works for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。