Skip to content

Commit 1875ea2

Browse files
author
Phalcon
committed
Merge pull request #1076 from dreamsxin/image_updated
Updated image function construct
2 parents d9c4039 + 5c966b4 commit 1875ea2

File tree

6 files changed

+170
-74
lines changed

6 files changed

+170
-74
lines changed

ext/image/adapter.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ PHP_METHOD(Phalcon_Image_Adapter, reflection){
671671
* will be preserved.
672672
*
673673
* @param Phalcon\Image\Adapter $watermark watermark Image instance
674-
* @param int $offset_x offset from the left
675-
* @param int $offset_y offset from the top
674+
* @param int $offset_x offset from the left, If less than 0 offset from the right, If true right the x offset
675+
* @param int $offset_y offset from the top, If less than 0 offset from the bottom, If true bottom the Y offset
676676
* @param int $opacity opacity of watermark: 1-100
677677
* @return Phalcon\Image\Adapter
678678
*/
@@ -686,8 +686,6 @@ PHP_METHOD(Phalcon_Image_Adapter, watermark){
686686

687687
phalcon_fetch_params(1, 1, 3, &watermark, &offset_x, &offset_y, &opacity);
688688

689-
690-
691689
PHALCON_OBS_VAR(image_width);
692690
phalcon_read_property_this(&image_width, this_ptr, SL("_width"), PH_NOISY_CC);
693691

ext/image/adapter/gd.c

+81-42
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, check){
154154
*/
155155
PHP_METHOD(Phalcon_Image_Adapter_GD, __construct){
156156

157-
zval *file, *exception_message;
157+
zval *file, *width = NULL, *height = NULL, *exception_message;
158158
zval *checked = NULL, *realpath = NULL, *type = NULL, *mime = NULL, *image = NULL;
159-
zval *ret, *saveflag;
159+
zval *ret, *saveflag, *blendmode;
160160

161161
PHALCON_MM_GROW();
162162

163-
phalcon_fetch_params(1, 1, 0, &file);
163+
phalcon_fetch_params(1, 1, 2, &file, &width, &height);
164164

165165
PHALCON_OBS_VAR(checked);
166166
phalcon_read_static_property(&checked, SL("phalcon\\image\\adapter\\gd"), SL("_checked") TSRMLS_CC);
@@ -169,57 +169,95 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, __construct){
169169
phalcon_call_static_noret("phalcon\\image\\adapter\\gd", "check");
170170
}
171171

172-
phalcon_call_parent_p1_noret(this_ptr, phalcon_image_adapter_gd_ce, "__construct", file);
172+
if (phalcon_file_exists(file TSRMLS_CC) != FAILURE) {
173+
phalcon_call_parent_p1_noret(this_ptr, phalcon_image_adapter_gd_ce, "__construct", file);
173174

174-
PHALCON_OBS_VAR(realpath);
175-
phalcon_read_property_this(&realpath, this_ptr, SL("_realpath"), PH_NOISY_CC);
175+
PHALCON_OBS_VAR(realpath);
176+
phalcon_read_property_this(&realpath, this_ptr, SL("_realpath"), PH_NOISY_CC);
176177

177-
PHALCON_OBS_VAR(type);
178-
phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC);
178+
PHALCON_OBS_VAR(type);
179+
phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC);
179180

180-
PHALCON_OBS_VAR(mime);
181-
phalcon_read_property_this(&mime, this_ptr, SL("_mime"), PH_NOISY_CC);
181+
PHALCON_OBS_VAR(mime);
182+
phalcon_read_property_this(&mime, this_ptr, SL("_mime"), PH_NOISY_CC);
182183

183-
switch (Z_LVAL_P(type)) {
184-
case 1: // GIF
185-
{
186-
PHALCON_INIT_VAR(image);
187-
phalcon_call_func_p1(image, "imagecreatefromgif", realpath);
188-
break;
189-
}
190-
case 2: // JPEG
191-
{
192-
PHALCON_INIT_VAR(image);
193-
phalcon_call_func_p1(image, "imagecreatefromjpeg", realpath);
194-
break;
195-
}
196-
case 3: // PNG
197-
{
198-
PHALCON_INIT_VAR(image);
199-
phalcon_call_func_p1(image, "imagecreatefrompng", realpath);
200-
break;
184+
switch (Z_LVAL_P(type)) {
185+
case 1: // GIF
186+
{
187+
PHALCON_INIT_VAR(image);
188+
phalcon_call_func_p1(image, "imagecreatefromgif", realpath);
189+
break;
190+
}
191+
case 2: // JPEG
192+
{
193+
PHALCON_INIT_VAR(image);
194+
phalcon_call_func_p1(image, "imagecreatefromjpeg", realpath);
195+
break;
196+
}
197+
case 3: // PNG
198+
{
199+
PHALCON_INIT_VAR(image);
200+
phalcon_call_func_p1(image, "imagecreatefrompng", realpath);
201+
break;
202+
}
203+
default:
204+
{
205+
PHALCON_INIT_VAR(exception_message);
206+
PHALCON_CONCAT_SVS(exception_message, "Installed GD does not support '", mime, "' images");
207+
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_image_exception_ce, exception_message);
208+
return;
209+
}
201210
}
202-
default:
203-
{
211+
212+
if (Z_TYPE_P(image) != IS_RESOURCE) {
204213
PHALCON_INIT_VAR(exception_message);
205-
PHALCON_CONCAT_SVS(exception_message, "Installed GD does not support '", mime, "' images");
214+
PHALCON_CONCAT_SVS(exception_message, "Create image from file '", realpath, "' failure ");
206215
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_image_exception_ce, exception_message);
207216
return;
208217
}
209-
}
210218

211-
if (Z_TYPE_P(image) != IS_RESOURCE) {
219+
PHALCON_INIT_VAR(saveflag);
220+
ZVAL_BOOL(saveflag, 1);
221+
222+
phalcon_call_func_p2_noret("imagesavealpha", image, saveflag);
223+
} else if (width && height) {
224+
PHALCON_INIT_VAR(image);
225+
phalcon_call_func_p2(image, "imagecreatetruecolor", width, height);
226+
227+
if (Z_TYPE_P(image) != IS_RESOURCE) {
228+
PHALCON_THROW_EXCEPTION_STR(phalcon_image_exception_ce, "Call imagecreatetruecolor failure");
229+
return;
230+
}
231+
232+
PHALCON_INIT_VAR(blendmode);
233+
ZVAL_BOOL(blendmode, 1);
234+
235+
PHALCON_INIT_VAR(saveflag);
236+
ZVAL_BOOL(saveflag, 1);
237+
238+
phalcon_call_func_p2_noret("imagealphablending", image, blendmode);
239+
phalcon_call_func_p2_noret("imagesavealpha", image, saveflag);
240+
241+
phalcon_update_property_this(this_ptr, SL("_realpath"), file TSRMLS_CC);
242+
phalcon_update_property_this(this_ptr, SL("_width"), width TSRMLS_CC);
243+
phalcon_update_property_this(this_ptr, SL("_height"), height TSRMLS_CC);
244+
245+
PHALCON_INIT_VAR(type);
246+
ZVAL_LONG(type, 3);
247+
248+
phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);
249+
250+
PHALCON_INIT_VAR(mime);
251+
ZVAL_STRING(mime, "image/png", 1);
252+
253+
phalcon_update_property_this(this_ptr, SL("_mime"), mime TSRMLS_CC);
254+
} else {
212255
PHALCON_INIT_VAR(exception_message);
213-
PHALCON_CONCAT_SVS(exception_message, "Create image from file '", realpath, "' failure ");
256+
PHALCON_CONCAT_SVS(exception_message, "Create image from file '", file, "' failure ");
214257
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_image_exception_ce, exception_message);
215258
return;
216259
}
217260

