Skip to content

Instantly share code, notes, and snippets.

@mhsenpc
Created September 30, 2020 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhsenpc/69850ec6ba875e545907564a00c060f6 to your computer and use it in GitHub Desktop.
Save mhsenpc/69850ec6ba875e545907564a00c060f6 to your computer and use it in GitHub Desktop.
Calculate Image ratio function for MySql and MariaDB
DELIMITER $$
DROP FUNCTION IF EXISTS calc_ratio;
CREATE FUNCTION calc_ratio(width int,
height int)
RETURNS VARCHAR(10)
DETERMINISTIC
BEGIN
DECLARE ratio VARCHAR(10);
declare tmp1 float;
declare tmp2 float;
declare i int unsigned default 0;
declare mywidth int unsigned default 0;
declare myheight int unsigned default 0;
set mywidth = width;
set myheight = height;
set i = height;
while i > 1
do
set tmp1 = mod(mywidth, i);
set tmp2 = mod(myheight, i);
if tmp1 = 0 && tmp2 = 0 then
set mywidth = mywidth / i;
set myheight = myheight / i;
end if;
set i = i - 1;
end while;
set ratio = concat(mywidth, ':', myheight);
return ratio;
END$$
DELIMITER ;
@mhsenpc
Copy link
Author

mhsenpc commented Sep 30, 2020

Result:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment