PHPExcel导出图片到单元格

2021/4/16 20:25:33

本文主要是介绍PHPExcel导出图片到单元格,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

$objDrawing = new \PHPExcel_Worksheet_MemoryDrawing();
$value['sku_picture'] = $this->url . $value['sku_picture'];
$img_info = getimagesize($value['sku_picture']);
$suffix_info = explode('/', $img_info['mime']);
$extname = $suffix_info[1];
# 图片写入
$objDrawImageResource = '';
if ($extname == 'jpg' || $extname == 'jpeg' || $extname == 'JPG' || $extname == 'JPEG') {
    $objDrawImageResource = @imagecreatefromjpeg($value['sku_picture']);
} else if ($extname == 'png' || $extname == 'PNG') {
    $objDrawImageResource = @imagecreatefrompng($value['sku_picture']);
} else if ($extname == 'gif' || $extname == 'GIF') {
    $objDrawImageResource = @imagecreatefromgif($value['sku_picture']);
} else {
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D' . $num, $extname . '格式图片无法导出');
}
if ($objDrawImageResource && ($objDrawImageResource != $extname . '格式图片无法导出')) {
    $objDrawing->setImageResource($objDrawImageResource);
    $objDrawing->setRenderingFunction(\PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT); //渲染方法
    $objDrawing->setMimeType(\PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
    // 设置宽度高度
    $objDrawing->setHeight(150); //照片高度
    $objDrawing->setWidth(145); //照片宽度
    // 设置图片要插入的单元格
    $objDrawing->setCoordinates('D' . $num);
    // 图片偏移距离
    $objDrawing->setOffsetX(15);
    $objDrawing->setOffsetY(15);
    $objDrawing->setWorksheet($objPHPExcel->getActiveSheet(0));
    $objPHPExcel->getActiveSheet()->getRowDimension($num)->setRowHeight(160);
}

 



这篇关于PHPExcel导出图片到单元格的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程