<?php

/*
 * @author Balaji
 * @name: A to Z SEO Tools - PHP Script
 * @copyright 2020 ProThemes.Biz
 *
 */
 
function getSiteSnap($site,$item_purchase_code){

$image_url = "http://screen2.prothemes.biz/api2/ss.php?site=$site";

if (file_exists(HEL_DIR."site_snapshot/$site.jpg")){
    $myimage = HEL_DIR."site_snapshot/$site.jpg";
}else{

    $name = HEL_DIR."site_snapshot/$site.jpg";
    $apiLink = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://".$site."&screenshot=true&key=AIzaSyCi4a1vmHM4ZfRwiXdQxZE_R4k__8Urv_w";
    $imgSrc = file_get_contents($apiLink);
    $screenData = json_decode($imgSrc, true);
    if(isset($screenData["lighthouseResult"]["audits"]["final-screenshot"]["details"]["data"])) {
        $screenShot = $screenData["lighthouseResult"]["audits"]["final-screenshot"]["details"]["data"];
        $screenShot = str_replace(array('_', '-'), array('/', '+'), $screenShot);
        $screenShot = str_replace("data:image/jpeg;base64,", "", $screenShot);
        $imgSrc = base64_decode($screenShot);
    }else{
        $imgSrc = '';
    }
    $myFile = $name;
    $fh = fopen($myFile, 'w') or die("Can't open file");
    $stringData = $imgSrc;
    fwrite($fh, $stringData);
    fclose($fh);

    if ($imgSrc == ''){
        unlink($name);
        $myimage = HEL_DIR.'site_snapshot/no-preview.png';
    } else {
        $ssimage = imagecreatefromjpeg($myFile);
        $myimage = HEL_DIR."site_snapshot/$site.jpg";
        $name = $myimage;
        $thumb_width = 600;
        $thumb_height = 450;

        $width = imagesx($ssimage);
        $height = imagesy($ssimage);

        $original_aspect = $width / $height;
        $thumb_aspect = $thumb_width / $thumb_height;

        if ($original_aspect >= $thumb_aspect){
            // If image is wider than thumbnail (in aspect ratio sense)
            $new_height = $thumb_height;
            $new_width = $width / ($height / $thumb_height);
        } else {
            // If the thumbnail is wider than the image
            $new_width = $thumb_width;
            $new_height = $height / ($width / $thumb_width);
        }

        $thumb = imagecreatetruecolor($thumb_width, $thumb_height);

        $co = imagecolorallocate($thumb, 241, 241, 241);
        imagefill($thumb, 0, 0, $co);
        $text_color = imagecolorallocate($thumb, 153, 153, 153);
        imagestring($thumb, 200, 400, 300, 'No Preview Available', $text_color);

        // Resize and crop
        imagecopyresampled($thumb, $ssimage, 0,
            //- ($new_width - $thumb_width) / 2, // Center the image horizontally
            0, // - ($new_height - $thumb_height) / 2, // Center the image vertically
            0, 0, $new_width, $new_height, $width, $height);
        
        
        imagejpeg($thumb, $myimage, 80);

        if (filesize($name) == 0){
            unlink($name);
            $myimage = HEL_DIR."site_snapshot/no-preview.png";
        } elseif (filesize($name) <= 4){
            unlink($name);
            $myimage = HEL_DIR."site_snapshot/no-preview.png";
        } else {
            $myimage = HEL_DIR."site_snapshot/$site.jpg";
        }
    }
 }
    return $myimage;
}