大佬教程收集整理的这篇文章主要介绍了PHPExcel-遍历行-如果图像然后保存图像文件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我有一个导入,虽然通过一个excel文件循环并获取行并将其添加到数据库.
现在我要尝试的是查找该行的最后一个列中是否有图像.
并非所有行都有图像,这不是必需的.
我已尝试使用此处所述的解决方案Retrieving image in PHPExcel
但这会得到所有图像.如果不是每一行都有图像,这将是不好的.我不知道哪个图像属于哪一行.
任何帮助将不胜感激
解决方法:
我可以使用以下脚本进行操作.
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 @R_278_1@R_944_11226@6@l 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;
}
}
以上是大佬教程为你收集整理的PHPExcel-遍历行-如果图像然后保存图像文件全部内容,希望文章能够帮你解决PHPExcel-遍历行-如果图像然后保存图像文件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。