This snippet uploads all images embedded in the import file to the WordPress Media Library. Hat tip to https://stackoverflow.com/a/10910199 for the code inspiration.
add_filter( 'wp_all_import_phpexcel_object', 'wpai_wp_all_import_phpexcel_object', 10, 2 );
function wpai_wp_all_import_phpexcel_object( $objPHPExcel, $xlsFilePath ) {
$i = 0;
foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing) {
if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
ob_end_clean();
switch ($drawing->getMimeType()) {
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG :
$extension = 'png'; break;
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_GIF:
$extension = 'gif'; break;
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG :
$extension = 'jpg'; break;
}
} else {
$zipReader = fopen($drawing->getPath(),'r');
$imageContents = '';
while (!feof($zipReader)) {
$imageContents .= fread($zipReader,1024);
}
fclose($zipReader);
$extension = $drawing->getExtension();
}
$uploads = wp_upload_dir();
$myFileName = $uploads['basedir'] . '/00_Image_'.++$i.'.'.$extension;
file_put_contents($myFileName,$imageContents);
$url = $uploads['baseurl'] . '/' . basename( $myFileName );
$upload_to_medalibrary = PMXI_API::upload_image( 0, $url, 'yes', false, true );
unlink ( $myFileName );
}
return $objPHPExcel;
}