裁剪,调整大小和保存图像从一个文件夹到另一个文件夹在wordpress


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

我正在为我多年前开发的WordPress插件添加升级。这个插件只是一个产品目录,所以只是显示产品和他们的图像。产品可以有多个图像。

我通过CSS重新调整旧版本插件中的图像大小,分配它们的宽度和高度。这是工作,但图像看起来拉伸,但用户很高兴。现在我在插件中添加了一个新功能,即从上传的图像中裁剪和重新调整大小,并以不同的名称保存,如thumbnail.jpg。这个新功能对于上传图片的新用户来说非常有效,但是对于那些升级到新版本的老用户来说,情况就不一样了。

问题是老用户已经有了产品和图像。当我尝试通过foreach循环获取所有产品和图像时,它在200 - 250张图像上完美工作,但在超过250张图像上中断-没有错误:

我的许多老用户有600多张图片,所以我想要一种方法来裁剪和重新调整现有图片的大小,并以新的名称保存它们,并将文件名保存在数据库中。

我使用wordpress默认的wp_get_image_editor();功能。

这是我查询旧产品的图片:

$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;

这是我的foreach循环(我使用两个循环。第一个循环是获取有图片的产品,第二个循环是获取每个帖子的图片,正如我在前面的问题中提到的,产品可以有多个图片。所以必须使用两个循环)

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>';
            }

您可以尝试添加新的图像大小add_image_size ()然后运行https://wordpress.org/plugins/regenerate-thumbnails/这当然有一个解决超时问题的办法。有一种方法可以在插件用户更新时显示一条消息,如果他们有旧图像,请他们安装并运行插件。

如果你想调整图片的大小,那就按照这个来,当你在媒体中上传图片时,它会调整你的图片的大小,

add_filter('wp_handle_upload_prefilter','aj_handle_upload');
function aj_handle_upload($file)
{
    add_image_size( 'mobile', 360, 0, array( 'center', 'center' ));//mobile is userdefined name.    
    add_image_size( 'desktop', 700, 0, array( 'center', 'center' ));//desktop & tablet is user defined name.    
    return $file;
}