PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/gd - gd_ctx.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 51 62 82.3 %
Date: 2014-04-18 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Stanislav Malyshev <stas@php.net>                           |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include "php_gd.h"
      22             : 
      23             : #define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
      24             : 
      25          27 : static void _php_image_output_putc(struct gdIOCtx *ctx, int c)
      26             : {
      27             :         /* without the following downcast, the write will fail
      28             :          * (i.e., will write a zero byte) for all
      29             :          * big endian architectures:
      30             :          */
      31          27 :         unsigned char ch = (unsigned char) c;
      32             :         TSRMLS_FETCH();
      33          27 :         php_write(&ch, 1 TSRMLS_CC);
      34          27 : }
      35             : 
      36         247 : static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l)
      37             : {
      38             :         TSRMLS_FETCH();
      39         247 :         return php_write((void *)buf, l TSRMLS_CC);
      40             : }
      41             : 
      42          28 : static void _php_image_output_ctxfree(struct gdIOCtx *ctx)
      43             : {
      44          28 :         if(ctx) {
      45          28 :                 efree(ctx);
      46             :         }
      47          28 : }
      48             : 
      49             : /* {{{ _php_image_output_ctx */
      50          81 : static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
      51             : {
      52             :         zval *imgind;
      53          81 :         char *file = NULL;
      54          81 :         int file_len = 0;
      55             :         long quality, basefilter;
      56             :         gdImagePtr im;
      57          81 :         FILE *fp = NULL;
      58          81 :         int argc = ZEND_NUM_ARGS();
      59          81 :         int q = -1, i;
      60          81 :         int f = -1;
      61             :         gdIOCtx *ctx;
      62             : 
      63             :         /* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp().
      64             :          * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
      65             :          * from imagey<type>().
      66             :          */
      67             : 
      68          81 :         if (image_type == PHP_GDIMG_TYPE_XBM) {
      69           0 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
      70           0 :                         return;
      71             :                 }
      72             :         } else {
      73             :                 /* PHP_GDIMG_TYPE_GIF
      74             :                  * PHP_GDIMG_TYPE_PNG 
      75             :                  * PHP_GDIMG_TYPE_JPG 
      76             :                  * PHP_GDIMG_TYPE_WBM */
      77          81 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|s!ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
      78           0 :                         return;
      79             :                 }
      80             :         }
      81             : 
      82          81 :         ZEND_FETCH_RESOURCE(im, gdImagePtr, &imgind, -1, "Image", phpi_get_le_gd());
      83             : 
      84          81 :         if (argc > 1) {
      85          70 :                 if (argc >= 3) {
      86          18 :                         q = quality; /* or colorindex for foreground of BW images (defaults to black) */
      87          18 :                         if (argc == 4) {
      88           0 :                                 f = basefilter;
      89             :                         }
      90             :                 }
      91             :         }
      92             : 
      93         134 :         if (argc > 1 && file_len) {
      94          53 :                 if (strlen(file) != file_len) {
      95           0 :                         RETURN_FALSE;
      96             :                 }
      97          53 :                 PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename");
      98             : 
      99          53 :                 fp = VCWD_FOPEN(file, "wb");
     100          53 :                 if (!fp) {
     101           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing: %s", file, strerror(errno));
     102           0 :                         RETURN_FALSE;
     103             :                 }
     104             : 
     105          53 :                 ctx = gdNewFileCtx(fp);
     106             :         } else {
     107          28 :                 ctx = emalloc(sizeof(gdIOCtx));
     108          28 :                 ctx->putC = _php_image_output_putc;
     109          28 :                 ctx->putBuf = _php_image_output_putbuf;
     110             : #if HAVE_LIBGD204
     111          28 :                 ctx->gd_free = _php_image_output_ctxfree;
     112             : #else
     113             :                 ctx->free = _php_image_output_ctxfree;
     114             : #endif
     115             : 
     116             : #if APACHE && defined(CHARSET_EBCDIC)
     117             :                 /* XXX this is unlikely to work any more thies@thieso.net */
     118             :                 /* This is a binary file already: avoid EBCDIC->ASCII conversion */
     119             :                 ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
     120             : #endif
     121             :         }
     122             : 
     123          81 :         switch(image_type) {
     124             :                 case PHP_GDIMG_CONVERT_WBM:
     125           0 :                         if(q<0||q>255) {
     126           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
     127             :                         }
     128             :                 case PHP_GDIMG_TYPE_JPG:
     129           8 :                         (*func_p)(im, ctx, q);
     130           8 :                         break;
     131             :                 case PHP_GDIMG_TYPE_PNG:
     132          61 :                         (*func_p)(im, ctx, q, f);
     133          61 :                         break;
     134             :                 case PHP_GDIMG_TYPE_XBM:
     135             :                 case PHP_GDIMG_TYPE_WBM:
     136           1 :                         if (argc < 3) {
     137           1 :                                 for(i=0; i < gdImageColorsTotal(im); i++) {
     138           0 :                                         if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
     139             :                                 }
     140           1 :                                 q = i;
     141             :                         }
     142           1 :                         if (image_type == PHP_GDIMG_TYPE_XBM) {
     143           0 :                                 (*func_p)(im, file, q, ctx);
     144             :                         } else {
     145           1 :                                 (*func_p)(im, q, ctx);
     146             :                         }
     147           1 :                         break;
     148             :                 default:
     149          11 :                         (*func_p)(im, ctx);
     150             :                         break;
     151             :         }
     152             : 
     153             : #if HAVE_LIBGD204
     154          81 :         ctx->gd_free(ctx);
     155             : #else
     156             :         ctx->free(ctx);
     157             : #endif
     158             : 
     159          81 :         if(fp) {
     160          53 :                 fflush(fp);
     161          53 :                 fclose(fp);
     162             :         }
     163             : 
     164          81 :     RETURN_TRUE;
     165             : }
     166             : /* }}} */
     167             : 
     168             : /*
     169             :  * Local variables:
     170             :  * tab-width: 4
     171             :  * c-basic-offset: 4
     172             :  * End:
     173             :  * vim600: sw=4 ts=4 fdm=marker
     174             :  * vim<600: sw=4 ts=4
     175             :  */

Generated by: LCOV version 1.10

Generated at Fri, 18 Apr 2014 07:01:27 +0000 (11 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.