218-
PHALCON_INIT_VAR(saveflag);
219-
ZVAL_BOOL(saveflag, 1);
220-
221-
phalcon_call_func_p2_noret("imagesavealpha", image, saveflag);
222-
223261
phalcon_update_property_this(this_ptr, SL("_image"), image TSRMLS_CC);
224262

225263
PHALCON_MM_RESTORE();
@@ -361,6 +399,8 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _crop) {
361399
PHALCON_INIT_NVAR(tmp_image);
362400
phalcon_call_method_p2(tmp_image, this_ptr, "_create", width, height);
363401

402+
phalcon_update_property_this(this_ptr, SL("_image"), tmp_image TSRMLS_CC);
403+
364404
PHALCON_INIT_VAR(dst);
365405
ZVAL_LONG(dst, 0);
366406

@@ -1216,10 +1256,10 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _create) {
12161256
}
12171257

12181258
PHALCON_INIT_VAR(blendmode);
1219-
ZVAL_BOOL(blendmode, 0);
1259+
ZVAL_FALSE(blendmode);
12201260

12211261
PHALCON_INIT_VAR(saveflag);
1222-
ZVAL_BOOL(saveflag, 1);
1262+
ZVAL_TRUE(saveflag);
12231263

12241264
phalcon_call_func_p2_noret("imagealphablending", image, blendmode);
12251265
phalcon_call_func_p2_noret("imagesavealpha", image, saveflag);
@@ -1245,4 +1285,3 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, __destruct){
12451285

12461286
PHALCON_MM_RESTORE();
12471287
}
1248-

