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导出带表情的字符报错


下一篇:thinkphp封装短信宝验证码