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: 68 86 79.1 %
Date: 2014-08-04 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:04 +0000 (19 days ago)

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