Detect that canvas element is outside of canvas boundary Detect that canvas element is outside of canvas boundary php php

Detect that canvas element is outside of canvas boundary


You can use PHP function imageftfbox (GD lib should be enabled).

It returns array of coordinates [x0,y0, x1,y1, x2,y2, x3,y3].

Based on them you can determine whether it's out of your SVG element placement, here's an example of how you can do this:

function cutStringToPix($str, $svgWidth, $fontSize = 14, $font='times_new_roman.ttf', $delimiter = '')   $font = './fonts/' .$font;  $sWidth = $svgWidth;  $newStr = $str;  $words = $delimiter     ? explode($delimiter, $str)     : str_split($str);  $wordsCnt = count($words);  // Reduce your string until it fits  for (;$sWidth >= $svgWidth && $wordsCnt; $wordsCnt--) {    $newStr = implode(' ', array_slice($words, 0, $wordsCnt));    $bbox = imagettfbbox($fontSize, 0, $font, $newStr);    $sWidth = $bbox[2] - $bbox[0];  };  return $newStr;}// Now use your $newStr

So ok, that's your example from comment

It uses TimesNewRoman font, you need to find TTF for it(for instance this...) and save it on server in "fonts" dir (relatively to your lib.php where you deside to store 'cutStringToPix' func) as 'times_new_roman.ttf'

then your SVG-forming part will be like this:

//include 'cutStringPix' func$message_string = array('{first_name}','{last_name}');$replace_string = array('Fist Name of User','Last Name Of User');$front_svg_url = $svg_url.$res_code[0]['front_side_svg_image'];$front_raw_svg = file_get_contents($front_svg_url);//so what we adding:$replace_string = cutStringToPix($replace_string, 162, 13);$front_side_svg = str_ireplace($message_string, $replace_string, $front_raw_svg);$file_name = uniqid($prefix).".svg";