[英]Uploading two images, re-sizing, and saving to database with CodeIgniter
[英]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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.