From 617e9bb731f2d0e92583f9c75643d5fe8018aa03 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Tue, 10 Sep 2013 11:51:28 +0800 Subject: [PATCH 01/14] Fix BUG #1214 --- ext/image/adapter/imagick.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/ext/image/adapter/imagick.c b/ext/image/adapter/imagick.c index bf8f6d6c994..ad20e3a5b52 100644 --- a/ext/image/adapter/imagick.c +++ b/ext/image/adapter/imagick.c @@ -436,6 +436,8 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _rotate) { phalcon_fetch_params(1, 1, 0, °rees); + ce0 = zend_fetch_class(SL("ImagickPixel"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); + PHALCON_OBS_VAR(im); phalcon_read_property_this(&im, this_ptr, SL("_image"), PH_NOISY_CC); @@ -445,8 +447,6 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _rotate) { PHALCON_INIT_VAR(tmp); ZVAL_LONG(tmp, 0); - ce0 = zend_fetch_class(SL("ImagickPixel"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); - PHALCON_INIT_VAR(background); object_init_ex(background, ce0); if (phalcon_has_constructor(background TSRMLS_CC)) { @@ -655,6 +655,7 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _reflection) { phalcon_fetch_params(1, 3, 0, &height, &opacity, &fade_in); ce0 = zend_fetch_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); + ce1 = zend_fetch_class(SL("ImagickPixel"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_OBS_VAR(im); phalcon_read_property_this(&im, this_ptr, SL("_image"), PH_NOISY_CC); @@ -781,8 +782,6 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _reflection) { PHALCON_INIT_VAR(h0); ZVAL_LONG(h0, ini_h); - ce1 = zend_fetch_class(SL("ImagickPixel"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); - PHALCON_INIT_VAR(background); object_init_ex(background, ce1); if (phalcon_has_constructor(background TSRMLS_CC)) { @@ -906,14 +905,14 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _watermark) { phalcon_fetch_params(1, 4, 0, &watermark_image, &offset_x, &offset_y, &opacity); + ce0 = zend_fetch_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); + PHALCON_OBS_VAR(im); phalcon_read_property_this(&im, this_ptr, SL("_image"), PH_NOISY_CC); PHALCON_OBS_VAR(type); phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC); - ce0 = zend_fetch_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); - PHALCON_INIT_VAR(watermark); phalcon_call_method(watermark, watermark_image, "getImage"); object_init_ex(watermark, ce0); @@ -1015,6 +1014,10 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _text) { phalcon_fetch_params(1, 9, 0, &text, &offset_x, &offset_y, &opacity, &r, &g, &b, &size, &fontfile); + ce0 = zend_fetch_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); + ce1 = zend_fetch_class(SL("ImagickDraw"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); + ce2 = zend_fetch_class(SL("ImagickPixel"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); + if (!offset_x) { PHALCON_INIT_VAR(offset_x); } else { @@ -1027,10 +1030,6 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _text) { PHALCON_SEPARATE_PARAM(offset_y); } - ce0 = zend_fetch_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); - ce1 = zend_fetch_class(SL("ImagickDraw"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); - ce2 = zend_fetch_class(SL("ImagickPixel"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); - PHALCON_OBS_VAR(im); phalcon_read_property_this(&im, this_ptr, SL("_image"), PH_NOISY_CC); @@ -1250,6 +1249,8 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _mask){ phalcon_fetch_params(1, 1, 0, &mask); + ce0 = zend_fetch_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); + PHALCON_OBS_VAR(im); phalcon_read_property_this(&im, this_ptr, SL("_image"), PH_NOISY_CC); @@ -1275,8 +1276,6 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _mask){ PHALCON_INIT_VAR(tmp); ZVAL_LONG(tmp, 0); - ce0 = zend_fetch_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); - PHALCON_INIT_VAR(composite); phalcon_get_class_constant(composite, ce0, SS("COMPOSITE_DSTIN") TSRMLS_CC); From a8c77dc3c7e9633a026745b42e5ada24d69b4623 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Tue, 10 Sep 2013 12:01:04 +0800 Subject: [PATCH 02/14] add mask for ImageTest.php --- unit-tests/ImageTest.php | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/unit-tests/ImageTest.php b/unit-tests/ImageTest.php index e0b9a20cfdb..d252c7e7724 100644 --- a/unit-tests/ImageTest.php +++ b/unit-tests/ImageTest.php @@ -27,22 +27,22 @@ public function testGD() return; } - @unlink('unit-tests/assets/gd-new.jpg'); - @unlink('unit-tests/assets/gd-resize.jpg'); - @unlink('unit-tests/assets/gd-crop.jpg'); - @unlink('unit-tests/assets/gd-rotate.jpg'); - @unlink('unit-tests/assets/gd-flip.jpg'); - @unlink('unit-tests/assets/gd-sharpen.jpg'); - @unlink('unit-tests/assets/gd-reflection.jpg'); - @unlink('unit-tests/assets/gd-watermark.jpg'); - @unlink('unit-tests/assets/gd-background.jpg'); + @unlink('unit-tests/assets/production/gd-new.jpg'); + @unlink('unit-tests/assets/production/gd-resize.jpg'); + @unlink('unit-tests/assets/production/gd-crop.jpg'); + @unlink('unit-tests/assets/production/gd-rotate.jpg'); + @unlink('unit-tests/assets/production/gd-flip.jpg'); + @unlink('unit-tests/assets/production/gd-sharpen.jpg'); + @unlink('unit-tests/assets/production/gd-reflection.jpg'); + @unlink('unit-tests/assets/production/gd-watermark.jpg'); + @unlink('unit-tests/assets/production/gd-mask.jpg'); + @unlink('unit-tests/assets/production/gd-background.jpg'); try { - // Create new image - $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/gd-new.jpg', 100, 100); + $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/production/gd-new.jpg', 100, 100); $image->save(); - $this->assertTrue(file_exists('unit-tests/assets/production/new.jpg')); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-new.jpg')); $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/phalconphp.jpg'); @@ -111,6 +111,11 @@ public function testGD() $image->watermark($mark, TRUE, TRUE)->save('unit-tests/assets/production/gd-watermark.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/gd-watermark.jpg')); + // Mask image + $mask = new Phalcon\Image\Adapter\GD('unit-tests/assets/logo.png'); + $image->mask($mark)->save('unit-tests/assets/production/gd-mask.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-mask.jpg')); + // Add a text to the bottom right of the image $image->text('hello', TRUE, TRUE); @@ -142,6 +147,7 @@ public function testGD() // Render the image as a PNG //$data = $image->render('png'); } catch (Exception $e) { + $this->assertTrue(FALSE); } } @@ -160,6 +166,7 @@ public function testImagick() @unlink('unit-tests/assets/production/imagick-sharpen.jpg'); @unlink('unit-tests/assets/production/imagick-reflection.jpg'); @unlink('unit-tests/assets/production/imagick-watermark.jpg'); + @unlink('unit-tests/assets/production/imagick-mask.jpg'); @unlink('unit-tests/assets/production/imagick-background.jpg'); try { @@ -231,6 +238,11 @@ public function testImagick() $image->watermark($mark, TRUE, TRUE)->save('unit-tests/assets/production/imagick-watermark.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/imagick-watermark.jpg')); + // Mask image + $mask = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/logo.png'); + $image->mask($mark)->save('unit-tests/assets/production/imagick-mask.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-mask.jpg')); + // Add a text to the bottom right of the image $image->text('hello', TRUE, TRUE); @@ -262,6 +274,7 @@ public function testImagick() // Render the image as a PNG //$data = $image->render('png'); } catch (Exception $e) { + $this->assertTrue(FALSE); } } } \ No newline at end of file From beaf2408542463aef034b105621d74d919e373a1 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Tue, 10 Sep 2013 13:09:04 +0800 Subject: [PATCH 03/14] Fix any bug and Perfect ImageTeset.php --- ext/image/adapter.c | 23 +++++++++++++---------- ext/image/adapter/gd.c | 10 ++++------ ext/image/adapter/imagick.c | 8 ++++---- unit-tests/ImageTest.php | 18 ++++++++++-------- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/ext/image/adapter.c b/ext/image/adapter.c index cee4ba506d7..ec1267fa8ca 100644 --- a/ext/image/adapter.c +++ b/ext/image/adapter.c @@ -798,7 +798,7 @@ PHP_METHOD(Phalcon_Image_Adapter, text){ ZVAL_STRING(tmp_color, c, 1); - if (Z_STRLEN_P(tmp_color) >= 6) { + if (Z_STRLEN_P(tmp_color) < 6) { PHALCON_THROW_EXCEPTION_STR(phalcon_image_exception_ce, "color is not valid"); return; } @@ -1106,23 +1106,26 @@ PHP_METHOD(Phalcon_Image_Adapter, save){ */ PHP_METHOD(Phalcon_Image_Adapter, render){ - zval *ext = NULL, *quality = NULL, *type, *include_dot; + zval *ext = NULL, *quality = NULL, *constant, *file; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 2, &ext, &quality); if (!ext) { - PHALCON_INIT_VAR(include_dot); - ZVAL_FALSE(include_dot); + PHALCON_INIT_NVAR(ext); + file = phalcon_fetch_nproperty_this(this_ptr, SL("_file"), PH_NOISY_CC); + + PHALCON_INIT_VAR(constant); + if (zend_get_constant(SL("PATHINFO_EXTENSION"), constant TSRMLS_CC) == FAILURE) { + RETURN_MM(); + } - type = phalcon_fetch_nproperty_this(this_ptr, SL("_type"), PH_NOISY_CC); + phalcon_call_func_p2(ext, "pathinfo", file, constant); - /** - * @todo image_type_to_extension is from GD - */ - PHALCON_INIT_VAR(ext); - phalcon_call_func_p2(ext, "image_type_to_extension", type, include_dot); + if (!PHALCON_IS_NOT_EMPTY(ext)) { + ZVAL_STRING(ext, "png", 1); + } } if (!quality || Z_TYPE_P(quality) != IS_LONG) { diff --git a/ext/image/adapter/gd.c b/ext/image/adapter/gd.c index a4ba5ef1fc7..33026367101 100644 --- a/ext/image/adapter/gd.c +++ b/ext/image/adapter/gd.c @@ -767,7 +767,6 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _reflection) { ZVAL_LONG(dst, 0); PHALCON_CALL_FUNCTION(NULL, NULL, "imagecopy", 8, reflection, image, dst, dst, dst, dst, image_width, image_height); - phalcon_update_property_this(this_ptr, SL("_image"), reflection TSRMLS_CC); PHALCON_INIT_NVAR(tmp); ZVAL_LONG(tmp, 1); @@ -1080,7 +1079,7 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _text) { phalcon_call_func_p5_ex(color, &color, "imagecolorallocatealpha", image, r, g, b, opacity); phalcon_call_func_p6_noret("imagestring", image, size, offset_x, offset_y, text, color); - } + } PHALCON_MM_RESTORE(); } @@ -1113,7 +1112,6 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _mask){ PHALCON_INIT_VAR(saveflag); ZVAL_TRUE(saveflag); - PHALCON_INIT_VAR(mask_image); phalcon_call_func_p2_noret("imagesavealpha", mask_image, saveflag); PHALCON_OBS_VAR(mask_image_width); @@ -1128,7 +1126,6 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _mask){ PHALCON_OBS_VAR(newimage); phalcon_call_method_p2_ex(newimage, &newimage, this_ptr, "_create", image_width, image_height); - phalcon_call_func_p2_noret("imagesavealpha", newimage, saveflag); PHALCON_INIT_VAR(c); @@ -1148,8 +1145,9 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _mask){ phalcon_call_func_p2_ex(temp_image, &temp_image, "imagecreatetruecolor", image_width, image_height); PHALCON_CALL_FUNCTION(NULL, NULL, "imagecopyresampled", 10, temp_image, mask_image, c, c, c, c, image_width, image_height, mask_image_width, mask_image_height); - + phalcon_call_func_p1_noret("imagedestroy", mask_image); + PHALCON_CPY_WRT(mask_image, temp_image); } @@ -1204,9 +1202,9 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _mask){ } } - phalcon_update_property_this(this_ptr, SL("_image"), newimage TSRMLS_CC); phalcon_call_func_p1_noret("imagedestroy", image); phalcon_call_func_p1_noret("imagedestroy", mask_image); + phalcon_update_property_this(this_ptr, SL("_image"), newimage TSRMLS_CC); PHALCON_MM_RESTORE(); } diff --git a/ext/image/adapter/imagick.c b/ext/image/adapter/imagick.c index ad20e3a5b52..f50bfcaf2e1 100644 --- a/ext/image/adapter/imagick.c +++ b/ext/image/adapter/imagick.c @@ -160,7 +160,6 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, __construct){ phalcon_call_method(type, im, "getImageType"); phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC); - PHALCON_INIT_VAR(format); phalcon_call_method(format, im, "getImageFormat"); @@ -914,7 +913,6 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _watermark) { phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC); PHALCON_INIT_VAR(watermark); - phalcon_call_method(watermark, watermark_image, "getImage"); object_init_ex(watermark, ce0); if (phalcon_has_constructor(watermark TSRMLS_CC)) { phalcon_call_method_noret(watermark, "__construct"); @@ -1256,6 +1254,7 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _mask){ PHALCON_OBS_VAR(type); phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC); + PHALCON_INIT_VAR(mask_im); object_init_ex(mask_im, ce0); if (phalcon_has_constructor(mask_im TSRMLS_CC)) { @@ -1292,13 +1291,14 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _mask){ PHALCON_INIT_NVAR(next); phalcon_call_method(next, im, "nextImage"); } while (zend_is_true(next)); - } else { + } + + else { phalcon_call_method_p1_noret(im, "setImageMatte", matte); phalcon_call_method_p4_noret(im, "compositeImage", mask_im, composite, tmp, tmp); } phalcon_call_method_noret(mask_im, "clear"); - phalcon_call_method_noret(mask_im, "destroy"); PHALCON_MM_RESTORE(); } diff --git a/unit-tests/ImageTest.php b/unit-tests/ImageTest.php index d252c7e7724..0cec60fe340 100644 --- a/unit-tests/ImageTest.php +++ b/unit-tests/ImageTest.php @@ -50,8 +50,9 @@ public function testGD() $image->resize(200, 200)->save('unit-tests/assets/production/gd-resize.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/gd-resize.jpg')); - $width = imagesx('unit-tests/assets/production/gd-resize.jpg'); - $height = imagesy('unit-tests/assets/production/gd-resize.jpg'); + $tmp = imagecreatefromjpeg('unit-tests/assets/production/gd-resize.jpg'); + $width = imagesx($tmp); + $height = imagesy($tmp); $this->assertTrue($width <= 200); $this->assertTrue($height <= 200); @@ -72,8 +73,9 @@ public function testGD() $image->crop(200, 200)->save('unit-tests/assets/production/gd-crop.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/gd-crop.jpg')); - $width = imagesx('unit-tests/assets/production/gd-crop.jpg'); - $height = imagesy('unit-tests/assets/production/gd-crop.jpg'); + $tmp = imagecreatefromjpeg('unit-tests/assets/production/gd-crop.jpg'); + $width = imagesx($tmp); + $height = imagesy($tmp); $this->assertEquals($width, 200); $this->assertEquals($height, 200); @@ -113,7 +115,7 @@ public function testGD() // Mask image $mask = new Phalcon\Image\Adapter\GD('unit-tests/assets/logo.png'); - $image->mask($mark)->save('unit-tests/assets/production/gd-mask.jpg'); + $image->mask($mask)->save('unit-tests/assets/production/gd-mask.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/gd-mask.jpg')); // Add a text to the bottom right of the image @@ -171,9 +173,9 @@ public function testImagick() try { // Create new image - $image = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/imagick-new.jpg', 100, 100); + $image = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/production/imagick-new.jpg', 100, 100); $image->save(); - $this->assertTrue(file_exists('unit-tests/assets/production/new.jpg')); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-new.jpg')); $image = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/phalconphp.jpg'); @@ -240,7 +242,7 @@ public function testImagick() // Mask image $mask = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/logo.png'); - $image->mask($mark)->save('unit-tests/assets/production/imagick-mask.jpg'); + $image->mask($mask)->save('unit-tests/assets/production/imagick-mask.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/imagick-mask.jpg')); // Add a text to the bottom right of the image From 65c2a542afe9a64fb7ad8773be3080329571a861 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Tue, 10 Sep 2013 15:04:57 +0800 Subject: [PATCH 04/14] delete Exception Handling for ImageTest --- unit-tests/ImageTest.php | 316 +++++++++++++++++++-------------------- 1 file changed, 154 insertions(+), 162 deletions(-) diff --git a/unit-tests/ImageTest.php b/unit-tests/ImageTest.php index 0cec60fe340..90951f8ea84 100644 --- a/unit-tests/ImageTest.php +++ b/unit-tests/ImageTest.php @@ -38,119 +38,115 @@ public function testGD() @unlink('unit-tests/assets/production/gd-mask.jpg'); @unlink('unit-tests/assets/production/gd-background.jpg'); - try { - // Create new image - $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/production/gd-new.jpg', 100, 100); - $image->save(); - $this->assertTrue(file_exists('unit-tests/assets/production/gd-new.jpg')); + // Create new image + $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/production/gd-new.jpg', 100, 100); + $image->save(); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-new.jpg')); - $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/phalconphp.jpg'); + $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/phalconphp.jpg'); - // Resize to 200 pixels on the shortest side - $image->resize(200, 200)->save('unit-tests/assets/production/gd-resize.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/gd-resize.jpg')); + // Resize to 200 pixels on the shortest side + $image->resize(200, 200)->save('unit-tests/assets/production/gd-resize.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-resize.jpg')); - $tmp = imagecreatefromjpeg('unit-tests/assets/production/gd-resize.jpg'); - $width = imagesx($tmp); - $height = imagesy($tmp); - $this->assertTrue($width <= 200); - $this->assertTrue($height <= 200); + $tmp = imagecreatefromjpeg('unit-tests/assets/production/gd-resize.jpg'); + $width = imagesx($tmp); + $height = imagesy($tmp); + $this->assertTrue($width <= 200); + $this->assertTrue($height <= 200); - // Resize to 200x200 pixels, keeping aspect ratio - //$image->resize(200, 200, Phalcon\Image::INVERSE); + // Resize to 200x200 pixels, keeping aspect ratio + //$image->resize(200, 200, Phalcon\Image::INVERSE); - // Resize to 500 pixel width, keeping aspect ratio - //$image->resize(500, NULL); + // Resize to 500 pixel width, keeping aspect ratio + //$image->resize(500, NULL); - // Resize to 500 pixel height, keeping aspect ratio - //$image->resize(NULL, 500); + // Resize to 500 pixel height, keeping aspect ratio + //$image->resize(NULL, 500); - // Resize to 200x500 pixels, ignoring aspect ratio - //$image->resize(200, 500, Phalcon\Image::NONE); + // Resize to 200x500 pixels, ignoring aspect ratio + //$image->resize(200, 500, Phalcon\Image::NONE); - // Crop the image to 200x200 pixels, from the center - $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/phalconphp.jpg'); - $image->crop(200, 200)->save('unit-tests/assets/production/gd-crop.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/gd-crop.jpg')); + // Crop the image to 200x200 pixels, from the center + $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/phalconphp.jpg'); + $image->crop(200, 200)->save('unit-tests/assets/production/gd-crop.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-crop.jpg')); - $tmp = imagecreatefromjpeg('unit-tests/assets/production/gd-crop.jpg'); - $width = imagesx($tmp); - $height = imagesy($tmp); - $this->assertEquals($width, 200); - $this->assertEquals($height, 200); + $tmp = imagecreatefromjpeg('unit-tests/assets/production/gd-crop.jpg'); + $width = imagesx($tmp); + $height = imagesy($tmp); + $this->assertEquals($width, 200); + $this->assertEquals($height, 200); - // Rotate 45 degrees clockwise - $image->rotate(45)->save('unit-tests/assets/production/gd-rotate.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/gd-rotate.jpg')); + // Rotate 45 degrees clockwise + $image->rotate(45)->save('unit-tests/assets/production/gd-rotate.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-rotate.jpg')); - // Rotate 90% counter-clockwise - //$image->rotate(-90); + // Rotate 90% counter-clockwise + //$image->rotate(-90); - // Flip the image from top to bottom - $image->flip(Phalcon\Image::HORIZONTAL)->save('unit-tests/assets/production/gd-flip.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/gd-flip.jpg')); + // Flip the image from top to bottom + $image->flip(Phalcon\Image::HORIZONTAL)->save('unit-tests/assets/production/gd-flip.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-flip.jpg')); - // Flip the image from left to right - //$image->flip(Phalcon\Image::VERTICAL); + // Flip the image from left to right + //$image->flip(Phalcon\Image::VERTICAL); - // Sharpen the image by 20% - $image->sharpen(20)->save('unit-tests/assets/production/gd-sharpen.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/gd-sharpen.jpg')); + // Sharpen the image by 20% + $image->sharpen(20)->save('unit-tests/assets/production/gd-sharpen.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-sharpen.jpg')); - // Create a 50 pixel reflection that fades from 0-100% opacity - $image->reflection(50)->save('unit-tests/assets/production/gd-reflection.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/gd-reflection.jpg')); + // Create a 50 pixel reflection that fades from 0-100% opacity + $image->reflection(50)->save('unit-tests/assets/production/gd-reflection.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-reflection.jpg')); - // Create a 50 pixel reflection that fades from 100-0% opacity - //$image->reflection(50, 100, TRUE)->save('reflection.jpg'); + // Create a 50 pixel reflection that fades from 100-0% opacity + //$image->reflection(50, 100, TRUE)->save('reflection.jpg'); - // Create a 50 pixel reflection that fades from 0-60% opacity - //$image->reflection(50, 60, TRUE); + // Create a 50 pixel reflection that fades from 0-60% opacity + //$image->reflection(50, 60, TRUE); - // Add a watermark to the bottom right of the image - $mark = new Phalcon\Image\Adapter\GD('unit-tests/assets/logo.png'); - $image->watermark($mark, TRUE, TRUE)->save('unit-tests/assets/production/gd-watermark.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/gd-watermark.jpg')); + // Add a watermark to the bottom right of the image + $mark = new Phalcon\Image\Adapter\GD('unit-tests/assets/logo.png'); + $image->watermark($mark, TRUE, TRUE)->save('unit-tests/assets/production/gd-watermark.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-watermark.jpg')); - // Mask image - $mask = new Phalcon\Image\Adapter\GD('unit-tests/assets/logo.png'); - $image->mask($mask)->save('unit-tests/assets/production/gd-mask.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/gd-mask.jpg')); + // Mask image + $mask = new Phalcon\Image\Adapter\GD('unit-tests/assets/logo.png'); + $image->mask($mask)->save('unit-tests/assets/production/gd-mask.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-mask.jpg')); - // Add a text to the bottom right of the image - $image->text('hello', TRUE, TRUE); + // Add a text to the bottom right of the image + $image->text('hello', TRUE, TRUE); - // Set font size - // $image->text('hello', TRUE, TRUE, NULL, NULL, 12); + // Set font size + // $image->text('hello', TRUE, TRUE, NULL, NULL, 12); - // Set font - // $image->text('hello', TRUE, TRUE, NULL, NULL, 12, /usr/share/fonts/truetype/wqy/wqy-microhei.ttc); + // Set font + // $image->text('hello', TRUE, TRUE, NULL, NULL, 12, /usr/share/fonts/truetype/wqy/wqy-microhei.ttc); - // Add a text to the center of the image - //$image->text('hello'); + // Add a text to the center of the image + //$image->text('hello'); - // Make the image background black - $mark->background('#000')->save('unit-tests/assets/production/gd-background.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/gd-background.jpg')); + // Make the image background black + $mark->background('#000')->save('unit-tests/assets/production/gd-background.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/gd-background.jpg')); - // Make the image background black with 50% opacity - //$image->background('#000', 50); + // Make the image background black with 50% opacity + //$image->background('#000', 50); - // Save the image as a PNG - //$image->save('saved/gd.png'); + // Save the image as a PNG + //$image->save('saved/gd.png'); - // Overwrite the original image - //$image->save(); + // Overwrite the original image + //$image->save(); - // Render the image at 50% quality - //$data = $image->render(NULL, 50); - - // Render the image as a PNG - //$data = $image->render('png'); - } catch (Exception $e) { - $this->assertTrue(FALSE); - } + // Render the image at 50% quality + //$data = $image->render(NULL, 50); + + // Render the image as a PNG + //$data = $image->render('png'); } public function testImagick() @@ -171,112 +167,108 @@ public function testImagick() @unlink('unit-tests/assets/production/imagick-mask.jpg'); @unlink('unit-tests/assets/production/imagick-background.jpg'); - try { - // Create new image - $image = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/production/imagick-new.jpg', 100, 100); - $image->save(); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-new.jpg')); + // Create new image + $image = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/production/imagick-new.jpg', 100, 100); + $image->save(); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-new.jpg')); - $image = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/phalconphp.jpg'); + $image = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/phalconphp.jpg'); - // Resize to 200 pixels on the shortest side - $image->resize(200, 200)->save('unit-tests/assets/production/imagick-resize.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-resize.jpg')); + // Resize to 200 pixels on the shortest side + $image->resize(200, 200)->save('unit-tests/assets/production/imagick-resize.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-resize.jpg')); - // Resize to 200x200 pixels, keeping aspect ratio - //$image->resize(200, 200, Phalcon\Image::INVERSE); + // Resize to 200x200 pixels, keeping aspect ratio + //$image->resize(200, 200, Phalcon\Image::INVERSE); - // Resize to 500 pixel width, keeping aspect ratio - //$image->resize(500, NULL); + // Resize to 500 pixel width, keeping aspect ratio + //$image->resize(500, NULL); - // Resize to 500 pixel height, keeping aspect ratio - //$image->resize(NULL, 500); + // Resize to 500 pixel height, keeping aspect ratio + //$image->resize(NULL, 500); - // Resize to 200x500 pixels, ignoring aspect ratio - //$image->resize(200, 500, Phalcon\Image::NONE); + // Resize to 200x500 pixels, ignoring aspect ratio + //$image->resize(200, 500, Phalcon\Image::NONE); - // The images using liquid rescaling resize to 200x200 - $image->liquidRescale(200, 200)->save('unit-tests/assets/production/imagick-liquidRescale.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-liquidRescale.jpg')); + // The images using liquid rescaling resize to 200x200 + $image->liquidRescale(200, 200)->save('unit-tests/assets/production/imagick-liquidRescale.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-liquidRescale.jpg')); - // The images using liquid rescaling resize to 500x500 - //$image->liquidRescale(500, 500, 3, 25); + // The images using liquid rescaling resize to 500x500 + //$image->liquidRescale(500, 500, 3, 25); - // Crop the image to 200x200 pixels, from the center - $image->crop(200, 200)->save('unit-tests/assets/production/imagick-crop.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-crop.jpg')); + // Crop the image to 200x200 pixels, from the center + $image->crop(200, 200)->save('unit-tests/assets/production/imagick-crop.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-crop.jpg')); - // Rotate 45 degrees clockwise - $image->rotate(45)->save('unit-tests/assets/production/imagick-rotate.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-rotate.jpg')); + // Rotate 45 degrees clockwise + $image->rotate(45)->save('unit-tests/assets/production/imagick-rotate.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-rotate.jpg')); - // Rotate 90% counter-clockwise - //$image->rotate(-90); + // Rotate 90% counter-clockwise + //$image->rotate(-90); - // Flip the image from top to bottom - $image->flip(Phalcon\Image::HORIZONTAL)->save('unit-tests/assets/production/imagick-flip.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-flip.jpg')); + // Flip the image from top to bottom + $image->flip(Phalcon\Image::HORIZONTAL)->save('unit-tests/assets/production/imagick-flip.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-flip.jpg')); - // Flip the image from left to right - //$image->flip(Phalcon\Image::VERTICAL); + // Flip the image from left to right + //$image->flip(Phalcon\Image::VERTICAL); - // Sharpen the image by 20% - $image->sharpen(20)->save('unit-tests/assets/production/imagick-sharpen.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-sharpen.jpg')); + // Sharpen the image by 20% + $image->sharpen(20)->save('unit-tests/assets/production/imagick-sharpen.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-sharpen.jpg')); - // Create a 50 pixel reflection that fades from 0-100% opacity - $image->reflection(50)->save('unit-tests/assets/production/imagick-reflection.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-reflection.jpg')); + // Create a 50 pixel reflection that fades from 0-100% opacity + $image->reflection(50)->save('unit-tests/assets/production/imagick-reflection.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-reflection.jpg')); - // Create a 50 pixel reflection that fades from 100-0% opacity - //$image->reflection(50, 100, TRUE)->save('reflection.jpg'); + // Create a 50 pixel reflection that fades from 100-0% opacity + //$image->reflection(50, 100, TRUE)->save('reflection.jpg'); - // Create a 50 pixel reflection that fades from 0-60% opacity - //$image->reflection(50, 60, TRUE); + // Create a 50 pixel reflection that fades from 0-60% opacity + //$image->reflection(50, 60, TRUE); - // Add a watermark to the bottom right of the image - $mark = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/logo.png'); - $image->watermark($mark, TRUE, TRUE)->save('unit-tests/assets/production/imagick-watermark.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-watermark.jpg')); + // Add a watermark to the bottom right of the image + $mark = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/logo.png'); + $image->watermark($mark, TRUE, TRUE)->save('unit-tests/assets/production/imagick-watermark.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-watermark.jpg')); - // Mask image - $mask = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/logo.png'); - $image->mask($mask)->save('unit-tests/assets/production/imagick-mask.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-mask.jpg')); + // Mask image + $mask = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/logo.png'); + $image->mask($mask)->save('unit-tests/assets/production/imagick-mask.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-mask.jpg')); - // Add a text to the bottom right of the image - $image->text('hello', TRUE, TRUE); + // Add a text to the bottom right of the image + $image->text('hello', TRUE, TRUE); - // Set font size - //$image->text('hello', TRUE, TRUE, NULL, NULL, 12); + // Set font size + //$image->text('hello', TRUE, TRUE, NULL, NULL, 12); - // Set font - //$image->text('hello', TRUE, TRUE, NULL, NULL, 12, /usr/share/fonts/truetype/wqy/wqy-microhei.ttc); + // Set font + //$image->text('hello', TRUE, TRUE, NULL, NULL, 12, /usr/share/fonts/truetype/wqy/wqy-microhei.ttc); - // Add a text to the center of the image - //$image->text('hello'); + // Add a text to the center of the image + //$image->text('hello'); - // Make the image background black - $mark->background('#000')->save('unit-tests/assets/production/imagick-background.jpg'); - $this->assertTrue(file_exists('unit-tests/assets/production/imagick-background.jpg')); + // Make the image background black + $mark->background('#000')->save('unit-tests/assets/production/imagick-background.jpg'); + $this->assertTrue(file_exists('unit-tests/assets/production/imagick-background.jpg')); - // Make the image background black with 50% opacity - //$image->background('#000', 50); + // Make the image background black with 50% opacity + //$image->background('#000', 50); - // Save the image as a PNG - //$image->save('saved/gd.png'); + // Save the image as a PNG + //$image->save('saved/gd.png'); - // Overwrite the original image - //$image->save(); + // Overwrite the original image + //$image->save(); - // Render the image at 50% quality - //$data = $image->render(NULL, 50); - - // Render the image as a PNG - //$data = $image->render('png'); - } catch (Exception $e) { - $this->assertTrue(FALSE); - } + // Render the image at 50% quality + //$data = $image->render(NULL, 50); + + // Render the image as a PNG + //$data = $image->render('png'); } } \ No newline at end of file From 3a5aff682c77fb90625c20189843255876c344f6 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Wed, 11 Sep 2013 08:07:26 +0800 Subject: [PATCH 05/14] Fix by @sjinks --- ext/image/adapter.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/image/adapter.c b/ext/image/adapter.c index ec1267fa8ca..11e1611ea41 100644 --- a/ext/image/adapter.c +++ b/ext/image/adapter.c @@ -266,10 +266,10 @@ PHP_METHOD(Phalcon_Image_Adapter, resize){ tmp_height = 1; } - PHALCON_INIT_NVAR(width); + zval_dtor(width); ZVAL_LONG(width, tmp_width); - PHALCON_INIT_NVAR(height); + zval_dtor(height); ZVAL_LONG(height, tmp_height); } From 586d3f559203050da4f110afee893b6d3ab014ce Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Wed, 11 Sep 2013 09:36:02 +0800 Subject: [PATCH 06/14] use zval_dtor --- ext/image/adapter.c | 66 +++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/ext/image/adapter.c b/ext/image/adapter.c index 11e1611ea41..c225d1ee2ee 100644 --- a/ext/image/adapter.c +++ b/ext/image/adapter.c @@ -416,16 +416,16 @@ PHP_METHOD(Phalcon_Image_Adapter, crop){ tmp_height = tmp_max_height; } - PHALCON_INIT_NVAR(width); + zval_dtor(width); ZVAL_LONG(width, tmp_width); - PHALCON_INIT_NVAR(height); + zval_dtor(height); ZVAL_LONG(height, tmp_height); - PHALCON_INIT_NVAR(offset_x); + zval_dtor(offset_x); ZVAL_LONG(offset_x, tmp_offset_x); - PHALCON_INIT_NVAR(offset_y); + zval_dtor(offset_y); ZVAL_LONG(offset_y, tmp_offset_y); phalcon_call_method_p4_noret(this_ptr, "_crop", width, height, offset_x, offset_y); @@ -467,7 +467,7 @@ PHP_METHOD(Phalcon_Image_Adapter, rotate){ } while (tmp_degrees < -180); } - PHALCON_INIT_NVAR(degrees); + zval_dtor(degrees); ZVAL_LONG(degrees, tmp_degrees); phalcon_call_method_p1_noret(this_ptr, "_rotate", degrees); @@ -565,7 +565,7 @@ PHP_METHOD(Phalcon_Image_Adapter, reflection){ } else if (Z_TYPE_P(height) != IS_LONG || Z_LVAL_P(height) > tmp_image_height) { PHALCON_SEPARATE_PARAM(height); - PHALCON_INIT_NVAR(height); + zval_dtor(height); ZVAL_LONG(height, tmp_image_height); } @@ -574,12 +574,13 @@ PHP_METHOD(Phalcon_Image_Adapter, reflection){ ZVAL_LONG(opacity, 100); } else { PHALCON_SEPARATE_PARAM(opacity); + convert_to_long(opacity); - if (Z_TYPE_P(opacity) != IS_LONG || Z_LVAL_P(opacity) > 100) { - PHALCON_INIT_NVAR(opacity); + if (Z_LVAL_P(opacity) > 100) { + zval_dtor(opacity); ZVAL_LONG(opacity, 100); } else if (Z_LVAL_P(opacity) < 0) { - PHALCON_INIT_NVAR(opacity); + zval_dtor(opacity); ZVAL_LONG(opacity, 0); } } @@ -634,7 +635,7 @@ PHP_METHOD(Phalcon_Image_Adapter, watermark){ } else { PHALCON_SEPARATE_PARAM(offset_x); if (Z_TYPE_P(offset_x) == IS_LONG) { - tmp_offset_x = phalcon_get_intval(offset_x); + tmp_offset_x = Z_LVAL_P(offset_x); if (tmp_offset_x < 0) { tmp_offset_x = (int)(tmp_image_width - tmp_watermark_width + tmp_offset_x + 0.5); } @@ -645,7 +646,7 @@ PHP_METHOD(Phalcon_Image_Adapter, watermark){ } } - PHALCON_INIT_NVAR(offset_x); + zval_dtor(offset_x); ZVAL_LONG(offset_x, tmp_offset_x); if (!offset_y) { @@ -653,7 +654,7 @@ PHP_METHOD(Phalcon_Image_Adapter, watermark){ } else { PHALCON_SEPARATE_PARAM(offset_y); if (Z_TYPE_P(offset_y) == IS_LONG) { - tmp_offset_y = phalcon_get_intval(offset_y); + tmp_offset_y = Z_LVAL_P(offset_y); if (tmp_offset_y < 0) { tmp_offset_y = (int)(tmp_image_height - tmp_watermark_height + tmp_offset_y + 0.5); } @@ -664,7 +665,7 @@ PHP_METHOD(Phalcon_Image_Adapter, watermark){ } } - PHALCON_INIT_NVAR(offset_y); + zval_dtor(offset_y); ZVAL_LONG(offset_y, tmp_offset_y); if (!opacity) { @@ -673,7 +674,7 @@ PHP_METHOD(Phalcon_Image_Adapter, watermark){ } else { PHALCON_SEPARATE_PARAM(opacity); - PHALCON_INIT_NVAR(opacity); + zval_dtor(opacity); if (phalcon_get_intval(opacity) < 1) { ZVAL_LONG(opacity, 1); } else if (phalcon_get_intval(opacity) > 100) { @@ -723,12 +724,12 @@ PHP_METHOD(Phalcon_Image_Adapter, text){ } if (Z_TYPE_P(offset_x) == IS_NULL ) { - PHALCON_INIT_NVAR(offset_x); + zval_dtor(offset_x); ZVAL_BOOL(offset_x, 0); } if (Z_TYPE_P(offset_y) == IS_NULL) { - PHALCON_INIT_NVAR(offset_y); + zval_dtor(offset_y); ZVAL_BOOL(offset_y, 0); } @@ -737,18 +738,18 @@ PHP_METHOD(Phalcon_Image_Adapter, text){ ZVAL_LONG(opacity, 100); } else if (Z_TYPE_P(opacity) == IS_NULL) { PHALCON_SEPARATE_PARAM(opacity); - PHALCON_INIT_NVAR(opacity); + zval_dtor(opacity); ZVAL_LONG(opacity, 100); } else { i = phalcon_get_intval(opacity); if (i < 1) { PHALCON_SEPARATE_PARAM(opacity); - PHALCON_INIT_NVAR(opacity); + zval_dtor(opacity); ZVAL_LONG(opacity, 1); } else if (i > 100) { PHALCON_SEPARATE_PARAM(opacity); - PHALCON_INIT_NVAR(opacity); + zval_dtor(opacity); ZVAL_LONG(opacity, 100); } } @@ -758,7 +759,7 @@ PHP_METHOD(Phalcon_Image_Adapter, text){ ZVAL_STRING(color, "#000000", 1); } else if (Z_TYPE_P(color) == IS_NULL) { PHALCON_SEPARATE_PARAM(color); - PHALCON_INIT_NVAR(color); + zval_dtor(color); ZVAL_STRING(color, "#000000", 1); } @@ -767,7 +768,7 @@ PHP_METHOD(Phalcon_Image_Adapter, text){ ZVAL_LONG(size, 12); } else if (Z_TYPE_P(size) == IS_NULL) { PHALCON_SEPARATE_PARAM(size); - PHALCON_INIT_NVAR(size); + zval_dtor(size); ZVAL_LONG(size, 12); } @@ -922,10 +923,10 @@ PHP_METHOD(Phalcon_Image_Adapter, background){ i = phalcon_get_intval(opacity); if (i < 1) { - PHALCON_INIT_NVAR(opacity); + zval_dtor(opacity); ZVAL_LONG(opacity, 1); } else if (i > 100) { - PHALCON_INIT_NVAR(opacity); + zval_dtor(opacity); ZVAL_LONG(opacity, 100); } } @@ -956,17 +957,17 @@ PHP_METHOD(Phalcon_Image_Adapter, blur){ } else if (Z_TYPE_P(radius) != IS_LONG) { PHALCON_SEPARATE_PARAM(radius); - PHALCON_INIT_NVAR(radius); + zval_dtor(radius); ZVAL_LONG(radius, 1); } else { r = phalcon_get_intval(radius); if (r < 1) { PHALCON_SEPARATE_PARAM(radius); - PHALCON_INIT_NVAR(radius); + zval_dtor(radius); ZVAL_LONG(radius, 1); } else if (r > 100) { PHALCON_SEPARATE_PARAM(radius); - PHALCON_INIT_NVAR(radius); + zval_dtor(radius); ZVAL_LONG(radius, 100); } } @@ -995,12 +996,11 @@ PHP_METHOD(Phalcon_Image_Adapter, pixelate){ ZVAL_LONG(amount, 10); } else if (Z_TYPE_P(amount) != IS_LONG) { PHALCON_SEPARATE_PARAM(amount); - - PHALCON_INIT_NVAR(amount); + zval_dtor(amount); ZVAL_LONG(amount, 10); } else if (phalcon_get_intval(amount) < 2) { PHALCON_SEPARATE_PARAM(amount); - PHALCON_INIT_NVAR(amount); + zval_dtor(amount); ZVAL_LONG(amount, 2); } @@ -1036,8 +1036,7 @@ PHP_METHOD(Phalcon_Image_Adapter, save){ ZVAL_LONG(quality, 100); } else if (Z_TYPE_P(quality) != IS_LONG) { PHALCON_SEPARATE_PARAM(quality); - - PHALCON_INIT_NVAR(quality); + zval_dtor(quality); ZVAL_LONG(quality, 100); } else { @@ -1128,9 +1127,12 @@ PHP_METHOD(Phalcon_Image_Adapter, render){ } } - if (!quality || Z_TYPE_P(quality) != IS_LONG) { + if (!quality) { PHALCON_INIT_VAR(quality); ZVAL_LONG(quality, 100); + } else if (Z_TYPE_P(quality) != IS_LONG) { + PHALCON_SEPARATE_PARAM(quality); + convert_to_long(quality); } phalcon_call_method_p2(return_value, this_ptr, "_render", ext, quality); From f2af4856dae6c2050cecc428514d1a57a9d2bf58 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Wed, 11 Sep 2013 10:32:56 +0800 Subject: [PATCH 07/14] Updated --- ext/image/adapter.c | 2 ++ ext/image/adapter/gd.c | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ext/image/adapter.c b/ext/image/adapter.c index c225d1ee2ee..e3a5aacd622 100644 --- a/ext/image/adapter.c +++ b/ext/image/adapter.c @@ -374,6 +374,7 @@ PHP_METHOD(Phalcon_Image_Adapter, crop){ } if (!offset_x) { + PHALCON_INIT_VAR(offset_x); tmp_offset_x = (int)(((tmp_image_width - tmp_width) / 2) + 0.5); } else { PHALCON_SEPARATE_PARAM(offset_x); @@ -390,6 +391,7 @@ PHP_METHOD(Phalcon_Image_Adapter, crop){ } if (!offset_y) { + PHALCON_INIT_VAR(offset_y); tmp_offset_y = (int)(((tmp_image_height - tmp_height) / 2) + 0.5); } else { PHALCON_SEPARATE_PARAM(offset_y); diff --git a/ext/image/adapter/gd.c b/ext/image/adapter/gd.c index 33026367101..8da525c178f 100644 --- a/ext/image/adapter/gd.c +++ b/ext/image/adapter/gd.c @@ -364,7 +364,7 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _resize) { } #else PHALCON_OBS_NVAR(tmp_image); - PHALCON_CALL_FUNCTION(tmp_image, &tmp_image, "imagescale", 3, tmp_image, width, height); + PHALCON_CALL_FUNCTION(tmp_image, &tmp_image, "imagescale", 3, image, width, height); phalcon_call_func_p1_noret("imagedestroy", image); phalcon_update_property_this(this_ptr, SL("_image"), tmp_image TSRMLS_CC); @@ -443,10 +443,10 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _crop) { #else PHALCON_INIT_VAR(rect); array_init_size(rect, 4); - phalcon_array_append(&rect, offset_x, 0); - phalcon_array_append(&rect, offset_y, 0); - phalcon_array_append(&rect, width, 0); - phalcon_array_append(&rect, height, 0); + phalcon_array_update_string(&rect, SL("x"), offset_x, 0); + phalcon_array_update_string(&rect, SL("y"), offset_y, 0); + phalcon_array_update_string(&rect, SL("width"), width, 0); + phalcon_array_update_string(&rect, SL("height"), height, 0); PHALCON_OBS_VAR(tmp_image); PHALCON_CALL_FUNCTION(tmp_image, &tmp_image, "imagecrop", 2, image, rect); From f97f7e1ef3ccf51e53b18a275f826cac414c4000 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Wed, 11 Sep 2013 10:38:58 +0800 Subject: [PATCH 08/14] Updated --- ext/image/adapter/gd.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ext/image/adapter/gd.c b/ext/image/adapter/gd.c index 8da525c178f..04d5c25433d 100644 --- a/ext/image/adapter/gd.c +++ b/ext/image/adapter/gd.c @@ -443,12 +443,12 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _crop) { #else PHALCON_INIT_VAR(rect); array_init_size(rect, 4); - phalcon_array_update_string(&rect, SL("x"), offset_x, 0); - phalcon_array_update_string(&rect, SL("y"), offset_y, 0); - phalcon_array_update_string(&rect, SL("width"), width, 0); - phalcon_array_update_string(&rect, SL("height"), height, 0); + phalcon_array_update_string(&rect, SL("x"), &offset_x, PH_COPY); + phalcon_array_update_string(&rect, SL("y"), &offset_y, PH_COPY); + phalcon_array_update_string(&rect, SL("width"), &width, PH_COPY); + phalcon_array_update_string(&rect, SL("height"), &height, PH_COPY); - PHALCON_OBS_VAR(tmp_image); + PHALCON_INIT_VAR(tmp_image); PHALCON_CALL_FUNCTION(tmp_image, &tmp_image, "imagecrop", 2, image, rect); #endif From 8a24e4e96d32cbac9eef11a85ae75ed84f6b489a Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Wed, 11 Sep 2013 10:41:12 +0800 Subject: [PATCH 09/14] Updated --- ext/image/adapter/gd.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ext/image/adapter/gd.c b/ext/image/adapter/gd.c index 04d5c25433d..45b1952154a 100644 --- a/ext/image/adapter/gd.c +++ b/ext/image/adapter/gd.c @@ -450,6 +450,12 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _crop) { PHALCON_INIT_VAR(tmp_image); PHALCON_CALL_FUNCTION(tmp_image, &tmp_image, "imagecrop", 2, image, rect); + + phalcon_call_func_p1_noret("imagedestroy", image); + phalcon_update_property_this(this_ptr, SL("_image"), tmp_image TSRMLS_CC); + + phalcon_update_property_this(this_ptr, SL("_width"), width TSRMLS_CC); + phalcon_update_property_this(this_ptr, SL("_height"), height TSRMLS_CC); #endif PHALCON_MM_RESTORE(); From 6efdefe8ff53d57ea2d2ed958d7b65eb7d3bae50 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Wed, 11 Sep 2013 10:46:49 +0800 Subject: [PATCH 10/14] Updated --- ext/image/adapter/gd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/image/adapter/gd.c b/ext/image/adapter/gd.c index 45b1952154a..22d591040c9 100644 --- a/ext/image/adapter/gd.c +++ b/ext/image/adapter/gd.c @@ -613,7 +613,7 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _flip) { RETURN_MM(); } } - PHALCON_OBS_VAR(flipped_image); + PHALCON_INIT_VAR(flipped_image); PHALCON_CALL_FUNCTION(flipped_image, &flipped_image, "imageflip", 2, image, mode); #endif From 46d93e5bb3dbee24dc85bc44051732928bfdd2ea Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Wed, 11 Sep 2013 10:55:12 +0800 Subject: [PATCH 11/14] fix imagefip --- ext/image/adapter/gd.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ext/image/adapter/gd.c b/ext/image/adapter/gd.c index 22d591040c9..e9535240a94 100644 --- a/ext/image/adapter/gd.c +++ b/ext/image/adapter/gd.c @@ -448,7 +448,7 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _crop) { phalcon_array_update_string(&rect, SL("width"), &width, PH_COPY); phalcon_array_update_string(&rect, SL("height"), &height, PH_COPY); - PHALCON_INIT_VAR(tmp_image); + PHALCON_OBS_VAR(tmp_image); PHALCON_CALL_FUNCTION(tmp_image, &tmp_image, "imagecrop", 2, image, rect); phalcon_call_func_p1_noret("imagedestroy", image); @@ -530,8 +530,9 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _rotate) { PHP_METHOD(Phalcon_Image_Adapter_GD, _flip) { zval *direction; - zval *image = NULL, *flipped_image; + zval *image = NULL; #if PHP_VERSION_ID < 50500 + zval *flipped_image; zval *width, *height; zval *dst_x = NULL, *dst_y = NULL, *src_x = NULL, *src_y = NULL, *src_width = NULL, *src_height = NULL; int w, h, x, y; @@ -602,6 +603,9 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _flip) { PHALCON_CALL_FUNCTION(NULL, NULL, "imagecopy", 8, flipped_image, image, dst_x, dst_y, src_x, src_y, src_width, src_height); } } + + phalcon_call_func_p1_noret("imagedestroy", image); + phalcon_update_property_this(this_ptr, SL("_image"), flipped_image TSRMLS_CC); #else PHALCON_INIT_VAR(mode); if (Z_LVAL_P(direction) == PHALCON_IMAGE_HORIZONTAL) { @@ -613,12 +617,9 @@ PHP_METHOD(Phalcon_Image_Adapter_GD, _flip) { RETURN_MM(); } } - PHALCON_INIT_VAR(flipped_image); - PHALCON_CALL_FUNCTION(flipped_image, &flipped_image, "imageflip", 2, image, mode); -#endif - phalcon_call_func_p1_noret("imagedestroy", image); - phalcon_update_property_this(this_ptr, SL("_image"), flipped_image TSRMLS_CC); + PHALCON_CALL_FUNCTION(NULL, NULL, "imageflip", 2, image, mode); +#endif PHALCON_MM_RESTORE(); } From 9d0f3fc7c07193fe10cc25c8a54440e14c5013c7 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Thu, 12 Sep 2013 15:19:33 +0800 Subject: [PATCH 12/14] Reuse PHALCON_INIT_NVAR replace zval_dtor --- ext/image/adapter.c | 56 ++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/ext/image/adapter.c b/ext/image/adapter.c index e3a5aacd622..746c13d1c84 100644 --- a/ext/image/adapter.c +++ b/ext/image/adapter.c @@ -266,10 +266,10 @@ PHP_METHOD(Phalcon_Image_Adapter, resize){ tmp_height = 1; } - zval_dtor(width); + PHALCON_INIT_NVAR(width); ZVAL_LONG(width, tmp_width); - zval_dtor(height); + PHALCON_INIT_NVAR(height); ZVAL_LONG(height, tmp_height); } @@ -418,16 +418,16 @@ PHP_METHOD(Phalcon_Image_Adapter, crop){ tmp_height = tmp_max_height; } - zval_dtor(width); + PHALCON_INIT_NVAR(width); ZVAL_LONG(width, tmp_width); - zval_dtor(height); + PHALCON_INIT_NVAR(height); ZVAL_LONG(height, tmp_height); - zval_dtor(offset_x); + PHALCON_INIT_NVAR(offset_x); ZVAL_LONG(offset_x, tmp_offset_x); - zval_dtor(offset_y); + PHALCON_INIT_NVAR(offset_y); ZVAL_LONG(offset_y, tmp_offset_y); phalcon_call_method_p4_noret(this_ptr, "_crop", width, height, offset_x, offset_y); @@ -469,7 +469,7 @@ PHP_METHOD(Phalcon_Image_Adapter, rotate){ } while (tmp_degrees < -180); } - zval_dtor(degrees); + PHALCON_INIT_NVAR(degrees); ZVAL_LONG(degrees, tmp_degrees); phalcon_call_method_p1_noret(this_ptr, "_rotate", degrees); @@ -567,7 +567,7 @@ PHP_METHOD(Phalcon_Image_Adapter, reflection){ } else if (Z_TYPE_P(height) != IS_LONG || Z_LVAL_P(height) > tmp_image_height) { PHALCON_SEPARATE_PARAM(height); - zval_dtor(height); + PHALCON_INIT_NVAR(height); ZVAL_LONG(height, tmp_image_height); } @@ -579,10 +579,10 @@ PHP_METHOD(Phalcon_Image_Adapter, reflection){ convert_to_long(opacity); if (Z_LVAL_P(opacity) > 100) { - zval_dtor(opacity); + PHALCON_INIT_NVAR(opacity); ZVAL_LONG(opacity, 100); } else if (Z_LVAL_P(opacity) < 0) { - zval_dtor(opacity); + PHALCON_INIT_NVAR(opacity); ZVAL_LONG(opacity, 0); } } @@ -648,7 +648,7 @@ PHP_METHOD(Phalcon_Image_Adapter, watermark){ } } - zval_dtor(offset_x); + PHALCON_INIT_NVAR(offset_x); ZVAL_LONG(offset_x, tmp_offset_x); if (!offset_y) { @@ -667,7 +667,7 @@ PHP_METHOD(Phalcon_Image_Adapter, watermark){ } } - zval_dtor(offset_y); + PHALCON_INIT_NVAR(offset_y); ZVAL_LONG(offset_y, tmp_offset_y); if (!opacity) { @@ -676,7 +676,7 @@ PHP_METHOD(Phalcon_Image_Adapter, watermark){ } else { PHALCON_SEPARATE_PARAM(opacity); - zval_dtor(opacity); + PHALCON_INIT_NVAR(opacity); if (phalcon_get_intval(opacity) < 1) { ZVAL_LONG(opacity, 1); } else if (phalcon_get_intval(opacity) > 100) { @@ -726,12 +726,12 @@ PHP_METHOD(Phalcon_Image_Adapter, text){ } if (Z_TYPE_P(offset_x) == IS_NULL ) { - zval_dtor(offset_x); + PHALCON_INIT_NVAR(offset_x); ZVAL_BOOL(offset_x, 0); } if (Z_TYPE_P(offset_y) == IS_NULL) { - zval_dtor(offset_y); + PHALCON_INIT_NVAR(offset_y); ZVAL_BOOL(offset_y, 0); } @@ -740,18 +740,18 @@ PHP_METHOD(Phalcon_Image_Adapter, text){ ZVAL_LONG(opacity, 100); } else if (Z_TYPE_P(opacity) == IS_NULL) { PHALCON_SEPARATE_PARAM(opacity); - zval_dtor(opacity); + PHALCON_INIT_NVAR(opacity); ZVAL_LONG(opacity, 100); } else { i = phalcon_get_intval(opacity); if (i < 1) { PHALCON_SEPARATE_PARAM(opacity); - zval_dtor(opacity); + PHALCON_INIT_NVAR(opacity); ZVAL_LONG(opacity, 1); } else if (i > 100) { PHALCON_SEPARATE_PARAM(opacity); - zval_dtor(opacity); + PHALCON_INIT_NVAR(opacity); ZVAL_LONG(opacity, 100); } } @@ -761,7 +761,7 @@ PHP_METHOD(Phalcon_Image_Adapter, text){ ZVAL_STRING(color, "#000000", 1); } else if (Z_TYPE_P(color) == IS_NULL) { PHALCON_SEPARATE_PARAM(color); - zval_dtor(color); + PHALCON_INIT_NVAR(color); ZVAL_STRING(color, "#000000", 1); } @@ -770,7 +770,7 @@ PHP_METHOD(Phalcon_Image_Adapter, text){ ZVAL_LONG(size, 12); } else if (Z_TYPE_P(size) == IS_NULL) { PHALCON_SEPARATE_PARAM(size); - zval_dtor(size); + PHALCON_INIT_NVAR(size); ZVAL_LONG(size, 12); } @@ -925,10 +925,10 @@ PHP_METHOD(Phalcon_Image_Adapter, background){ i = phalcon_get_intval(opacity); if (i < 1) { - zval_dtor(opacity); + PHALCON_INIT_NVAR(opacity); ZVAL_LONG(opacity, 1); } else if (i > 100) { - zval_dtor(opacity); + PHALCON_INIT_NVAR(opacity); ZVAL_LONG(opacity, 100); } } @@ -959,17 +959,17 @@ PHP_METHOD(Phalcon_Image_Adapter, blur){ } else if (Z_TYPE_P(radius) != IS_LONG) { PHALCON_SEPARATE_PARAM(radius); - zval_dtor(radius); + PHALCON_INIT_NVAR(radius); ZVAL_LONG(radius, 1); } else { r = phalcon_get_intval(radius); if (r < 1) { PHALCON_SEPARATE_PARAM(radius); - zval_dtor(radius); + PHALCON_INIT_NVAR(radius); ZVAL_LONG(radius, 1); } else if (r > 100) { PHALCON_SEPARATE_PARAM(radius); - zval_dtor(radius); + PHALCON_INIT_NVAR(radius); ZVAL_LONG(radius, 100); } } @@ -998,11 +998,11 @@ PHP_METHOD(Phalcon_Image_Adapter, pixelate){ ZVAL_LONG(amount, 10); } else if (Z_TYPE_P(amount) != IS_LONG) { PHALCON_SEPARATE_PARAM(amount); - zval_dtor(amount); + PHALCON_INIT_NVAR(amount); ZVAL_LONG(amount, 10); } else if (phalcon_get_intval(amount) < 2) { PHALCON_SEPARATE_PARAM(amount); - zval_dtor(amount); + PHALCON_INIT_NVAR(amount); ZVAL_LONG(amount, 2); } @@ -1038,7 +1038,7 @@ PHP_METHOD(Phalcon_Image_Adapter, save){ ZVAL_LONG(quality, 100); } else if (Z_TYPE_P(quality) != IS_LONG) { PHALCON_SEPARATE_PARAM(quality); - zval_dtor(quality); + PHALCON_INIT_NVAR(quality); ZVAL_LONG(quality, 100); } else { From d0341524d425f8a6369c2ec43ae7d00c4070b5a3 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Thu, 12 Sep 2013 17:17:46 +0800 Subject: [PATCH 13/14] Update the attributes width and height for crop --- ext/image/adapter/imagick.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ext/image/adapter/imagick.c b/ext/image/adapter/imagick.c index f50bfcaf2e1..27c7db266ab 100644 --- a/ext/image/adapter/imagick.c +++ b/ext/image/adapter/imagick.c @@ -369,7 +369,7 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _liquidRescale){ */ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _crop) { - zval *width, *height, *offset_x, *offset_y; + zval *width, *height, *offset_x, *offset_y, *w, *h; zval *im, *ret = NULL, *index, *next = NULL, *type, *tmp; PHALCON_MM_GROW(); @@ -417,6 +417,15 @@ PHP_METHOD(Phalcon_Image_Adapter_Imagick, _crop) { phalcon_call_method_p4_noret(im, "setImagePage", width, height, tmp, tmp); } + PHALCON_INIT_VAR(w); + phalcon_call_method(w, im, "getImageWidth"); + + PHALCON_INIT_VAR(h); + phalcon_call_method(h, im, "getImageHeight"); + + phalcon_update_property_this(this_ptr, SL("_width"), w TSRMLS_CC); + phalcon_update_property_this(this_ptr, SL("_height"), h TSRMLS_CC); + PHALCON_MM_RESTORE(); } From 05e44109a495ddf8170098ac812dc7dbf00b49be Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Thu, 12 Sep 2013 17:18:03 +0800 Subject: [PATCH 14/14] Updated ImageTest --- unit-tests/ImageTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/unit-tests/ImageTest.php b/unit-tests/ImageTest.php index 90951f8ea84..114bf7a07eb 100644 --- a/unit-tests/ImageTest.php +++ b/unit-tests/ImageTest.php @@ -55,6 +55,9 @@ public function testGD() $this->assertTrue($width <= 200); $this->assertTrue($height <= 200); + $this->assertTrue($image->getWidth() <= 200); + $this->assertTrue($image->getHeight() <= 200); + // Resize to 200x200 pixels, keeping aspect ratio //$image->resize(200, 200, Phalcon\Image::INVERSE); @@ -78,11 +81,17 @@ public function testGD() $this->assertEquals($width, 200); $this->assertEquals($height, 200); + $this->assertTrue($image->getWidth() == 200); + $this->assertTrue($image->getHeight() == 200); + // Rotate 45 degrees clockwise $image->rotate(45)->save('unit-tests/assets/production/gd-rotate.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/gd-rotate.jpg')); + $this->assertTrue($image->getWidth() > 200); + $this->assertTrue($image->getHeight() > 200); + // Rotate 90% counter-clockwise //$image->rotate(-90); @@ -177,6 +186,8 @@ public function testImagick() // Resize to 200 pixels on the shortest side $image->resize(200, 200)->save('unit-tests/assets/production/imagick-resize.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/imagick-resize.jpg')); + $this->assertTrue($image->getWidth() <= 200); + $this->assertTrue($image->getHeight() <= 200); // Resize to 200x200 pixels, keeping aspect ratio //$image->resize(200, 200, Phalcon\Image::INVERSE); @@ -193,19 +204,28 @@ public function testImagick() // The images using liquid rescaling resize to 200x200 $image->liquidRescale(200, 200)->save('unit-tests/assets/production/imagick-liquidRescale.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/imagick-liquidRescale.jpg')); + $this->assertTrue($image->getWidth() == 200); + $this->assertTrue($image->getHeight() == 200); // The images using liquid rescaling resize to 500x500 //$image->liquidRescale(500, 500, 3, 25); // Crop the image to 200x200 pixels, from the center + + $image = new Phalcon\Image\Adapter\Imagick('unit-tests/assets/phalconphp.jpg'); $image->crop(200, 200)->save('unit-tests/assets/production/imagick-crop.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/imagick-crop.jpg')); + $this->assertTrue($image->getWidth() == 200); + $this->assertTrue($image->getHeight() == 200); // Rotate 45 degrees clockwise $image->rotate(45)->save('unit-tests/assets/production/imagick-rotate.jpg'); $this->assertTrue(file_exists('unit-tests/assets/production/imagick-rotate.jpg')); + $this->assertTrue($image->getWidth() > 200); + $this->assertTrue($image->getHeight() > 200); + // Rotate 90% counter-clockwise //$image->rotate(-90);