编程语言
首页 > 编程语言> > PHPExcel导出图片到单元格

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);
}

 

标签:objDrawImageResource,sku,picture,单元格,导出,objDrawing,value,extname,PHPExcel
来源: https://www.cnblogs.com/kaka0318/p/14668528.html