~ubuntu-branches/ubuntu/saucy/mapserver/saucy-security

« back to all changes in this revision

Viewing changes to mapscript/php/image.c

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2011-12-23 14:02:06 UTC
  • mfrom: (26.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111223140206-n3h9t2hsa8hyslmu
Tags: 6.0.1-2
Added missed stuff for libmapscript-perl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
 * $Id: php_mapscript.c 9765 2010-01-28 15:32:10Z aboudreault $
 
3
 *
 
4
 * Project:  MapServer
 
5
 * Purpose:  PHP/MapScript extension for MapServer.  External interface 
 
6
 *           functions
 
7
 * Author:   Daniel Morissette, DM Solutions Group (dmorissette@dmsolutions.ca)
 
8
 *           Alan Boudreault, Mapgears
 
9
 *
 
10
 **********************************************************************
 
11
 * Copyright (c) 2000-2010, Daniel Morissette, DM Solutions Group Inc.
 
12
 *
 
13
 * Permission is hereby granted, free of charge, to any person obtaining a
 
14
 * copy of this software and associated documentation files (the "Software"),
 
15
 * to deal in the Software without restriction, including without limitation
 
16
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
17
 * and/or sell copies of the Software, and to permit persons to whom the
 
18
 * Software is furnished to do so, subject to the following conditions:
 
19
 * 
 
20
 * The above copyright notice and this permission notice shall be included in 
 
21
 * all copies of this Software or works derived from this Software.
 
22
 * 
 
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 
26
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
28
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 
29
 * DEALINGS IN THE SOFTWARE.
 
30
 **********************************************************************/
 
31
 
 
32
#include "php_mapscript.h"
 
33
#include "ext/standard/head.h"
 
34
#include "main/php_output.h"
 
35
 
 
36
zend_class_entry *mapscript_ce_image;
 
37
 
 
38
ZEND_BEGIN_ARG_INFO_EX(image___get_args, 0, 0, 1)
 
39
  ZEND_ARG_INFO(0, property)
 
40
ZEND_END_ARG_INFO()
 
41
 
 
42
ZEND_BEGIN_ARG_INFO_EX(image___set_args, 0, 0, 2)
 
43
  ZEND_ARG_INFO(0, property)
 
44
  ZEND_ARG_INFO(0, value)
 
45
ZEND_END_ARG_INFO()
 
46
 
 
47
ZEND_BEGIN_ARG_INFO_EX(image_pasteImage_args, 0, 0, 2)
 
48
  ZEND_ARG_INFO(0, srcImg)
 
49
  ZEND_ARG_INFO(0, transparentColorHex)
 
50
  ZEND_ARG_INFO(0, dstX)
 
51
  ZEND_ARG_INFO(0, dstY)
 
52
  ZEND_ARG_INFO(0, angle)
 
53
ZEND_END_ARG_INFO()
 
54
 
 
55
ZEND_BEGIN_ARG_INFO_EX(image_saveImage_args, 0, 0, 2)
 
56
  ZEND_ARG_INFO(0, filename)
 
57
  ZEND_ARG_OBJ_INFO(0, map, mapObj, 0)
 
58
ZEND_END_ARG_INFO()
 
59
 
 
60
 
 
61
/* {{{ proto image __construct()
 
62
   imageObj CANNOT be instanciated, this will throw an exception on use */
 
63
PHP_METHOD(imageObj, __construct)
 
64
{
 
65
    mapscript_throw_exception("imageObj cannot be constructed" TSRMLS_CC);
 
66
}
 
67
/* }}} */
 
68
 
 
69
PHP_METHOD(imageObj, __get)
 
70
{
 
71
    char *property;
 
72
    long property_len;
 
73
    zval *zobj = getThis();
 
74
    php_image_object *php_image;
 
75
 
 
76
    PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
 
77
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
 
78
                              &property, &property_len) == FAILURE) {
 
79
        PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
80
        return;
 
81
    }
 
82
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
83
    
 
84
    php_image = (php_image_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
85
 
 
86
    IF_GET_LONG("width", php_image->image->width)
 
87
    else IF_GET_LONG("height", php_image->image->height) 
 
88
    else IF_GET_LONG("resolution", php_image->image->resolution) 
 
89
    else IF_GET_LONG("resolutionfactor", php_image->image->resolutionfactor) 
 
90
    else IF_GET_STRING("imagepath", php_image->image->imagepath) 
 
91
    else IF_GET_STRING("imageurl", php_image->image->imageurl) 
 
92
    else IF_GET_STRING("imagetype", php_image->image->format->name) 
 
93
    else 
 
94
    {
 
95
        mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
 
96
    }
 
97
}
 
