File: /var/www/html/www.uscornatese.it/json/breakingnews.php
<?php
require_once('../wp-load.php' );
global $wpdb;
function getImageFromText($html) {
preg_match_all('/<img[^>]+>/i',$html, $result);
//print_r($result);
$img = array();
foreach( $result as $img_tag) {
preg_match_all('/(src)=("[^"]*")/i',$img_tag[0], $im);
}
return $im[2][0];
}
function Img_Resize($path, $factor, $alias) {
$path = str_replace("http://www.atalantini.online/", "/var/www/html/camperfacherisold/atalantini_com/", $path);
$image = explode("/", $path);
$filename = $image[count($image) -1];
$opath = str_replace($filename,"", $path);
if (!file_exists($opath . "/thumb/")) {
mkdir($opath . "/thumb/");
}
$fpath = $opath . "/thumb/";
$final_path = $fpath . $alias . "_" . $filename;
if (!file_exists($final_path)) {
$x = getimagesize($path);
$width = $x['0'];
$height = $x['1'];
$rs_width = $width / 2;//resize to half of the original width.
$rs_width = $factor;
$rs_height = $height / 2;//resize to half of the original height.
list($width_orig, $height_orig) = getimagesize($path);
$ratio_orig = $width_orig/$height_orig;
$rs_height = $rs_width/$ratio_orig;
$scalingFactor = $rs_width / $width;
$rs_height = $height * $scalingFactor;
if ($x['mime'] == "image/gif") {
$img = imagecreatefromgif($path);
} else if ($x['mime'] == "image/jpeg") {
$img = imagecreatefromjpeg($path);
} else if ($x['mime'] == "image/jpg") {
$img = imagecreatefromjpeg($path);
} else if ($x['mime'] == "image/png") {
$img = imagecreatefrompng($path);
} else {
$img = imagecreatefrompng($path);
}
$img_base = imagecreatetruecolor($rs_width, $rs_height);
imagecopyresized($img_base, $img, 0, 0, 0, 0, $rs_width, $rs_height, $width, $height);
$path_info = pathinfo($fpath . "tmp_" . $filename);
if ($path_info['extension'] == "gif") {
imagegif($img_base, $fpath . "tmp_" . $filename);
}
if ($path_info['extension'] == "jpeg") {
imagejpeg($img_base, $fpath . "tmp_" . $filename);
}
if ($path_info['extension'] == "jpg") {
imagejpeg($img_base, $fpath . "tmp_" . $filename);
}
if ($path_info['extension'] == "png") {
imagepng($img_base, $fpath . "tmp_" . $filename);
}
$dst_x = 0; // X-coordinate of destination point
$dst_y = 0; // Y-coordinate of destination point
$src_x = 0; // Crop Start X position in original image
$src_y = 0; // Crop Srart Y position in original image
$dst_w = 572; // Thumb width
$dst_h = 282; // Thumb height
$src_w = 572; // Crop end X position in original image
$src_h = 282; // Crop end Y position in original image
// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
if ($path_info['extension'] == "gif") {
$src_image = imagecreatefromgif($fpath . "tmp_" . $filename);
}
if ($path_info['extension'] == "jpeg") {
$src_image = imagecreatefromjpeg($fpath . "tmp_" . $filename);
}
if ($path_info['extension'] == "jpg") {
$src_image = imagecreatefromjpeg($fpath . "tmp_" . $filename);
}
if ($path_info['extension'] == "png") {
$src_image = imagecreatefrompng($fpath . "tmp_" . $filename);
}
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, $fpath . $alias . "_" . $filename);
}
$final_path = $fpath . $alias . "_" . $filename;
$final_path = str_replace("/var/www/html/camperfacherisold/atalantini_com/", "http://www.atalantini.online/", $final_path);
return $final_path;
}
$sql = "(select * from wp_posts where post_status = 'publish' and id in (select object_id from wp_term_relationships where term_taxonomy_id = 10)) order by post_date desc;";
$records = $wpdb->get_results($sql);
$values = array();
foreach ( $records as $record ) {
$id = $record->ID;
$content = $record->post_content;
$content = preg_replace( '/(width|height)="\d*"\s/', "", $content );
$content = str_replace("<img ", "<img width=\"300\" ", $content);
$record->post_content = $content;
$date = $record->post_date;
$pdate = explode("-", substr($date, 0, 10));
$post_date = $pdate[2] . "-" . $pdate[1] . "-" . $pdate[0];
$record->post_title = strip_tags(strtolower($record->post_title));
$record->post_date = $post_date . " " . substr($date, 11, 8);
$value = get_field( "immagine_gallery", $id );
$image = $value['url'];
if ($image != "") {
$image = Img_Resize($image, "570", "thumb");
}
if ($image == "") {
$checkContent = strtolower($record->post_content);
if (strpos($checkContent, "<img ") > -1) {
$image = getImageFromText(strtolower($record->post_content));
$image = str_replace("\"", "", $image);
}
}
if (strpos($image, "atalantini.online") === false) {
$image = "https://www.atalantini.online/wp-content/themes/atalantini/images/logo.png";
}
$path = str_replace("https://www.atalantini.online/", "/var/www/html/camperfacherisold/atalantini_com/", $image);
$path = str_replace(" ","\\ ", $path);
if (!file_exists($path)) {
$image = "";
}
if ($image == "") {
$image = $value['url'];
if ($image != "") {
$image = Img_Resize($image, "570", "thumb");
}
//$image = "https://www.atalantini.online/wp-content/themes/atalantini/images/logo.png";
}
if ($image == "") {
$image = "https://www.atalantini.online/wp-content/themes/atalantini/images/logo.png";
}
// echo $record->post_title . " " . $path . " " . $image . "<br>";
$record->image = $image;
//if ($image != "") {
// $record->image = Img_Resize($image, "570", "thumb");
//}
$values[] = $record;
}
echo json_encode($values);
?>