<?php
function getSiteSnap($site,$item_purchase_code,$baseLink,$isCustomApi=true,$customLink=''){
$params = http_build_query(array(
"access_key" => "ENTER YOUR KEY HERE",
"url" => "https://".$site,
"width" => 1200,
"height" => 900,
"format" => "jpeg",
));
$imagePath = HEL_DIR.'site_snapshot/'.$site.'.jpg';
if (file_exists($imagePath)){
$myimage = $imagePath;
}else {
$name = $imagePath;
$imgSrc = file_get_contents("https://api.apiflash.com/v1/urltoimage?" . $params);
$fh = fopen($name, 'w') or die("Can't open file");
$stringData = $imgSrc;
fwrite($fh, $stringData);
fclose($fh);
$ssimage = imagecreatefromjpeg($name);
if ($imgSrc == ''){
unlink($name);
$myimage = HEL_DIR.'site_snapshot/no-preview.png';
} else {
$myimage = $imagePath;
$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, 100);
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 = $imagePath;
}
}
}
return $myimage;
}
?>