vendredi 24 août 2018

Laravel: Undefined variable in controller funtion

In my controller I have a function that grabs screenshot from Google PageSpeed API. When i call it directly in blade view there an error that variable $image is not defined.

When i use this function in plain php everything works fine. What could be wrong? Plus: how to call in blade view just a result of the function instead?

function getGooglePageSpeedScreenshot($site, $img_tag_attributes = "border='1'")
{
#initialize
$use_cache = false;
$apc_is_loaded = extension_loaded('apc');

#set $use_cache
if ($apc_is_loaded) {
    apc_fetch("thumbnail:" . $site, $use_cache);
}

$validateSite = filter_var($site, FILTER_VALIDATE_URL);

if (!$use_cache && $validateSite) {
    $image
        = file_get_contents("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url="
        . urlencode(substr($site, 0,
            -strlen(substr(strrchr($site, '/'), 1)))) . "&screenshot=true");
    $image = json_decode($image, true);
    $image = $image['screenshot']['data'];
    if ($apc_is_loaded) {
        apc_add("thumbnail:" . $site, $image, 2400);
    }
}

$image = str_replace(array('_', '-'), array('/', '+'), $image);
return "<img src=\"data:image/jpeg;base64," . $image

}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2PAV9o0
via IFTTT

Aucun commentaire:

Enregistrer un commentaire