]> granicus.if.org Git - php/commitdiff
Preserve aspect ratio for width or height
authorAndreas Treichel <gmblar@gmail.com>
Sat, 29 Dec 2018 18:41:57 +0000 (19:41 +0100)
committerChristoph M. Becker <cmbecker69@gmx.de>
Sat, 5 Jan 2019 12:39:49 +0000 (13:39 +0100)
NEWS
UPGRADING
ext/gd/gd.c
ext/gd/tests/imagescale_preserve_ratio.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index b27f23027e9a61f3db365de536f9a77a62a4cdb7..df7bc70af7995417e31add06022c342574a77a52 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -25,6 +25,8 @@ PHP                                                                        NEWS
     falling back to IMG_CROP_SIDES.
   . The default $mode parameter of imagecropauto() has been changed to
     IMG_CROP_DEFAULT; passing -1 is now deprecated.
+  . Added support for aspect ratio preserving scaling to a fixed height for
+    imagescale(). (Andreas Treichel)
 
 - Hash:
   . The hash extension is now an integral part of PHP and cannot be disabled
index ce157fc702da44db64109aaa452a0e4a80ee1b09..2da9ad242b9bd22d72308d94d668268b173732f2 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -145,6 +145,8 @@ PHP 7.4 UPGRADE NOTES
      * Threshold-cropping now uses the algorithm of system libgd
   . The default $mode parameter of imagecropauto() has been changed to
     IMG_CROP_DEFAULT; passing -1 is now deprecated.
+  . imagescale() now supports aspect ratio preserving scaling to a fixed height
+    by passing -1 as $new_width.
 
 - Hash:
   . The hash extension cannot be disabled anymore and is always an integral
index f4b7d2c2a5bd164a7db6065926096de25fa9cec4..d4e4db1be6bdd5937d7e5a747a1f191a0f4bbc52 100644 (file)
@@ -4757,15 +4757,19 @@ PHP_FUNCTION(imagescale)
                RETURN_FALSE;
        }
 
-       if (tmp_h < 0) {
+       if (tmp_h < 0 || tmp_w < 0) {
                /* preserve ratio */
                long src_x, src_y;
 
                src_x = gdImageSX(im);
                src_y = gdImageSY(im);
-               if (src_x) {
+
+               if (src_x && tmp_h < 0) {
                        tmp_h = tmp_w * src_y / src_x;
                }
+               if (src_y && tmp_w < 0) {
+                       tmp_w = tmp_h * src_x / src_y;
+               }
        }
 
        if (tmp_h <= 0 || tmp_h > INT_MAX || tmp_w <= 0 || tmp_w > INT_MAX) {
diff --git a/ext/gd/tests/imagescale_preserve_ratio.phpt b/ext/gd/tests/imagescale_preserve_ratio.phpt
new file mode 100644 (file)
index 0000000..00076e4
--- /dev/null
@@ -0,0 +1,55 @@
+--TEST--
+Scale images and preserve aspect ratio
+--SKIPIF--
+<?php
+if (!extension_loaded('gd')) die("skip gd extension not available\n");
+?>
+--FILE--
+<?php
+
+$img = imagecreatetruecolor ( 256, 384);
+
+$thumbnail = imagescale($img, 64, -1, IMG_BICUBIC);
+var_dump(imagesx($thumbnail));
+var_dump(imagesy($thumbnail));
+
+$thumbnail = imagescale($img, -1, 64, IMG_BICUBIC);
+var_dump(imagesx($thumbnail));
+var_dump(imagesy($thumbnail));
+
+$img = imagecreatetruecolor ( 384, 256);
+
+$thumbnail = imagescale($img, 64, -1, IMG_BICUBIC);
+var_dump(imagesx($thumbnail));
+var_dump(imagesy($thumbnail));
+
+$thumbnail = imagescale($img, -1, 64, IMG_BICUBIC);
+var_dump(imagesx($thumbnail));
+var_dump(imagesy($thumbnail));
+
+$img = imagecreatetruecolor ( 256, 256);
+
+$thumbnail = imagescale($img, 64, -1, IMG_BICUBIC);
+var_dump(imagesx($thumbnail));
+var_dump(imagesy($thumbnail));
+
+$thumbnail = imagescale($img, -1, 64, IMG_BICUBIC);
+var_dump(imagesx($thumbnail));
+var_dump(imagesy($thumbnail));
+
+?>
+DONE
+--EXPECT--
+int(64)
+int(96)
+int(42)
+int(64)
+int(64)
+int(42)
+int(96)
+int(64)
+int(64)
+int(64)
+int(64)
+int(64)
+DONE