<?php
error_reporting(E_ALL);
ini_set('max_execution_time', '0'); 
ini_set('memory_limit','2048M');
	if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  
  	include("configi.php");


  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($conn, $theValue) : mysqli_escape_string($conn, $theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


function ak_img_resize($target, $newcopy, $w, $h, $ext) {
	

    list($w_orig, $h_orig) = getimagesize($target);
    $scale_ratio = $w_orig / $h_orig;
    if (($w / $h) > $scale_ratio) {
           $w = $h * $scale_ratio;
    } else {
           $h = $w / $scale_ratio;
    }
    $img = "";
    $ext = strtolower($ext);
    if ($ext == "gif"){ 
      $img = imagecreatefromgif($target);
    } else if($ext =="png"){ 
      $img = imagecreatefrompng($target);
    } else { 
      $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    // imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
    imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
    imagejpeg($tci, $newcopy, 80);

}

//copy image2wbmp

$phxcrmclient_id = 94;

include_once("configi.php");


$images = 1;

if ($images == 1){

	$queryi="select * from ".$phxcrmclient_id."_prod_search where prod_image_url1 is not null order by uid";
	
	echo $queryi;
				
	//$queryi="select * from ".$phxcrmclient_id."_products_load where Image_Src is not null and uid >= 1460 order by uid";
				
	$resulti=mysqli_query($conn, $queryi);

				
	while($rowi=mysqli_fetch_array($resulti)) { 

		$uid =  $rowi['uid'];
		$prod_image_url1 =  $rowi['prod_image_url1'];
		
//		echo "working on ".$uid . "<br>";
		

			$from_url = "../images/productsfull/".$prod_image_url1;	

			$thumb_url = "../images/productsthumb/".$prod_image_url1; 

		if (stripos($from_url,"png")){
			$file_type = "png";
		}else if (stripos($from_url,"jpg")){
			$file_type = "jpg";
		}else{

			echo "error file type not found ".$Image_Src;
			exit;
			
		}		
		
		
		$wmax = 80;
		$hmax = 80;
		
		if ($file_type == "jpg"){
			$result = ak_img_resize($from_url, $thumb_url, $wmax, $hmax, "jpg");
		}else{
			$result = ak_img_resize($from_url, $thumb_url, $wmax, $hmax, "png");
		}
		
	

	}
	
	echo "completed images";

//exit;



}

	

echo "all complete";


