递归源文件到环境

时间:2017-07-15 11:04:48

标签: r

我想在完全隔离的环境中的文件夹中运行一个函数。此函数有一些它引用的资源,也在此文件夹中。

我知道如何使用source()将R函数加载到新环境中:

ne <- new.env()
source("myFuncs.R", ne)

但是,如果myFuncs.R包含source()source("helpers.R")形式调用自身,则会将这些函数加载到全局环境中!

我如何以递归方式将文件发送到新环境中?这可以在R中完成,而不必以某种方式通过正则表达式破解我的方式吗?

1 个答案:

答案 0 :(得分:2)

重新定义$image = imagecreatefromjpeg('https://vignette4.wikia.nocookie.net/matrix/images/1/1f/Monica_Bellucci_Dolce_and_Gabbana.jpg/revision/latest?cb=20130227074822'); $w = imagesx($image); $h = imagesy($image); $border = imagecreatefrompng('http://meson.ad-l.ink/8mRbHMnS9/thumb.png'); $borderW = imagesx($border); $borderH = imagesy($border); $borderSize = 70; // New image width and height $isHorizontalAlign = true; $coProp = $w / $h; if ($coProp > 1) $isHorizontalAlign = false; if ($isHorizontalAlign) { $newWidth = $w - $borderSize * 2; $newHeight = $newWidth * ($h / $w); } else { $newHeight = $h - $borderSize * 2; $newWidth = $newHeight * ($w / $h); } // Transparent border $indent = imagecreatetruecolor($w, $h); //imagesavealpha($indent, true); $color = imagecolorallocatealpha($indent, 0, 0, 0, 127); imagefill($indent, 0, 0, $color); $paddingLeft = ($w - $newWidth) / 2; $paddingTop = ($h - $newHeight) / 2; imagecopyresampled($indent, $image, $paddingLeft, $paddingTop, 0, 0, $newWidth, $newHeight, $w, $h); // New border width $x1 = $newWidth; $x2 = $borderSize; $x3 = (int)($x1 / $x2); if ($x3 == $x1 / $x2) $bw = $borderSize; else { $x4 = $x1 - $x3 * $x2; $x5 = $x4 / $x3; $x2 = $x2 + $x5; $bw = $x2; } // New border height $y1 = $newHeight; $y2 = $borderSize; $y3 = (int)($y1 / $y2); if ($y3 == $y1 / $y2) $bh = $borderSize; else { $y4 = $y1 - $y3 * $y2; $y5 = $y4 / $y3; $y2 = $y2 + $y5; $bh = $y2; } // Horizontal $percent1 = $bw / $borderSize; $borderNewWidth1 = (int)$borderW * $percent1; $borderNewHeight1 = (int)$borderH * $percent1; $thumb1 = imagecreatetruecolor($borderNewWidth1, $borderNewHeight1); imagesavealpha($thumb1, true); $color = imagecolorallocatealpha($thumb1, 0, 0, 0, 127); imagefill($thumb1, 0, 0, $color); imagecopyresized($thumb1, $border, 0, 0, 0, 0, $borderNewWidth1, $borderNewHeight1, $borderW, $borderH); // Vertical $percent2 = $bh / $borderSize; $borderNewWidth2 = (int)$borderW * $percent2; $borderNewHeight2 = (int)$borderH * $percent2; $thumb2 = imagecreatetruecolor($borderNewWidth2, $borderNewHeight2); imagesavealpha($thumb2, true); $color = imagecolorallocatealpha($thumb2, 0, 0, 0, 127); imagefill($thumb2, 0, 0, $color); imagecopyresized($thumb2, $border, 0, 0, 0, 0, $borderNewWidth2, $borderNewHeight2, $borderW, $borderH); // Angles $borderNewWidth3 = (int)$borderW * $percent1; $borderNewHeight3 = (int)$borderH * $percent2; $thumb3 = imagecreatetruecolor($borderNewWidth3, $borderNewHeight3); imagesavealpha($thumb3, true); $color = imagecolorallocatealpha($thumb3, 0, 0, 0, 127); imagefill($thumb3, 0, 0, $color); imagecopyresized($thumb3, $border, 0, 0, 0, 0, $borderNewWidth3, $borderNewHeight3, $borderW, $borderH); // Horizontal border $horizontalX = ($w - $newWidth) / 2; $horizontalY = (($h - $newHeight) / 2 - $bw) + 1; $horizontalY2 = $h - ($h - $newHeight) / 2; for ($i = 0; $i < round($newWidth / $bw); $i++) { // Top imagecopy($indent, $thumb1, $horizontalX + ($i * $bw), $horizontalY, $borderSize * $percent1, 0, $bw + 1, $bw); // Bottom imagecopy($indent, $thumb1, $horizontalX + ($i * $bw), $horizontalY2, $borderSize * $percent1, $borderSize * 2 * $percent1, $bw + 1, $bw - 1); } // Vertical border $verticalY = ($h - $newHeight) / 2; $verticalX = (($w - $newWidth) / 2 - $bh) + 1; $verticalX2 = $w - ($w - $newWidth) / 2; for ($i = 0; $i < round($newHeight / $bh); $i++) { // Left imagecopy($indent, $thumb2, $verticalX, $verticalY + ($i * $bh), 0, $borderSize * $percent2, $bh, $bh + 1); // Right imagecopy($indent, $thumb2, $verticalX2, $verticalY + ($i * $bh), ($borderSize * $percent2) * 2, $borderSize * $percent2, $bh, $bh + 1); } // Left top border imagecopy($indent, $thumb3, (($w - $newWidth) / 2 - $bw) + 1, (($h - $newHeight) / 2 - $bh) + 1, 0, 0, $bw, $bh); // Left bottom border imagecopy($indent, $thumb3, (($w - $newWidth) / 2 - $bw) + 1, $h - ($h - $newHeight) / 2, 0, ($borderSize * 2) * $percent2, $bw, $bh); // Right top border imagecopy($indent, $thumb3, $w - ($w - $newWidth) / 2, (($h - $newHeight) / 2 - $bh) + 1, ($borderSize * 2) * $percent1, 0, $bw, $bh); // Right bottom border imagecopy($indent, $thumb3, $w - ($w - $newWidth) / 2, $h - ($h - $newHeight) / 2, ($borderSize * 2) * $percent1, ($borderSize * 2) * $percent2, $bw, $bh); // Save result to base64 header('Content-Type: image/png'); imagepng($indent);

source