ext/image/adapter/gd.h

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, __destruct);
4040

4141
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_image_adapter_gd___construct, 0, 0, 1)
4242
ZEND_ARG_INFO(0, file)
43+
ZEND_ARG_INFO(0, width)
44+
ZEND_ARG_INFO(0, height)
4345
ZEND_END_ARG_INFO()
4446

4547
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_image_adapter_gd__resize, 0, 0, 2)

ext/image/adapter/imagick.c

+73-28
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, check){
105105
*/
106106
PHP_METHOD(Phalcon_Image_Adapter_Imagick, __construct){
107107

108-
zval *file;
109-
zval *checked = NULL, *realpath, *type, *im, *ret, *mode;
110-
zend_class_entry *ce0;
108+
zval *file, *width = NULL, *height = NULL, *exception_message;
109+
zval *checked = NULL, *realpath, *format, *type = NULL, *mime = NULL, *im, *ret = NULL, *mode, *imagickpixel, *color;
110+
zend_class_entry *ce0, *ce1;
111111

112112
PHALCON_MM_GROW();
113113

114-
phalcon_fetch_params(1, 1, 0, &file);
114+
phalcon_fetch_params(1, 1, 2, &file, &width, &height);
115+
116+
ce0 = zend_fetch_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
115117

116118
PHALCON_OBS_VAR(checked);
117119
phalcon_read_static_property(&checked, SL("phalcon\\image\\adapter\\imagick"), SL("_checked") TSRMLS_CC);
@@ -120,43 +122,84 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, __construct){
120122
phalcon_call_static_noret("phalcon\\image\\adapter\\imagick", "check");
121123
}
122124

123-
phalcon_call_parent_p1_noret(this_ptr, phalcon_image_adapter_imagick_ce, "__construct", file);
124-
125-
PHALCON_OBS_VAR(realpath);
126-
phalcon_read_property_this(&realpath, this_ptr, SL("_realpath"), PH_NOISY_CC);
127-
128-
PHALCON_OBS_VAR(type);
129-
phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC);
130-
131-
ce0 = zend_fetch_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
132-
133125
PHALCON_INIT_VAR(im);
134126
object_init_ex(im, ce0);
135127
if (phalcon_has_constructor(im TSRMLS_CC)) {
136128
phalcon_call_method_noret(im, "__construct");
137129
}
138130