98
 
 
99
PHP_METHOD(imageObj, __set)
 
100
{
 
101
    char *property;
 
102
    long property_len;
 
103
    zval *value;
 
104
    zval *zobj = getThis();
 
105
    php_image_object *php_image;
 
106
 
 
107
    PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
 
108
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz",
 
109
                              &property, &property_len, &value) == FAILURE) {
 
110
        PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
111
        return;
 
112
    }
 
113
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
114
    
 
115
    php_image = (php_image_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
116
 
 
117
    IF_SET_STRING("imagepath", php_image->image->imagepath, value)
 
118
    else IF_SET_STRING("imageurl", php_image->image->imageurl, value) 
 
119
    else IF_SET_STRING("imagetype", php_image->image->format->name, value) 
 
120
    else if ( (STRING_EQUAL("width", property)) ||
 
121
              (STRING_EQUAL("resolution", property)) ||
 
122
              (STRING_EQUAL("resolutionfactor", property)) ||
 
123
              (STRING_EQUAL("height", property)) )
 
124
    {
 
125
        mapscript_throw_exception("Property '%s' is read-only and cannot be set." TSRMLS_CC, property);
 
126
    }
 
127
    else 
 
128
    {
 
129
        mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
 
130
    }
 
131
}
 
132
 
 
133
/* {{{ proto int saveWebImage()
 
134
   Writes image to temp directory.  Returns image URL. */
 
135
PHP_METHOD(imageObj, saveWebImage)
 
136
{
 
137
    zval *zobj = getThis();
 
138
    php_image_object *php_image;
 
139
    char *imageFile = NULL;
 
140
    char *imageFilename = NULL;
 
141
    char path[MS_MAXPATHLEN];
 
142
    char *imageUrlFull = NULL;
 
143
 
 
144
    PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
 
145
    if (zend_parse_parameters_none() == FAILURE) {
 
146
        PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
147
        return;
 
148
    }
 
149
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
150
    
 
151
    php_image = (php_image_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
152
 
 
153
    imageFilename = msTmpFilename(php_image->image->format->extension);
 
154
    imageFile = msBuildPath(path, php_image->image->imagepath, imageFilename);
 
155
 
 
156
    if (msSaveImage(NULL, php_image->image, imageFile) != MS_SUCCESS)
 
157
    {
 
158
        mapscript_throw_mapserver_exception("Failed writing image to %s" TSRMLS_CC, imageFile);
 
159
        return;
 
160
    }
 
161
 
 
162
    imageUrlFull = msBuildPath(path, php_image->image->imageurl, imageFilename);
 
163
    msFree(imageFilename);
 
164
    
 
165
    RETURN_STRING(imageUrlFull, 1);
 
166
}
 
167
/* }}} */
 
168
 
 
169
/* {{{ proto void pasteImage(imageObj Src, int transparentColor [[,int dstx, int dsty], int angle])
 
170
   Pastes another imageObj on top of this imageObj. transparentColor is
 
171
   the color (0xrrggbb) from srcImg that should be considered transparent.
 
172
   Pass transparentColor=-1 if you don't want any transparent color.
 
173
   If optional dstx,dsty are provided then they define the position where the
 
174
   image should be copied (dstx,dsty = top-left corner position).
 
175
   The optional angle is a value between 0 and 360 degrees to rotate the
 
176
   source image counterclockwise.  Note that if a rotation is requested then
 
177
   the dstx and dsty coordinates specify the CENTER of the destination area.
 
178
   NOTE : this function only works for 8 bits GD images.
 
179
*/
 
180
PHP_METHOD(imageObj, pasteImage)
 
181
{
 
182
    long transparent=-1, dstx=0, dsty=0, angle=0;
 
183
    int angleSet=MS_FALSE;
 
184
    zval *zimage;
 
185
    zval *zobj = getThis();
 
186
    php_image_object *php_image, *php_imageSrc;
 
187
    /*int oldTransparentColor, newTransparentColor=-1, r, g, b;*/
 
188
    rendererVTableObj *renderer = NULL;
 
189
    rasterBufferObj rb;
 
190
 
 
191
    PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
 
192
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol|lll",
 
193
                              &zimage, mapscript_ce_image, &transparent,
 
194
                              &dstx, &dsty, &angle) == FAILURE) {
 
195
        PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
196
        return;
 
197
    }
 
