PHPExcel-遍历行-如果是图像,则保存图像文件
因此,我有一个导入,它循环遍历excel文件,获取行并将它们添加到数据库中 现在我要做的是找出那一行的最后一列中是否有图像 并非所有行都有图像,这不是必需的 我已经尝试使用这里所述的解决方案 但这得到了所有的图像。如果不是每一行都有一个图像,这是不好的。我不知道哪个图像属于哪一行PHPExcel-遍历行-如果是图像,则保存图像文件,php,image,phpexcel,Php,Image,Phpexcel,因此,我有一个导入,它循环遍历excel文件,获取行并将它们添加到数据库中 现在我要做的是找出那一行的最后一列中是否有图像 并非所有行都有图像,这不是必需的 我已经尝试使用这里所述的解决方案 但这得到了所有的图像。如果不是每一行都有一个图像,这是不好的。我不知道哪个图像属于哪一行 如果有任何帮助,我将不胜感激。我能够通过以下脚本实现这一点 if ($_FILES['file_source']['tmp_name']) { // Get our import file ex
如果有任何帮助,我将不胜感激。我能够通过以下脚本实现这一点
if ($_FILES['file_source']['tmp_name']) {
// Get our import file extension
${'Extension'} = strtolower(array_pop(explode('.', $_FILES['file_source']['name'])));
// Store our content array
${'Accounts'} = array();
if (${'Extension'} == 'xls' || ${'Extension'} == 'xlsx') {
// Create a new Excel instance
if (${'Extension'} == 'xlsx') {
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
} else {
$objReader = PHPExcel_IOFactory::createReader('Excel5');
}
$objReader->setReadDataOnly(false);
$objPHPExcel = $objReader->load($_FILES['file_source']['tmp_name']);
$objWorksheet = $objPHPExcel->getActiveSheet();
// Check for the columns and column titles
if($objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, 1)->getValue() != 'Post Title' || $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, 1)->getValue() != 'Content' || $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(2, 1)->getValue() != 'Link' || $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(3, 1)->getValue() != 'Link Title' || $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(4, 1)->getValue() != 'Link Description' || $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(5, 1)->getValue() != 'Image'){
echo json_encode(array('response'=>'fail', 'reason'=>'column_error'));
exit;
}
// Get the total number of rows in the spreadsheet
$rows = $objWorksheet->getHighestRow();
// Check that each row contains all the required data
$row_errors = 0;
$row = 1;
// skip the first row if it has our column names
for (((($objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, $row)->getValue()) == 'Post Title') ? $row = 2 : $row = 1); $row <= $rows; ++$row) {
// Sanitize all our & add them to the accounts array
if($objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, $row)->getValue() == '') { $row_errors++; }
if($objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, $row)->getValue() == '') { $row_errors++; }
}
if($row_errors>0){
echo json_encode(array('response'=>'fail', 'reason'=>'required_data'));
exit;
}
// Loop through all the rows (line items)
$row = 1;
${'Iterator'} = 0;
// skip the first row if it has our column names
for (((($objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, $row)->getValue()) == 'Post Title') ? $row = 2 : $row = 1); $row <= $rows; ++$row) {
// Sanitize all our & add them to the accounts array
${'Accounts'}[${'Iterator'}] = array('Post Title' => inputCleanSQL($objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, $row)->getValue()),
'Content' => inputCleanSQL($objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, $row)->getValue()),
'Link' => inputCleanSQL($objPHPExcel->getActiveSheet()->getCellByColumnAndRow(2, $row)->getValue()),
'Link Title' => inputCleanSQL($objPHPExcel->getActiveSheet()->getCellByColumnAndRow(3, $row)->getValue()),
'Link Description' => inputCleanSQL($objPHPExcel->getActiveSheet()->getCellByColumnAndRow(4, $row)->getValue())
);
// Check if there is a corresponding image with this row
foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing) {
if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
$cellID = $drawing->getCoordinates();
if($cellID == PHPExcel_Cell::stringFromColumnIndex(5).$row){
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
ob_end_clean();
$filetype = $drawing->getMimeType();
$filename = md5(microtime());
switch ($filetype) {
case 'image/gif':
$image = imagecreatefromstring($imageContents);
imagegif($image, "/var/www/social/uploads/i/$filename.gif", 100);
$new_file = "$filename.gif";
break;
case 'image/jpeg':
$image = imagecreatefromstring($imageContents);
imagejpeg($image, "/var/www/social/uploads/i/$filename.jpeg", 100);
$new_file = "$filename.jpeg";
break;
case 'image/png':
$image = imagecreatefromstring($imageContents);
imagepng($image, "/var/www/social/uploads/i/$filename.png", 100);
$new_file = "$filename.png";
break;
default:
continue 2;
}
// Add our image location to the array
${'Accounts'}[${'Iterator'}]['image'] = array('link'=>'http://IMAGECDN/'.$new_file, 'type'=>$filetype, 'name'=>$new_file, 'size'=>filesize('/'.$new_file));
}
}
}
// increase our interator
${'Iterator'}++;
}
// if not proper file type throw an error
} else {
echo json_encode(array('response'=>'fail', 'reason'=>'invalid_type'));
exit;
}
}
if($\u FILES['file\u source']['tmp\u name']){
//获取我们的导入文件扩展名
${'Extension'}=strtolower(数组_pop(分解('.',$文件['file\u source']['name']));
//存储我们的内容数组
${'Accounts'}=array();
如果(${Extension'}=='xls'|${'Extension'}=='xlsx'){
//创建新的Excel实例
如果(${'Extension'}=='xlsx'){
$objReader=PHPExcel_IOFactory::createReader('Excel2007');
}否则{
$objReader=PHPExcel_IOFactory::createReader('Excel5');
}
$objReader->setReadDataOnly(false);
$objPHPExcel=$objReader->load($_FILES['file\u source']['tmp\u name']);
$objWorksheet=$objPHPExcel->getActiveSheet();
//检查列和列标题
如果($objPHPExcel->getActiveSheet()->GetCellByColumnRow(0,1)->getValue()!='Post Title'|$objPHPExcel->getActiveSheet()->GetCellByColumnRow(1,1)->getValue()!='Content'| |$objPHPExcel->getActiveSheet()->GetCellByColumnRow(2,1)->getValue()!='Link'|$objPHPExcel->getActiveSheet()->GetCellByColumnRow(3,1)->getValue()!='Link Title'| |$objPHPExcel->getActiveSheet()->getCellByColumnRow(4,1)->getValue()!='Link Description'|$objPHPExcel->getActiveSheet()->getCellByColumnRow(5,1)->getValue()!='Image'){
echo json_编码(数组('response'=>'fail','reason'=>'column_error');
出口
}
//获取电子表格中的总行数
$rows=$objWorksheet->getHighestRow();
//检查每一行是否包含所有必需的数据
$row_错误=0;
$row=1;
//如果第一行有我们的列名,请跳过它
对于((($objPHPExcel->getActiveSheet()->GetCellByColumnRow(0,$row)->getValue())=='PostTitle')?$row=2:$row=1;$row getActiveSheet()->GetCellByColumnRow(0,$row)->getValue()=''{$row\u errors++}
如果($objPHPExcel->getActiveSheet()->GetCellByColumnRow(1,$row)->getValue()==''){$row\u errors++}
}
如果($row\u errors>0){
echo json_编码(数组('response'=>'fail','reason'=>'required_data');
出口
}
//循环浏览所有行(行项目)
$row=1;
${'Iterator'}=0;
//如果第一行有我们的列名,请跳过它
对于((($objPHPExcel->getActiveSheet()->GetCellByColumnRow(0,$row)->getValue())=='PostTitle')?$row=2:$row=1);$row inputCleanSQL($objPHPExcel->getActiveSheet()->GetCellByColumnRow(0,$row)->getValue()),
'Content'=>inputCleanSQL($objPHPExcel->getActiveSheet()->GetCellByColumnRow(1,$row)->getValue()),
'Link'=>inputCleanSQL($objPHPExcel->getActiveSheet()->GetCellByColumnRow(2,$row)->getValue()),
“链接标题”=>inputCleanSQL($objPHPExcel->getActiveSheet()->GetCellByColumnRow(3,$row)->getValue()),
“链接描述”=>inputCleanSQL($objPHPExcel->getActiveSheet()->GetCellByColumnRow(4,$row)->getValue())
);
//检查是否有与此行对应的图像
foreach($objPHPExcel->getActiveSheet()->getDrawingCollection()作为$drawing){
if($PHPExcel的绘图实例\工作表\内存绘图){
$cellID=$drawing->getCoordinates();
如果($cellID==PHPExcel_Cell::stringFromColumnIndex(5)。$row){
ob_start();
调用用户函数(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents=ob_get_contents();
ob_end_clean();
$filetype=$drawing->getMimeType();
$filename=md5(microtime());
开关($filetype){
案例“image/gif”:
$image=imagecreatefromstring($imageContents);
imagegif($image,“/var/www/social/uploads/i/$filename.gif”,100);
$new_file=“$filename.gif”;
打破
案例“图像/jpeg”:
$image=imagecreatefromstring($imageContents);
imagejpeg($image,“/var/www/social/uploads/i/$filename.jpeg”,100);
$new_file=“$filename.jpeg”;
打破
案例“image/png”:
$image=imagecreatefromstring($imageContents);
imagepng($image,“/var/www/social/uploads/i/$filename.png”,100);
$new_file=“$filename.png”;