浏览代码

Feat(Image): scale

viest 6 年之前
父节点
当前提交
e4983c8262
共有 5 个文件被更改,包括 71 次插入5 次删除
  1. 8 2
      kernel/excel.c
  2. 1 1
      kernel/include.h
  3. 4 2
      kernel/write.c
  4. 29 0
      tests/image_no_styles.phpt
  5. 29 0
      tests/image_width_height_styles.phpt

+ 8 - 2
kernel/excel.c

@@ -90,6 +90,8 @@ ZEND_BEGIN_ARG_INFO_EX(xls_insert_image_arginfo, 0, 0, 3)
                 ZEND_ARG_INFO(0, row)
                 ZEND_ARG_INFO(0, column)
                 ZEND_ARG_INFO(0, image)
+                ZEND_ARG_INFO(0, width)
+                ZEND_ARG_INFO(0, height)
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO_EX(xls_insert_formula_arginfo, 0, 0, 3)
@@ -362,18 +364,22 @@ PHP_METHOD(vtiful_xls, insertImage)
 {
     zval *image;
     zend_long row, column;
+    double width = 1, height = 1;
 
-    ZEND_PARSE_PARAMETERS_START(3, 3)
+    ZEND_PARSE_PARAMETERS_START(3, 5)
             Z_PARAM_LONG(row)
             Z_PARAM_LONG(column)
             Z_PARAM_ZVAL(image)
+            Z_PARAM_OPTIONAL
+            Z_PARAM_DOUBLE(width)
+            Z_PARAM_DOUBLE(height)
     ZEND_PARSE_PARAMETERS_END();
 
     ZVAL_COPY(return_value, getThis());
 
     xls_object *obj = Z_XLS_P(getThis());
 
-    image_writer(image, row, column, &obj->ptr);
+    image_writer(image, row, column, width, height,  &obj->ptr);
 }
 /* }}} */
 

+ 1 - 1
kernel/include.h

@@ -97,7 +97,7 @@ STATIC void _populate_range(lxw_workbook *self, lxw_series_range *range);
 STATIC void _populate_range_dimensions(lxw_workbook *self, lxw_series_range *range);
 
 void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res, zend_string *format);
-void image_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res);
+void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_t *res);
 void formula_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res);
 void auto_filter(zend_string *range, xls_resource_t *res);
 void merge_cells(zend_string *range, zend_string *value, xls_resource_t *res);

+ 4 - 2
kernel/write.c

@@ -47,9 +47,11 @@ void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *
 /*
  * Write the image to the file
  */
-void image_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res)
+void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_t *res)
 {
-    worksheet_insert_image(res->worksheet, row, columns, ZSTR_VAL(zval_get_string(value)));
+    lxw_image_options options = {.x_scale = width, .y_scale = height};
+
+    worksheet_insert_image_opt(res->worksheet, row, columns, ZSTR_VAL(zval_get_string(value)), &options);
 }
 
 /*

+ 29 - 0
tests/image_no_styles.phpt

@@ -0,0 +1,29 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
+--FILE--
+<?php
+$config = [
+    'path' => './tests'
+];
+
+$fileObject = new \Vtiful\Kernel\Excel($config);
+$fileObject = $fileObject->fileName('tutorial.xlsx');
+
+$filePath = $fileObject->header(['name', 'age'])
+    ->data([
+        ['viest', 21],
+        ['wjx',   21]
+    ])
+    ->insertImage(3, 0, __DIR__ . '/../resource/pecl.png')
+    ->output();
+
+var_dump($filePath);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/tutorial.xlsx');
+?>
+--EXPECT--
+string(21) "./tests/tutorial.xlsx"

+ 29 - 0
tests/image_width_height_styles.phpt

@@ -0,0 +1,29 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
+--FILE--
+<?php
+$config = [
+    'path' => './tests'
+];
+
+$fileObject = new \Vtiful\Kernel\Excel($config);
+$fileObject = $fileObject->fileName('tutorial.xlsx');
+
+$filePath = $fileObject->header(['name', 'age'])
+    ->data([
+        ['viest', 21],
+        ['wjx',   21]
+    ])
+    ->insertImage(3, 0, __DIR__ . '/../resource/pecl.png', 10, 20)
+    ->output();
+
+var_dump($filePath);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/tutorial.xlsx');
+?>
+--EXPECT--
+string(21) "./tests/tutorial.xlsx"