198
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
199
    
 
200
    if  (ZEND_NUM_ARGS() == 3) {
 
201
        mapscript_report_php_error(E_WARNING, "dstX parameter given but not dstY" TSRMLS_CC);
 
202
    }
 
203
    else 
 
204
        angleSet = MS_TRUE;
 
205
 
 
206
    php_image = (php_image_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
207
    php_imageSrc = (php_image_object *) zend_object_store_get_object(zimage TSRMLS_CC);
 
208
        
 
209
    if (!MS_RENDERER_PLUGIN(php_imageSrc->image->format) ||
 
210
        !MS_RENDERER_PLUGIN(php_image->image->format))
 
211
        {
 
212
            mapscript_throw_exception("PasteImage function should only be used with renderer plugin drivers." TSRMLS_CC);
 
213
            return;
 
214
        }
 
215
 
 
216
#ifdef undef //USE_AGG
 
217
    if( MS_RENDERER_AGG(php_imageSrc->image->format)) 
 
218
      msAlphaAGG2GD(php_imageSrc->image); 
 
219
    if( MS_RENDERER_AGG(php_image->image->format)) 
 
220
      msAlphaAGG2GD(php_image->image); 
 
221
#endif
 
222
 
 
223
 
 
224
    renderer = MS_IMAGE_RENDERER(php_image->image);
 
225
    memset(&rb,0,sizeof(rasterBufferObj));
 
226
    
 
227
    renderer->getRasterBufferHandle(php_imageSrc->image, &rb);
 
228
    renderer->mergeRasterBuffer(php_image->image, &rb, 1.0, 0, 0, dstx, dsty, rb.width, rb.height);
 
229
 
 
230
    /* Look for r,g,b in color table and make it transparent.
 
231
     * will return -1 if there is no exact match which will result in
 
232
     * no transparent color in the call to gdImageColorTransparent().
 
233
     */
 
234
/*    if (transparent != -1)
 
235
    {
 
236
        r = (transparent / 0x010000) & 0xff;
 
237
        g = (transparent / 0x0100) & 0xff;
 
238
        b = transparent & 0xff;
 
239
        newTransparentColor = gdImageColorExact(php_imageSrc->image->img.gd, r, g, b);
 
240
    }
 
241
 
 
242
    oldTransparentColor = gdImageGetTransparent(php_imageSrc->image->img.gd);
 
243
    gdImageColorTransparent(php_imageSrc->image->img.gd, newTransparentColor);
 
244
 
 
245
    if (!angleSet)
 
246
        gdImageCopy(php_image->image->img.gd, php_imageSrc->image->img.gd, dstx, dsty, 
 
247
                    0, 0, php_imageSrc->image->img.gd->sx, php_imageSrc->image->img.gd->sy);
 
248
    else
 
249
        gdImageCopyRotated(php_image->image->img.gd, php_imageSrc->image->img.gd, dstx, dsty, 
 
250
                           0, 0, php_imageSrc->image->img.gd->sx, php_imageSrc->image->img.gd->sy,
 
251
                           angle);
 
252
 
 
253
                           gdImageColorTransparent(php_imageSrc->image->img.gd, oldTransparentColor);*/
 
254
 
 
255
    RETURN_LONG(MS_SUCCESS);
 
256
 
 
257
}
 
258
/* }}} */
 
259
 
 
260
/* {{{ proto int saveImage(string filename, mapObj map)
 
261
   Writes image object to specifed filename.  If filename is empty then
 
262
   write to stdout.  Returns MS_FAILURE on error. Second aregument oMap is not
 
263
   manadatory. It is usful when saving to other formats like GTIFF to get
 
264
   georeference infos.*/
 
265
 
 
266
PHP_METHOD(imageObj, saveImage)
 
267
{
 
268
    zval *zobj = getThis();
 
269
    zval *zmap = NULL;
 
270
    char *filename = NULL;
 
271
    long filename_len = 0;
 
272
    php_image_object *php_image;
 
273
    php_map_object *php_map;
 
274
    int status = MS_SUCCESS;
 
275
    /* stdout specific vars */
 
276
    int size=0;
 
277
    void *iptr=NULL;
 
278
 
 
279
    PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
 
280
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sO",
 
281
                              &filename, &filename_len,
 
282
                              &zmap, mapscript_ce_map) == FAILURE) {
 
283
        PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
284
        return;
 
285
    }
 
