我正在尝试在我的服务器上生成缩略图,这些缩略图由插件上载。它将所有图像上载到一个目录。我试图将这些文件作为目标,然后根据img_resize用它们创建一个缩略图,但我没有任何运气。最终,我会将拇指插入媒体库和用户元,但我甚至还不能生成缩略图,所以这是手头的第一个任务!
以下是我迄今为止的简单代码:
require_once(ABSPATH . \'/wp-admin/includes/media.php\');
require_once(ABSPATH . \'/wp-admin/includes/image.php\');
$file = \'http://www.mysite.org/uploads/uploaddir/2012/02/bill.jpeg\';
$max_w = 200;
$max_h = 200;
$crop = true;
$filePathInfo = pathinfo($file);
$fileName = $filePathInfo[\'basename\'];
$wpUploadPath = wp_upload_dir();
$destpath = $wpUploadPath[\'basedir\'].\'/\'.$fileName;
image_resize( $file, $max_w, $max_h, $crop );
我试着用
$destpath
而且什么也没有发生。没有生成缩略图。我想得不对吗?
任何人如能对这种情况有所了解,都将不胜感激。
谢谢
Tre公司
解决注意:这是最终对我起作用的代码。我以前使用URI来定义image\\u resize。不是绝对目录。
$wpUploadDir = wp_upload_dir();
$baseDir = $wpUploadDir[\'basedir\'];
$fileName = \'bill.jpg\';
$filePath = $baseDir . "/" . $fileName;
$wp_filetype = wp_check_filetype($filePath, null);
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => $fileName,
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
$attach_id = wp_insert_attachment( $attachment, $filePath );
require_once(ABSPATH . \'wp-admin/includes/image.php\');
require_once(ABSPATH . \'wp-admin/includes/media.php\');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filePath );
wp_update_attachment_metadata( $attach_id, $attach_data );
image_resize($filePath, 200, 200, true, \'200x200\');
最合适的回答,由SO网友:Matthew Boynes 整理而成
查看您的代码,似乎可以在中查找该文件.../wp-content/uploads/bill.jpeg
. 您还需要包括yyyy/mm。我会用这个:
require_once(ABSPATH . \'/wp-admin/includes/media.php\');
require_once(ABSPATH . \'/wp-admin/includes/image.php\');
$file = \'http://www.mysite.org/uploads/uploaddir/2012/02/bill.jpeg\';
# Function reference
# image_resize ( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 )
$wpUploadPath = wp_upload_dir();
$fileName = preg_replace(\'/^.*?\\/(\\d{4})\\/(\\d\\d)\\/(.*)$/\', $wpUploadPath[\'basedir\'].\'/$1/$2/$3\', $file);
image_resize( $fileName, 200, 200, true, \'200x200\' );
试试看,让我知道你过得怎么样!