139-
phalcon_call_method_p1_noret(im, "readImage", realpath);
131+
if (phalcon_file_exists(file TSRMLS_CC) != FAILURE) {
132+
phalcon_call_parent_p1_noret(this_ptr, phalcon_image_adapter_imagick_ce, "__construct", file);
140133

141-
PHALCON_INIT_VAR(ret);
142-
phalcon_call_method(ret, im, "getImageAlphaChannel");
134+
PHALCON_OBS_VAR(realpath);
135+
phalcon_read_property_this(&realpath, this_ptr, SL("_realpath"), PH_NOISY_CC);
143136

144-
if (!zend_is_true(ret)) {
145-
PHALCON_INIT_VAR(mode);
146-
phalcon_get_class_constant(mode, ce0, SS("ALPHACHANNEL_SET") TSRMLS_CC);
147-
phalcon_call_method_p1_noret(im, "setImageAlphaChannel", mode);
148-
}
137+
PHALCON_OBS_VAR(type);
138+
phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC);
139+
phalcon_call_method_p1_noret(im, "readImage", realpath);
140+
141+
PHALCON_INIT_VAR(ret);
142+
phalcon_call_method(ret, im, "getImageAlphaChannel");
143+
144+
if (!zend_is_true(ret)) {
145+
PHALCON_INIT_VAR(mode);
146+
phalcon_get_class_constant(mode, ce0, SS("ALPHACHANNEL_SET") TSRMLS_CC);
147+
phalcon_call_method_p1_noret(im, "setImageAlphaChannel", mode);
148+
}
149+
150+
if (phalcon_get_intval(type) == 1) {
151+
PHALCON_INIT_NVAR(ret);
152+
phalcon_call_method(ret, im, "coalesceImages");
153+
154+
phalcon_call_method_noret(im, "clear");
155+
phalcon_call_method_noret(im, "destroy");
156+
157+
phalcon_update_property_this(this_ptr, SL("_image"), ret TSRMLS_CC);
158+
} else {
159+
phalcon_update_property_this(this_ptr, SL("_image"), im TSRMLS_CC);
160+
}
161+
} else if (width && height) {
162+
ce1 = zend_fetch_class(SL("ImagickPixel"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
163+
164+
PHALCON_INIT_VAR(imagickpixel);
165+
object_init_ex(imagickpixel, ce1);
166+
167+
if (phalcon_has_constructor(imagickpixel TSRMLS_CC)) {
168+
PHALCON_INIT_VAR(color);
169+
ZVAL_STRING(color, "transparent", 1);
170+
171+
phalcon_call_method_p1_noret(imagickpixel, "__construct", color);
172+
}
173+
174+
phalcon_call_method_p3_noret(im, "newImage", width, height, imagickpixel);
175+
176+
PHALCON_INIT_VAR(format);
177+
ZVAL_STRING(format, "png", 1);
149178

150-
if (phalcon_get_intval(type) == 1) {
151179
PHALCON_INIT_NVAR(ret);
152-
phalcon_call_method(ret, im, "coalesceImages");
180+
phalcon_call_method_p1(ret, im, "setFormat", format);
181+
phalcon_call_method_p1(ret, im, "setImageFormat", format);
153182

154-
phalcon_call_method_noret(im, "clear");
155-
phalcon_call_method_noret(im, "destroy");
183+
phalcon_update_property_this(this_ptr, SL("_image"), im TSRMLS_CC);
184+
185+
phalcon_update_property_this(this_ptr, SL("_realpath"), file TSRMLS_CC);
186+
phalcon_update_property_this(this_ptr, SL("_width"), width TSRMLS_CC);
187+
phalcon_update_property_this(this_ptr, SL("_height"), height TSRMLS_CC);
156188

157-
phalcon_update_property_this(this_ptr, SL("_image"), ret TSRMLS_CC);
189+
PHALCON_INIT_VAR(type);
190+
ZVAL_LONG(type, 3);
191+
192+
phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);
193+
194+
PHALCON_INIT_VAR(mime);
195+
ZVAL_STRING(mime, "image/png", 1);
196+
197+
phalcon_update_property_this(this_ptr, SL("_mime"), mime TSRMLS_CC);
158198
} else {
159-
phalcon_update_property_this(this_ptr, SL("_image"), im TSRMLS_CC);
199+
PHALCON_INIT_VAR(exception_message);
200+
PHALCON_CONCAT_SVS(exception_message, "Create image from file '", file, "' failure ");
201+
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_image_exception_ce, exception_message);
202+
return;
160203
}
161204

162205
PHALCON_MM_RESTORE();
@@ -1157,6 +1200,7 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _save) {
11571200
phalcon_read_property_this(&im, this_ptr, SL("_image"), PH_NOISY_CC);
11581201

11591202
phalcon_call_method_p1_noret(im, "setFormat", format);
1203+
phalcon_call_method_p1_noret(im, "setImageFormat", format);
11601204
phalcon_call_method_noret(im, "stripImage");
11611205

11621206
if (phalcon_get_intval(type) == 1) {
@@ -1248,6 +1292,7 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _render) {
12481292
phalcon_read_property_this(&im, this_ptr, SL("_image"), PH_NOISY_CC);
12491293

12501294
phalcon_call_method_p1_noret(im, "setFormat", format);
1295+
phalcon_call_method_p1_noret(im, "setImageFormat", format);
12511296
phalcon_call_method_noret(im, "stripImage");
12521297

12531298
phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);

ext/image/adapter/imagick.h

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, __destruct);
3939