286
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
287
    
 
288
    php_image = (php_image_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
289
    if (zmap)
 
290
        php_map = (php_map_object *) zend_object_store_get_object(zmap TSRMLS_CC);
 
291
 
 
292
    if(filename_len > 0)
 
293
    {
 
294
        if ((status = msSaveImage((zmap ? php_map->map:NULL), php_image->image, filename) != MS_SUCCESS))
 
295
        {
 
296
            mapscript_throw_mapserver_exception("Failed writing image to %s" TSRMLS_CC, filename);
 
297
            return;
 
298
        }
 
299
 
 
300
        RETURN_LONG(status);
 
301
    }
 
302
 
 
303
    /* no filename - read stdout */
 
304
 
 
305
    /* if there is no output buffer active, set the header */
 
306
    if (OG(ob_nesting_level)<=0)
 
307
    {
 
308
        php_header(TSRMLS_C);
 
309
    }
 
310
   
 
311
 
 
312
    if (MS_RENDERER_PLUGIN(php_image->image->format))
 
313
    {
 
314
        iptr = (void *)msSaveImageBuffer(php_image->image, &size, php_image->image->format);
 
315
    }
 
316
    else if (php_image->image->format->name && (strcasecmp(php_image->image->format->name, "imagemap")==0))
 
317
    {
 
318
        iptr = php_image->image->img.imagemap;
 
319
        size = strlen(php_image->image->img.imagemap);
 
320
    }
 
321
 
 
322
    if (size == 0) {
 
323
        mapscript_throw_mapserver_exception("Failed writing image to stdout" TSRMLS_CC);
 
324
        return;
 
325
    } 
 
326
    else
 
327
    {
 
328
        php_write(iptr, size TSRMLS_CC);
 
329
        status = MS_SUCCESS;
 
330
        /* status = size;  why should we return the size ?? */
 
331
        gdFree(iptr);
 
332
    }
 
333
 
 
334
    RETURN_LONG(status);
 
335
}
 
336
/* }}} */
 
337
 
 
338
zend_function_entry image_functions[] = {
 
339
    PHP_ME(imageObj, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
 
340
    PHP_ME(imageObj, __get, image___get_args, ZEND_ACC_PUBLIC)
 
341
    PHP_ME(imageObj, __set, image___set_args, ZEND_ACC_PUBLIC)
 
342
    PHP_MALIAS(imageObj, set, __set, NULL, ZEND_ACC_PUBLIC)
 
343
    PHP_ME(imageObj, saveWebImage, NULL, ZEND_ACC_PUBLIC)
 
344
    PHP_ME(imageObj, pasteImage, image_pasteImage_args, ZEND_ACC_PUBLIC)
 
345
    PHP_ME(imageObj, saveImage, image_saveImage_args, ZEND_ACC_PUBLIC)
 
346
    {NULL, NULL, NULL}
 
347
};
 
348
 
 
349
 
 
350
void mapscript_create_image(imageObj *image, zval *return_value TSRMLS_DC)
 
351
{
 
352
    php_image_object * php_image;
 
353
    object_init_ex(return_value, mapscript_ce_image); 
 
354
    php_image = (php_image_object *)zend_object_store_get_object(return_value TSRMLS_CC);
 
355
    php_image->image = image;    
 
356
}
 
357
 
 
358
static void mapscript_image_object_destroy(void *object TSRMLS_DC)
 
359
{
 
360
    php_image_object *php_image = (php_image_object *)object;
 
361
 
 
362
    MAPSCRIPT_FREE_OBJECT(php_image);
 
363
 
 
364
    msFreeImage(php_image->image);
 
365
    
 
366
    efree(object);
 
367
}
 
368
 
 
369
static zend_object_value mapscript_image_object_new(zend_class_entry *ce TSRMLS_DC)
 
370
{
 
371
    zend_object_value retval;
 
372
    php_image_object *php_image;
 
373
 
 
374
    MAPSCRIPT_ALLOC_OBJECT(php_image, php_image_object);
 
375
 
 
376
    retval = mapscript_object_new(&php_image->std, ce,
 
377
                                  &mapscript_image_object_destroy TSRMLS_CC);
 
378
 
 
379
    return retval;
 
380
}
 
381
 
 
382
PHP_MINIT_FUNCTION(image)
 
383
{
 
384
    zend_class_entry ce;
 
385
 
 
386
    MAPSCRIPT_REGISTER_CLASS("imageObj", 
 
387
                             image_functions,
 
388
                             mapscript_ce_image,
 
389
                             mapscript_image_object_new);
 
390
 
 
391
    mapscript_ce_image->ce_flags |= ZEND_ACC_FINAL_CLASS; 
 
392
    
 
393
    return SUCCESS;
 
394
}