ession_write_close(); ini_set('memory_limit', '512M'); set_time_limit(0); global $debug; $debug = false; if ($debug) var_dump($_SERVER['HTTP_IF_MODIFIED_SINCE']); if (!isset($generate_only) || isset($generate_only) && !$generate_only) { $force_regenerate = false; if (isset($_GET['force']) && $_GET['force'] == 1) { $force_regenerate = true; } $action = explode('/thumb/', strtok($_SERVER['REQUEST_URI'], '?#'), 2)[1]; // External image URL stored by scraper — fetch, resize, watermark, cache, serve. $actionWithoutSize = preg_replace('#^[^/]+/#', '', $action); if (substr($actionWithoutSize, 0, 4) === 'http') { $sizeKey = explode('/', $action)[0]; serveExternalThumb($actionWithoutSize, $sizeKey); exit; } generateThumb($action, false, $debug, $force_regenerate); } /** * @throws ImagickException */ function generateThumb($action, $generate_only = false, $debug = false, $force_regenerate = false) { if ($debug) { $time_start = microtime(true); } if (!strpos($action, "images/")) return "no img"; $size = explode("/", $action); $size = $size[0]; $img_file = explode("/", $action); $size = array_shift($img_file); $chunk = array_chunk($img_file, count($img_file) - 1); $img_path = urldecode(implode("/", $chunk[0])); $img_file = urldecode($img_file[count($img_file) - 1]); $img_location = APPLICATION_PATH . "/" . $img_path . "/" . $img_file; if ($debug) { var_dump($img_location); var_dump(file_exists($img_location)); } if (!strpos("./", $action) && file_exists($img_location)) { $sizeW = 300; $sizeH = 300; $resize = true; switch ($size) { case "flag": $sizeW = 18; $sizeH = 12; $resize = true; break; case "og-img": $org_width = getimagesize($img_location)[0]; if ($org_width >= 1000) { $sizeW = 1200; $sizeH = 630; } else { $sizeW = 600; $sizeH = 315; } $resize = true; break; case "wm": $org_width = getimagesize($img_location)[0]; if ($org_width >= 1400) { $sizeW = 1600; $sizeH = 840; } elseif ($org_width >= 1000) { $sizeW = 1200; $sizeH = 630; } else { $sizeW = 800; $sizeH = 420; } $resize = true; break; case "wm-je": $org_width = getimagesize($img_location)[0]; if ($org_width >= 1400) { $sizeW = 1600; $sizeH = 840; } elseif ($org_width >= 1000) { $sizeW = 1200; $sizeH = 630; } else { $sizeW = 800; $sizeH = 420; } $resize = true; break; case "bwm": $sizeW = 1920; $sizeH = 1080; break; case "lead-label": $sizeW = 600; $sizeH = 315; $resize = true; break; case "logo": $sizeW = 140; $sizeH = 280; break; case "mail-profile": $sizeW = 120; $sizeH = 120; break; case "living-in": $sizeW = 660; $sizeH = 495; break; case "xxs": $sizeW = 150; $sizeH = 150; break; case "xxs-square": $sizeW = 150; $sizeH = 150; $resize = true; break; case "xxs2": $sizeW = 150; $sizeH = 100; break; case "xs": $sizeW = 200; $sizeH = 130; break; case "xs-r": $sizeW = 200; $sizeH = 130; $resize = true; break; case "xs2": $sizeW = 200; $sizeH = 200; break; case "s": $sizeW = 250; $sizeH = 250; break; case "s2": $sizeW = 300; $sizeH = 200; case "sm": $sizeW = 350; $sizeH = 250; break; case "m": $sizeW = 368; $sizeH = 368; break; case "m2": $sizeW = 450; $sizeH = 450; break; case "m2-square": $sizeW = 450; $sizeH = 450; break; case "m3": $sizeW = 600; $sizeH = 300; break; case "ml": $sizeW = 600; $sizeH = 600; break; case "mxl": $sizeW = 800; $sizeH = 800; break; case "l": $sizeW = 1100; $sizeH = 740; break; case "l2": $sizeW = 1600; $sizeH = 750; break; case "l3": $sizeW = 1800; $sizeH = 1800; break; case "xl": $sizeW = 2400; $sizeH = 2400; break; case "750": $sizeW = 750; $sizeH = 99999; break; case "1000": $sizeW = 1000; $sizeH = 1000; break; case "u": //unmodified, original sizes $sizes = getimagesize($img_location); $sizeW = $sizes[0]; $sizeH = $sizes[1]; if ($debug) var_dump($sizes); break; default: echo "Size incorrect"; return; break; } if ($debug) { echo "Size set to " . $sizeW . ", " . $sizeH; } // Generate thumb if it doesnt exist $thumb_location = APPLICATION_PATH . "/" . $img_path . "/thumb-" . $size . "-" . $img_file; $path = APPLICATION_PATH . "/" . $img_path; chmod($path, 0777); if ($debug) { var_dump($path); echo "fileowner: " . var_dump(); print_r(stat($path)); echo substr(sprintf('%o', fileperms($path)), -4); } if ($force_regenerate) { // Remove existing thumb to force regeneration unlink($thumb_location); } // Check if thumb already exists or is outdated if (!file_exists($thumb_location) || (file_exists($thumb_location) && filemtime($thumb_location) != filemtime($img_location))) { if ($debug) { echo "Thumb file doesnt exist or version mismatch"; } // Make square? $crop_to_square = ((strpos($size, '-square') !== false) ? true : false); // Generate thumb global $db; // og-img & bwm will generate a custom thumb if ($size != 'og-img' && $size != 'bwm') { // Check if property image if (strpos($size, 'wm') === false && ( strpos($img_path, 'images/properties') !== false || strpos($img_path, 'images/notifications') !== false || strpos($img_path, 'images/leads/properties') !== false || strpos($img_path, 'images/newsletter/properties') !== false ) ) { $property_id = $chunk[0][count($chunk[0]) - 1]; $db->query_prepared( " SELECT p.id, la.licenseAgreementTypeId FROM properties p INNER JOIN agents2 a ON p.estateAgent = a.agentId AND a.hoofdvestiging = 1 INNER JOIN licenseAgreements la ON a.licenseAgreementId = la.id WHERE p.id = ?", 'i', [$property_id] ); if ($db->next_record()) { $property = $db->record; } // Resize to target dimensions first if (!thumbnailImage($img_location, $thumb_location, $sizeW, $sizeH, $crop_to_square, $resize)) { echo "Failed to create thumb.\n"; return; } if ($property) { // Add watermark for all free license agencies' images if ($property['licenseAgreementTypeId'] == 2 && $size !== 'living-in') { // Save as jpg first otherwise getImage has exception when file is webp (maybe cause by outdated gd, maybe update?) $img = new Imagick(realpath($thumb_location)); $img->setImageFormat('jpeg'); $img->writeImage($thumb_location); $img->destroy(); $im_size = getimagesize($thumb_location); $im_org = getImage($thumb_location, $im_size); addWatermark($im_org, $im_size[0], $im_size[1], 0, 0); if (pathinfo($thumb_location)['extension'] == 'png') { imagepng($im_org, $thumb_location); } else { imagejpeg($im_org, $thumb_location, 90); } } } } else { // Create thumbnail if (!thumbnailImage($img_location, $thumb_location, $sizeW, $sizeH, $crop_to_square, $resize)) { echo "Failed to create thumb.\n"; return; } } } if ($size == 'og-img') { $property_id = $chunk[0][count($chunk[0]) - 1]; $db->query_prepared(" SELECT p.id, p.country, p.currency, p.forSalePrice, p.forRentPrice, p.saleTerm, p.rentTerm, la.licenseAgreementTypeId, (SELECT COUNT(*) -4 FROM propertiesImages WHERE mediaType = 'image' AND propertyId = ? ORDER BY sortOrder, id) AS moreImagesTotal FROM properties p INNER JOIN agents2 a ON p.estateAgent = a.agentId AND a.hoofdvestiging = 1 INNER JOIN licenseAgreements la ON a.licenseAgreementId = la.id WHERE p.id = ?", 'ii', [$property_id, $property_id]); if ($db->next_record()) { $property = $db->record; } $db->query_prepared( " SELECT imageAbsPath FROM propertiesImages WHERE mediaType = 'image' AND propertyId = ? ORDER BY sortOrder, id LIMIT 1,3", 'i', [$property_id] ); while ($db->next_record()) { $propertyImages[] = $db->record; } if (count($propertyImages) == 3) { $flagfile = APPLICATION_PATH . '/images/flags/' . strtolower($property['country']) . '.png'; $font = APPLICATION_PATH . '/static/fonts/NunitoSans-SemiBold.ttf'; if ($org_width >= 1000) { thumbnailImage($img_location, $thumb_location, 1200, 420, $crop_to_square, $resize); foreach ($propertyImages as $image) { $tmp_thumb_location = APPLICATION_PATH . "/" . $img_path . "/thumb-" . $size . "-" . basename($image['imageAbsPath']); thumbnailImage($image['imageAbsPath'], $tmp_thumb_location, 400, 210, $crop_to_square, $resize); $im_size = getimagesize($tmp_thumb_location); if ($im_size['mime'] == 'image/png') { $tmp_images[] = imagecreatefrompng($tmp_thumb_location); } else { $tmp_images[] = imagecreatefromjpeg($tmp_thumb_location); } unlink($tmp_thumb_location); } } else { thumbnailImage($img_location, $thumb_location, 600, 210, $crop_to_square, $resize); foreach ($propertyImages as $image) { $tmp_thumb_location = APPLICATION_PATH . "/" . $img_path . "/thumb-" . $size . "-" . basename($image['imageAbsPath']); thumbnailImage($image['imageAbsPath'], $tmp_thumb_location, 200, 105, $crop_to_square, $resize); $im_size = getimagesize($tmp_thumb_location); if ($im_size['mime'] == 'image/png') { $tmp_images[] = imagecreatefrompng($tmp_thumb_location); } else { $tmp_images[] = imagecreatefromjpeg($tmp_thumb_location); } unlink($tmp_thumb_location); } } $im_size = getimagesize($thumb_location); $im_org = getImage($thumb_location, $im_size); $im_flag = imagecreatefrompng($flagfile); $price_offset = 0; $bg_color_price = imagecolorallocatealpha($im_org, 0, 23, 49, 15); $bg_white = imagecolorallocate($im_org, 255, 255, 255); $text_color = imagecolorallocate($im_org, 255, 255, 255); $text = getPriceLabel($property['currency'], $property['forSalePrice'], $property['forRentPrice'], $property['saleTerm'], $property['rentTerm'], $language, FALSE); // Create new image based on original size if ($org_width >= 1000) { // Create image container $im_container = imagecreatetruecolor(1200, 630); // Add image imagecopyresampled($im_container, $im_org, 0, 0, 0, 0, 1200, 420, 1200, 420); // Watermark if ($property['licenseAgreementTypeId'] == 2) { addWatermark($im_container, 1200, 420); } // Add overlay imagefilledrectangle($im_container, 0, 268 + $price_offset, strlen($text) * 26, 348 + $price_offset, $bg_color_price); // Add flag imagecopyresampled($im_container, $im_flag, 30, 290 + $price_offset, 0, 0, 54, 36, 18, 12); // Add price imagettftext($im_container, 28, 0, 100, 320 + $price_offset, $text_color, $font, $text); // Add thumb 1 imagecopyresampled($im_container, $tmp_images[0], 0, 420, 0, 0, 400, 210, 400, 210); if ($property['licenseAgreementTypeId'] == 2) { addWatermark($im_container, 400, 210, 0, 420); } // Add thumb 2 imagecopyresampled($im_container, $tmp_images[1], 400, 420, 0, 0, 400, 210, 400, 210); if ($property['licenseAgreementTypeId'] == 2) { addWatermark($im_container, 400, 210, 400, 420); } // Add thumb 3 imagecopyresampled($im_container, $tmp_images[2], 800, 420, 0, 0, 400, 210, 400, 210); if ($property['licenseAgreementTypeId'] == 2) { addWatermark($im_container, 400, 210, 800, 420); } // Add divider lines imagefilledrectangle($im_container, 0, 418, 1200, 422, $bg_white); imagefilledrectangle($im_container, 398, 418, 402, 630, $bg_white); imagefilledrectangle($im_container, 798, 418, 802, 630, $bg_white); // Add more image label if ($property['moreImagesTotal'] > 99) { imagettftext($im_container, 48, 0, 1000, 550, $text_color, $font, '+' . $property['moreImagesTotal']); } elseif ($property['moreImagesTotal'] > 9) { imagettftext($im_container, 48, 0, 1040, 550, $text_color, $font, '+' . $property['moreImagesTotal']); } elseif ($property['moreImagesTotal'] > 0) { imagettftext($im_container, 48, 0, 1080, 550, $text_color, $font, '+' . $property['moreImagesTotal']); } } else { // Create image container $im_container = imagecreatetruecolor(600, 315); // Add image imagecopyresampled($im_container, $im_org, 0, 0, 0, 0, 600, 210, 600, 210); // Watermark if ($property['licenseAgreementTypeId'] == 2) { addWatermark($im_container, 600, 210); } // Add thumb 1 imagecopyresampled($im_container, $tmp_images[0], 0, 210, 0, 0, 200, 105, 200, 105); if ($property['licenseAgreementTypeId'] == 2) { addWatermark($im_container, 200, 105, 0, 210); } // Add thumb 2 imagecopyresampled($im_container, $tmp_images[1], 200, 210, 0, 0, 200, 105, 200, 105); if ($property['licenseAgreementTypeId'] == 2) { addWatermark($im_container, 200, 105, 200, 210); } // Add thumb 3 imagecopyresampled($im_container, $tmp_images[2], 400, 210, 0, 0, 200, 105, 200, 105); if ($property['licenseAgreementTypeId'] == 2) { addWatermark($im_container, 200, 105, 400, 210); } // Add divider lines imagefilledrectangle($im_container, 0, 209, 600, 211, $bg_white); imagefilledrectangle($im_container, 199, 209, 201, 315, $bg_white); imagefilledrectangle($im_container, 399, 209, 401, 315, $bg_white); // Add more image label if ($property['moreImagesTotal'] > 99) { imagettftext($im_container, 28, 0, 500, 275, $text_color, $font, '+' . $property['moreImagesTotal']); } elseif ($property['moreImagesTotal'] > 9) { imagettftext($im_container, 28, 0, 520, 275, $text_color, $font, '+' . $property['moreImagesTotal']); } elseif ($property['moreImagesTotal'] > 0) { imagettftext($im_container, 28, 0, 540, 275, $text_color, $font, '+' . $property['moreImagesTotal']); } // Add overlay imagefilledrectangle($im_container, 0, 132 + $price_offset, strlen($text) * 13, 174 + $price_offset, $bg_color_price); // Add flag imagecopyresampled($im_container, $im_flag, 20, 148 + $price_offset, 0, 0, 18, 12, 18, 12); // Add price imagettftext($im_container, 14, 0, 50, 160 + $price_offset, $text_color, $font, $text); } // Save thumb if (pathinfo($thumb_location)['extension'] == 'png') { imagepng($im_container, $thumb_location); } else { imagejpeg($im_container, $thumb_location, 90); } } else { $flagfile = APPLICATION_PATH . '/images/flags/' . strtolower($property['country']) . '.png'; $font = APPLICATION_PATH . '/static/fonts/NunitoSans-SemiBold.ttf'; $im_size = getimagesize($thumb_location); $im_org = getImage($thumb_location, $im_size); $im_flag = imagecreatefrompng($flagfile); $price_offset = 0; $bg_color_price = imagecolorallocatealpha($im_org, 0, 23, 49, 15); $text_color = imagecolorallocate($im_org, 255, 255, 255); $text = getPriceLabel($property['currency'], $property['forSalePrice'], $property['forRentPrice'], $property['saleTerm'], $property['rentTerm'], $language, FALSE); if ($org_width >= 1000) { // Add watermark if ($property['licenseAgreementTypeId'] == 2) { addWatermark($im_org, $sizeW, $sizeH, 0, 0); } // Add overlay imagefilledrectangle($im_org, 0, 468 + $price_offset, strlen($text) * 26, 548 + $price_offset, $bg_color_price); // Add flag imagecopyresampled($im_org, $im_flag, 30, 496 + $price_offset, 0, 0, 36, 24, 18, 12); // Add price imagettftext($im_org, 28, 0, 95, 520 + $price_offset, $text_color, $font, $text); } else { // Add watermark if ($property['licenseAgreementTypeId'] == 2) { addWatermark($im_org, $sizeW, $sizeH, 0, 0); } // Add overlay imagefilledrectangle($im_org, 0, 232 + $price_offset, strlen($text) * 13, 274 + $price_offset, $bg_color_price); // Add flag imagecopyresampled($im_org, $im_flag, 20, 248 + $price_offset, 0, 0, 18, 12, 18, 12); // Add price imagettftext($im_org, 14, 0, 50, 260 + $price_offset, $text_color, $font, $text); } // Save thumb if (pathinfo($thumb_location)['extension'] == 'png') { imagepng($im_org, $thumb_location); } else { imagejpeg($im_org, $thumb_location, 90); } } // Free up memory imagedestroy($im_org); imagedestroy($im_container); imagedestroy($im_flag); imagedestroy($tmp_images[0]); imagedestroy($tmp_images[1]); imagedestroy($tmp_images[2]); } elseif ($size == 'lead-label') { /*$lead_id = $chunk[0][count($chunk[0]) - 1]; $db->query_prepared(" SELECT property_id FROM mailcontacts WHERE id = ?", 'i', [$lead_id] ); $db->next_record(); $property_id = $db->record['property_id'];*/ $property_id = $chunk[0][count($chunk[0]) - 1]; $db->query_prepared( " SELECT p.id, p.country, p.currency, p.forSalePrice, p.forRentPrice, p.saleTerm, p.rentTerm FROM properties p INNER JOIN agents2 a ON p.estateAgent = a.agentId AND a.hoofdvestiging = 1 WHERE p.id = ?", 'i', [$property_id] ); if ($db->next_record()) { $property = $db->record; } $flagfile = APPLICATION_PATH . '/images/flags/' . strtolower($property['country']) . '.png'; $font = APPLICATION_PATH . '/static/fonts/NunitoSans-SemiBold.ttf'; $im_size = getimagesize($thumb_location); $im_org = getImage($thumb_location, $im_size); $im_flag = imagecreatefrompng($flagfile); $bg_color = imagecolorallocatealpha($im_org, 0, 23, 49, 15); $text_color = imagecolorallocate($im_org, 255, 255, 255); $text = getPriceLabel($property['currency'], $property['forSalePrice'], $property['forRentPrice'], $property['saleTerm'], $property['rentTerm'], $language, FALSE); // Add overlay imagefilledrectangle($im_org, 0, 232, strlen($text) * 13, 274, $bg_color); // Add flag imagecopyresampled($im_org, $im_flag, 20, 248, 0, 0, 18, 12, 18, 12); // Add price imagettftext($im_org, 14, 0, 50, 260, $text_color, $font, $text); // Save thumb if (pathinfo($thumb_location)['extension'] == 'png') { imagepng($im_org, $thumb_location); } else { imagejpeg($im_org, $thumb_location, 90); } // Free up memory imagedestroy($im_org); imagedestroy($im_flag); } elseif ($size == 'bwm') { // Resize to target dimensions first if (!thumbnailImage($img_location, $thumb_location, $sizeW, $sizeH, false, true)) { echo "Failed to create thumb.\n"; return; } // Save as jpg first otherwise getImage has exception when file is webp (maybe caused by outdated gd, maybe update?) $img = new Imagick(realpath($thumb_location)); $img->setImageFormat('jpeg'); $img->writeImage($thumb_location); $img->destroy(); $im_size = getimagesize($thumb_location); $im_org = getImage($thumb_location, $im_size); $wmfile = APPLICATION_PATH . '/images/BC-WM-500.png'; $im_wm = imagecreatefrompng($wmfile); $wm_height = $sizeH * 0.35; $wm_width = $wm_height * 1.488095238095238; // Aspect ratio watermark image $wm_x = ($sizeW - $wm_width) / 2; // Center watermark $wm_y = $sizeH - ($sizeH * 0.40); // Place a bit from the bottom // Add watermark imagecopyresampled($im_org, $im_wm, $wm_x, $wm_y, 0, 0, $wm_width, $wm_height, 500, 336); // Save thumb if (pathinfo($thumb_location)['extension'] == 'png') { imagepng($im_org, $thumb_location); } else { imagejpeg($im_org, $thumb_location, 90); } // Free up memory imagedestroy($im_org); imagedestroy($im_wm); } elseif (strpos($size, 'wm') === 0) { $im_size = getimagesize($thumb_location); $im_org = getImage($thumb_location, $im_size); if (stripos($thumb_location, '/leads/') !== FALSE && stripos($thumb_location, '/properties/') === FALSE) { $lead_id = $chunk[0][count($chunk[0]) - 1]; $db->query_prepared(" SELECT p.id, p.country, p.currency, p.forSalePrice, p.forRentPrice, p.saleTerm, p.rentTerm, a.baerzBannerPosition, a.baerzBannerTransparency, a.baerzBannerHeight, a.baerzBannerOffset FROM properties p INNER JOIN agents2 a ON p.estateAgent = a.agentId AND a.hoofdvestiging = 1 INNER JOIN mailcontacts mc ON p.id = mc.property_id WHERE mc.id = ?", 'i', [$lead_id]); if ($db->next_record()) { $property = $db->record; } } else { $property_id = $chunk[0][count($chunk[0]) - 1]; $db->query_prepared(" SELECT p.id, p.country, p.currency, p.forSalePrice, p.forRentPrice, p.saleTerm, p.rentTerm, a.baerzBannerPosition, a.baerzBannerTransparency, a.baerzBannerHeight, a.baerzBannerOffset FROM properties p INNER JOIN agents2 a ON p.estateAgent = a.agentId AND a.hoofdvestiging = 1 WHERE p.id = ?", 'i', [$property_id]); if ($db->next_record()) { $property = $db->record; } } if ($size == 'wm-je') { // Always high transparency for James Edition $bg_color = imagecolorallocatealpha($im_org, 190, 156, 126, 70); } elseif ($property['baerzBannerTransparency'] == 'high') { $bg_color = imagecolorallocatealpha($im_org, 190, 156, 126, 70); } elseif ($property['baerzBannerTransparency'] == 'low') { $bg_color = imagecolorallocatealpha($im_org, 190, 156, 126, 5); } else { $bg_color = imagecolorallocatealpha($im_org, 190, 156, 126, 40); } $offset = $property['baerzBannerOffset']; if ($org_width >= 1400) { $wmfile = APPLICATION_PATH . '/images/baerz-watermark-1600-white.png'; $im_wm = imagecreatefrompng($wmfile); $height = ($property['baerzBannerHeight'] == 'big' ? 50 : 0); // Baerz banner /*// Add gradient $lines = 754 - 669; for($i = 0; $i < $lines; $i++) { $grad_color = imagecolorallocatealpha($im_org, 190, 156, 126, (115 - $i <= 30 ? 30 : 115-$i)); imagefilledrectangle($im_org, 0, 669 + $i, 1600, 669 + $i, $grad_color); }*/ if ($property['baerzBannerPosition'] == 'top') { // Add overlay imagefilledrectangle($im_org, 0, 0 + $offset, 1600, 207 + $height + $offset, $bg_color); // Add watermark imagecopyresampled($im_org, $im_wm, 0, 0 + ($height / 2) + $offset, 0, 0, 1600, 207, 1600, 207); } elseif ($property['baerzBannerPosition'] == 'center') { // Add overlay imagefilledrectangle($im_org, 0, 316.5 - ($height / 2) + $offset, 1600, 523.5 + ($height / 2) + $offset, $bg_color); // Add watermark imagecopyresampled($im_org, $im_wm, 0, 316.5 + $offset, 0, 0, 1600, 207, 1600, 207); } else { // Add overlay imagefilledrectangle($im_org, 0, 633 - $height + $offset, 1600, 840 + $offset, $bg_color); // Add watermark imagecopyresampled($im_org, $im_wm, 0, 633 - ($height / 2) + $offset, 0, 0, 1600, 207, 1600, 207); } } elseif ($org_width >= 1000) { $wmfile = APPLICATION_PATH . '/images/baerz-watermark-1200-white.png'; $im_wm = imagecreatefrompng($wmfile); $height = ($property['baerzBannerHeight'] == 'big' ? 40 : 0); // Baerz banner if ($property['baerzBannerPosition'] == 'top') { // Add overlay imagefilledrectangle($im_org, 0, 0 + $offset, 1200, 155 + $height + $offset, $bg_color); // Add watermark imagecopyresampled($im_org, $im_wm, 0, 0 + ($height / 2) + $offset, 0, 0, 1200, 155, 1200, 155); } elseif ($property['baerzBannerPosition'] == 'center') { // Add overlay imagefilledrectangle($im_org, 0, 237.5 - ($height / 2) + $offset, 1200, 392.5 + ($height / 2) + $offset, $bg_color); // Add watermark imagecopyresampled($im_org, $im_wm, 0, 237.5 + $offset, 0, 0, 1200, 155, 1200, 155); } else { // Add overlay imagefilledrectangle($im_org, 0, 475 - $height + $offset, 1200, 630 + $offset, $bg_color); // Add watermark imagecopyresampled($im_org, $im_wm, 0, 475 - ($height / 2) + $offset, 0, 0, 1200, 155, 1200, 155); } } else { $wmfile = APPLICATION_PATH . '/images/baerz-watermark-800-white.png'; $im_wm = imagecreatefrompng($wmfile); $height = ($property['baerzBannerHeight'] == 'big' ? 20 : 0); // Baerz banner if ($property['baerzBannerPosition'] == 'top') { // Add overlay imagefilledrectangle($im_org, 0, 0 + $offset, 800, 104 + $height + $offset, $bg_color); // Add watermark imagecopyresampled($im_org, $im_wm, 0, 0 + ($height / 2) + $offset, 0, 0, 800, 104, 800, 104); } elseif ($property['baerzBannerPosition'] == 'center') { // Add overlay imagefilledrectangle($im_org, 0, 158 - ($height / 2) + $offset, 800, 262 + ($height / 2) + $offset, $bg_color); // Add watermark imagecopyresampled($im_org, $im_wm, 0, 158 + $offset, 0, 0, 800, 104, 800, 104); } else { // Add overlay imagefilledrectangle($im_org, 0, 316 - $height + $offset, 800, 420 + $offset, $bg_color); // Add watermark imagecopyresampled($im_org, $im_wm, 0, 316 - ($height / 2) + $offset, 0, 0, 800, 104, 800, 104); } } // Save thumb if (pathinfo($thumb_location)['extension'] == 'png') { imagepng($im_org, $thumb_location); } else { imagejpeg($im_org, $thumb_location, 90); } // Free up memory imagedestroy($im_org); imagedestroy($im_wm); } if ($debug) { var_dump($thumb_location); } // Set modified time same as original image file - to identify old thumbs later on touch($thumb_location, filemtime($img_location)); // TODO: Bug - Sometimes thumb image is corrupt for lead images, cause unknown // Check if thumb is corrupt for lead images if (strpos($action, '/leads/') !== false) { if (isCorruptImage($thumb_location)) { // Delete thumb and retry unlink($thumb_location); generateThumb($action, false, false, false); } } } else { $thumb_location = APPLICATION_PATH . "/" . $img_path . "/thumb-" . $size . "-" . $img_file; if ($debug) var_dump("thumb already exists"); } // Output thumb if (!$generate_only) { // Checking if the client is validating his cache and if it is current. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($thumb_location)) ) { // send the last mod time of the file back if ($debug) { echo "Sending back last mod time"; } header( 'Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($thumb_location)) . ' GMT', true, 304 ); exit; } $etag = md5_file($thumb_location); if (!$debug) { header('Pragma: public'); header('Cache-Control: max-age=' . (86400 * 365)); header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + (86400 * 365) /*30 days*/)); header('Content-Type: image/jpeg'); header('ETag: "{$etag}"'); header('Last-modified: ' . gmdate('D, d M Y H:i:s', filemtime($thumb_location)) . ' GMT'); header('Content-transfer-encoding: binary'); header('Content-length: ' . filesize($thumb_location)); echo file_get_contents($thumb_location); } if ($debug) { $time_end = microtime(true); $execution_time = ($time_end - $time_start); echo 'Total Execution Time: ' . $execution_time . 'secs'; } die(); } else { // Done, return return; } } else { return "error"; } } function serveExternalThumb($externalUrl, $sizeKey) { $cacheDir = APPLICATION_PATH . '/images/external-thumbs/'; if (!is_dir($cacheDir)) { mkdir($cacheDir, 0777, true); chmod($cacheDir, 0777); } $cacheFile = $cacheDir . $sizeKey . '-' . md5($externalUrl) . '.jpg'; if (!file_exists($cacheFile)) { $sizemap = [ 'xxs' => [150, 150], 'xxs-square' => [150, 150], 'xs' => [200, 130], 's' => [250, 250], 'sm' => [350, 250], 'm' => [368, 368], 'm2' => [450, 450], 'ml' => [600, 600], 'mxl' => [800, 800], 'l' => [1100, 740], '750' => [750, 9999], '1000' => [1000, 1000], 'bwm' => [1920, 1080], ]; [$sizeW, $sizeH] = $sizemap[$sizeKey] ?? [600, 600]; $ch = curl_init($externalUrl); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 15, CURLOPT_FOLLOWLOCATION => true, CURLOPT_USERAGENT => defined('BAERZ_USERAGENT') ? BAERZ_USERAGENT : 'Mozilla/5.0', ]); $imageData = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if (!$imageData || $httpCode !== 200) { header('HTTP/1.0 404 Not Found'); exit; } $tempFile = sys_get_temp_dir() . '/ext-thumb-' . md5($externalUrl) . '.jpg'; file_put_contents($tempFile, $imageData); $crop = strpos($sizeKey, '-square') !== false; if (!thumbnailImage($tempFile, $cacheFile, $sizeW, $sizeH, $crop, true)) { @unlink($tempFile); header('HTTP/1.0 500 Internal Server Error'); exit; } @unlink($tempFile); // Convert to JPEG (Imagick handles webp/png/etc) $img = new Imagick($cacheFile); $img->setImageFormat('jpeg'); $img->writeImage($cacheFile); $img->destroy(); $imSize = getimagesize($cacheFile); $imOrg = imagecreatefromjpeg($cacheFile); if ($sizeKey === 'bwm') { // Match the bwm watermark logic exactly: larger mark, lower position $wmfile = APPLICATION_PATH . '/images/BC-WM-500.png'; $imWm = imagecreatefrompng($wmfile); $wmHeight = $sizeH * 0.35; $wmWidth = $wmHeight * 1.488095238095238; $wmX = ($sizeW - $wmWidth) / 2; $wmY = $sizeH - ($sizeH * 0.40); imagecopyresampled($imOrg, $imWm, $wmX, $wmY, 0, 0, $wmWidth, $wmHeight, 500, 336); imagedestroy($imWm); } else { addWatermark($imOrg, $imSize[0], $imSize[1]); } imagejpeg($imOrg, $cacheFile, 90); imagedestroy($imOrg); } header('Pragma: public'); header('Cache-Control: max-age=' . (86400 * 30)); header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + (86400 * 30))); header('Content-Type: image/jpeg'); header('Content-Length: ' . filesize($cacheFile)); readfile($cacheFile); exit; } function addWatermark(&$image, $sizeW, $sizeH, $offsetX = 0, $offsetY = 0) { $wmFile = APPLICATION_PATH . '/images/BC-WM-500.png'; $imWm = imagecreatefrompng($wmFile); $wmHeight = $sizeH * 0.25; $wmWidth = $wmHeight * 1.488095238095238; // Aspect ratio watermark image $wmX = ($sizeW - $wmWidth) / 2; // Center watermark $wmY = $sizeH - ($sizeH * 0.28); // Place a bit from the bottom // Add watermark imagecopyresampled($image, $imWm, $offsetX + $wmX, $offsetY + $wmY, 0, 0, $wmWidth, $wmHeight, 500, 336); } error_reporting(0); ini_set('display_errors', 0); function serveWatermarkedImage($imagePath) { $imagePath = ltrim($imagePath, '/'); $fullPath = __DIR__ . '/thumb/750/' . $imagePath; if (!file_exists($fullPath)) { $fullPath = __DIR__ . '/' . $imagePath; } if (!file_exists($fullPath) || is_dir($fullPath)) { header("HTTP/1.0 404 Not Found"); exit; } $imageInfo = getimagesize($fullPath); if (!$imageInfo) { header("HTTP/1.0 400 Bad Request"); exit; } switch ($imageInfo['mime']) { case 'image/jpeg': $image = imagecreatefromjpeg($fullPath); break; case 'image/png': $image = imagecreatefrompng($fullPath); break; default: exit; } $wmFile = __DIR__ . '/images/BC-WM-500.png'; if (file_exists($wmFile)) { $imWm = imagecreatefrompng($wmFile); $originalW = $imageInfo[0]; $originalH = $imageInfo[1]; $wmHeight = $originalH * 0.25; $wmWidth = $wmHeight * 1.488; $wmX = ($originalW - $wmWidth) / 2; $wmY = $originalH - ($originalH * 0.28); imagealphablending($image, true); imagesavealpha($image, true); imagecopyresampled($image, $imWm, $wmX, $wmY, 0, 0, $wmWidth, $wmHeight, 500, 336); imagedestroy($imWm); } header('Content-Type: ' . $imageInfo['mime']); if ($imageInfo['mime'] === 'image/jpeg') { imagejpeg($image, null, 85); } else { imagepng($image); } imagedestroy($image); exit; } if (isset($_GET['img'])) { serveWatermarkedImage($_GET['img']); } function getImage(&$img_location, $im_size) { $org_img_location = $img_location; $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $img_location); finfo_close($finfo); $extension = pathinfo($img_location)['extension']; if ($mime == 'image/png') { $image = imagecreatefrompng($img_location); if ($extension != 'png') { $img_location = str_ireplace(['.jpg', '.jpeg'], '.png', $img_location); updateImagePaths($img_location, $org_img_location, 'png'); } } elseif ($mime == 'image/webp') { $image = imagecreatefromwebp($img_location); if ($extension != 'jpg' && $extension != 'jpeg') { $img_location = str_ireplace(['.png'], '.jpg', $img_location); updateImagePaths($img_location, $org_img_location, 'jpg'); } } else { $image = imagecreatefromjpeg($img_location); if ($extension != 'jpg' && $extension != 'jpeg') { $img_location = str_ireplace(['.png'], '.jpg', $img_location); updateImagePaths($img_location, $org_img_location, 'jpg'); } } return $image; } function updateImagePaths($file, $org_file, $type) { global $db; rename($org_file, $file); switch ($type) { case 'png': $db->query_prepared( " UPDATE propertiesImages SET imageAbsPath = REPLACE(REPLACE(imageAbsPath, '.jpg', '.png'), '.jpeg', '.png'), imagePath = REPLACE(REPLACE(imagePath, '.jpg', '.png'), '.jpeg', '.png') WHERE id = ?", 'i', [pathinfo($file)['filename']] ); break; case 'jpg': $db->query_prepared( " UPDATE propertiesImages SET imageAbsPath = REPLACE(imageAbsPath, '.png', '.jpg'), imagePath = REPLACE(imagePath, '.png', '.jpg') WHERE id = ?", 'i', [pathinfo($file)['filename']] ); break; } } Baerz & Co - Luxury Homes frontend-en/thumb/ml file not found !