4040
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_image_adapter_imagick___construct, 0, 0, 1)
4141
ZEND_ARG_INFO(0, file)
42+
ZEND_ARG_INFO(0, width)
43+
ZEND_ARG_INFO(0, height)
4244
ZEND_END_ARG_INFO()
4345

4446
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_image_adapter_imagick__resize, 0, 0, 2)

unit-tests/ImageTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function testGD()
2727
return;
2828
}
2929

30+
@unlink('unit-tests/assets/new.jpg');
3031
@unlink('unit-tests/assets/gd-resize.jpg');
3132
@unlink('unit-tests/assets/gd-crop.jpg');
3233
@unlink('unit-tests/assets/gd-rotate.jpg');
@@ -37,6 +38,10 @@ public function testGD()
3738
@unlink('unit-tests/assets/gd-background.jpg');
3839

3940
try {
41+
$image = new Phalcon\Image\Adapter\GD('unit-tests/assets/new.jpg', 100, 100);
42+
$image->save();
43+
$this->assertTrue(file_exists('unit-tests/assets/production/new.jpg'));
44+
4045
$image = new Phalcon\Image\Adapter\GD('unit-tests/assets/phalconphp.jpg');
4146

4247
// Resize to 200 pixels on the shortest side
@@ -121,6 +126,7 @@ public function testImagick()
121126
return;
122127
}
123128

129+
@unlink('unit-tests/assets/production/new.jpg');
124130
@unlink('unit-tests/assets/production/imagick-resize.jpg');
125131
@unlink('unit-tests/assets/production/imagick-liquidRescale.jpg');
126132
@unlink('unit-tests/assets/production/imagick-crop.jpg');
@@ -132,6 +138,10 @@ public function testImagick()
132138
@unlink('unit-tests/assets/production/imagick-background.jpg');
133139

134140
try {
141+
$image = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/new.jpg', 100, 100);
142+
$image->save();
143+
$this->assertTrue(file_exists('unit-tests/assets/production/new.jpg'));
144+
135145
$image = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/phalconphp.jpg');
136146

137147
// Resize to 200 pixels on the shortest side

0 commit comments

Comments
 (0)