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: 71 90 78.9 %
Date: 2015-03-20 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 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          27 :         php_write(&ch, 1);
      33          27 : } /* }}} */
      34             : 
      35         247 : static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
      36             : {
      37         247 :         return php_write((void *)buf, l);
      38             : } /* }}} */
      39             : 
      40          28 : static void _php_image_output_ctxfree(struct gdIOCtx *ctx) /* {{{ */
      41             : {
      42          28 :         if(ctx) {
      43          28 :                 efree(ctx);
      44             :         }
      45          28 : } /* }}} */
      46             : 
      47        3544 : static void _php_image_stream_putc(struct gdIOCtx *ctx, int c) /* {{{ */ {
      48        3544 :         char ch = (char) c;
      49        3544 :         php_stream * stream = (php_stream *)ctx->data;
      50        3544 :         php_stream_write(stream, &ch, 1);
      51        3544 : } /* }}} */
      52             : 
      53        1430 : static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
      54             : {
      55        1430 :         php_stream * stream = (php_stream *)ctx->data;
      56        1430 :         return php_stream_write(stream, (void *)buf, l);
      57             : } /* }}} */
      58             : 
      59          54 : static void _php_image_stream_ctxfree(struct gdIOCtx *ctx) /* {{{ */
      60             : {
      61             : 
      62          54 :         if(ctx->data) {
      63          54 :                 php_stream_close((php_stream *) ctx->data);
      64          54 :                 ctx->data = NULL;
      65             :         }
      66          54 :         if(ctx) {
      67          54 :                 efree(ctx);
      68             :         }
      69          54 : } /* }}} */
      70             : 
      71             : /* {{{ _php_image_output_ctx */
      72          86 : static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
      73             : {
      74             :         zval *imgind;
      75          86 :         char *file = NULL;
      76          86 :         size_t file_len = 0;
      77             :         zend_long quality, basefilter;
      78             :         gdImagePtr im;
      79          86 :         int argc = ZEND_NUM_ARGS();
      80          86 :         int q = -1, i;
      81          86 :         int f = -1;
      82          86 :         gdIOCtx *ctx = NULL;
      83          86 :         zval *to_zval = NULL;
      84             :         php_stream *stream;
      85             : 
      86             :         /* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp().
      87             :          * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
      88             :          * from imagey<type>().
      89             :          */
      90          86 :         if (image_type == PHP_GDIMG_TYPE_XBM) {
      91           0 :                 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rp!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
      92           0 :                         return;
      93             :                 }
      94             :         } else {
      95             :                 /* PHP_GDIMG_TYPE_GIF
      96             :                  * PHP_GDIMG_TYPE_PNG
      97             :                  * PHP_GDIMG_TYPE_JPG
      98             :                  * PHP_GDIMG_TYPE_WBM
      99             :                  * PHP_GDIMG_TYPE_WEBP
     100             :                  * */
     101          86 :                 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|z/!ll", &imgind, &to_zval, &quality, &basefilter) == FAILURE) {
     102           0 :                         return;
     103             :                 }
     104             :         }
     105             : 
     106          86 :         if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(imgind), "Image", phpi_get_le_gd())) == NULL) {
     107           0 :                 RETURN_FALSE;
     108             :         }
     109             : 
     110          86 :         if (argc >= 3) {
     111          18 :                 q = quality; /* or colorindex for foreground of BW images (defaults to black) */
     112          18 :                 if (argc == 4) {
     113           0 :                         f = basefilter;
     114             :                 }
     115             :         }
     116             : 
     117         140 :         if (argc > 1 && to_zval != NULL) {
     118         116 :                 if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
     119           0 :                         php_stream_from_zval_no_verify(stream, to_zval);
     120           0 :                         if (stream == NULL) {
     121           0 :                                 RETURN_FALSE;
     122             :                         }
     123         116 :                 } else if (Z_TYPE_P(to_zval) == IS_STRING) {
     124          58 :                         if (CHECK_ZVAL_NULL_PATH(to_zval)) {
     125           4 :                                 php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, filename must not contain null bytes");
     126           4 :                                 RETURN_FALSE;
     127             :                         }
     128             : 
     129          54 :                         stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
     130          54 :                         if (stream == NULL) {
     131           0 :                                 RETURN_FALSE;
     132             :                         }
     133             :                 } else {
     134           0 :                         php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream");
     135           0 :                         RETURN_FALSE;
     136             :                 }
     137             :         } else {
     138          28 :                 ctx = emalloc(sizeof(gdIOCtx));
     139          28 :                 ctx->putC = _php_image_output_putc;
     140          28 :                 ctx->putBuf = _php_image_output_putbuf;
     141          28 :                 ctx->gd_free = _php_image_output_ctxfree;
     142             : 
     143             : #if APACHE && defined(CHARSET_EBCDIC)
     144             :                 /* XXX this is unlikely to work any more thies@thieso.net */
     145             :                 /* This is a binary file already: avoid EBCDIC->ASCII conversion */
     146             :                 ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
     147             : #endif
     148             :         }
     149             : 
     150          82 :         if (!ctx)       {
     151          54 :                 ctx = emalloc(sizeof(gdIOCtx));
     152          54 :                 ctx->putC = _php_image_stream_putc;
     153          54 :                 ctx->putBuf = _php_image_stream_putbuf;
     154          54 :                 ctx->gd_free = _php_image_stream_ctxfree;
     155          54 :                 ctx->data = (void *)stream;
     156             :         }
     157             : 
     158          82 :         switch(image_type) {
     159             :                 case PHP_GDIMG_CONVERT_WBM:
     160           0 :                         if(q<0||q>255) {
     161           0 :                                 php_error_docref(NULL, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
     162             :                         }
     163             :                 case PHP_GDIMG_TYPE_JPG:
     164           8 :                         (*func_p)(im, ctx, q);
     165           8 :                         break;
     166             :                 case PHP_GDIMG_TYPE_WEBP:
     167           0 :                         if (q == -1) {
     168           0 :                                 q = 80;
     169             :                         }
     170           0 :                         (*func_p)(im, ctx, q);
     171           0 :                         break;
     172             :                 case PHP_GDIMG_TYPE_PNG:
     173          62 :                         (*func_p)(im, ctx, q, f);
     174          62 :                         break;
     175             :                 case PHP_GDIMG_TYPE_XBM:
     176             :                 case PHP_GDIMG_TYPE_WBM:
     177           1 :                         if (argc < 3) {
     178           1 :                                 for(i=0; i < gdImageColorsTotal(im); i++) {
     179           0 :                                         if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
     180             :                                 }
     181           1 :                                 q = i;
     182             :                         }
     183           1 :                         if (image_type == PHP_GDIMG_TYPE_XBM) {
     184           0 :                                 (*func_p)(im, file, q, ctx);
     185             :                         } else {
     186           1 :                                 (*func_p)(im, q, ctx);
     187             :                         }
     188           1 :                         break;
     189             :                 default:
     190          11 :                         (*func_p)(im, ctx);
     191             :                         break;
     192             :         }
     193             : 
     194          82 :         ctx->gd_free(ctx);
     195             : 
     196          82 :         RETURN_TRUE;
     197             : }
     198             : /* }}} */
     199             : 
     200             : /*
     201             :  * Local variables:
     202             :  * tab-width: 4
     203             :  * c-basic-offset: 4
     204             :  * End:
     205             :  * vim600: sw=4 ts=4 fdm=marker
     206             :  * vim<600: sw=4 ts=4
     207             :  */

Generated by: LCOV version 1.10

Generated at Fri, 20 Mar 2015 23:40:26 +0000 (8 days ago)

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