require "gslib1.php";
require "../iimagelogger.php";
//$x=1012;
//print $x%11;
//exit;
//createThumbnail("b.jpg",600,760);
$topic=$param1;
$topic_arr=explode("-",$topic);
$topic_id=SQLite3::escapeString($topic_arr[sizeof($topic_arr)-1]);
$t=Convert_Timestamp_to_Datetime(Time());
$topic_rec=Lite_Rtv_Recs("select * from topics where topics_pk='$topic_id' and release_date<='$t'",1,1);
//$topic_rec=Lite_Rtv_Recs("select * from topics where topics_pk='$topic_id'",1,1);
$s=sizeof($topic_rec["topics_pk"]);
if ($s!=1){
print 'none found';
exit; //no valid topics with release date found
}
$next_topics_pk=$topic_rec["topics_pk"][0];
$next_kw_pk=$topic_rec["kw_pk"][0];
$next_image1=$topic_rec["image1"][0];
$next_kw_pk="watches";
$next_image_path=$images_root . $next_image1;
$rand_width=$topic_rec["image1_x"][0];
$image=$next_image_path;
//$image=str_replace('/','\\',$image);
createThumbnail($image,$rand_width,1900,"Y");
function get_scale_ratio($x,$y,$maxx,$maxy,$scale_up='Y'){
$x_to_yratio=$x/$y;
$y_to_xratio=$y/$x;
//print $x_to_yratio . "
";
$newx=$x;
$newy=$y;
while (($newx>$maxx) ){
$newx--;
$newy=$newy-$y_to_xratio;
}
/*
while (($newy>$maxy) AND ($newx>$maxx)){
$newy--;
$newx=$newx-$x_to_yratio;
}
*/
if ($scale_up=='Y') {
while ($newx<$maxx) {
$newx++;
$newy=$newy+$y_to_xratio;
}
/*
while (($newy<$maxy) AND ($newx<$maxx)){
$newy++;
$newx=$newx+$x_to_yratio;
}
*/
}
$newx=ceil($newx);
$newy=ceil($newy);
$ret[0]=$newx;
$ret[1]=$newy;
return $ret;
}
function createThumbnail($image_name,$new_width,$new_height,$scale_up="N")
{
//print $image_name;
list($width, $height, $image_type) = getimagesize($image_name);
$size=get_scale_ratio($width,$height,$new_width,$new_height,$scale_up);
$new_width=$size[0];
$new_height=$size[1];
switch ($image_type)
{
case 1: $src_img = imagecreatefromgif($image_name); break;
case 2: $src_img = imagecreatefromjpeg($image_name); break;
case 3: $src_img = imagecreatefrompng($image_name); break;
default: return ''; break;
}
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
if($old_x > $old_y)
{
$thumb_w = $new_width;
$thumb_h = $old_y*($new_height/$old_x);
}
if($old_x < $old_y)
{
$thumb_w = $old_x*($new_width/$old_y);
$thumb_h = $new_height;
}
if($old_x == $old_y)
{
$thumb_w = $new_width;
$thumb_h = $new_height;
}
$thumb_w=$new_width;
$thumb_h=$new_height;
$dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// New save location
/*
if($mime['mime']=='image/png'){ $result = imagepng($dst_img,$new_thumb_loc,8); }
if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
*/
ob_start();
switch ($image_type)
{
case 1: imagegif($dst_img); break;
case 2: imagejpeg($dst_img, NULL, 100); break; // best quality
case 3: imagepng($dst_img, NULL, 0); break; // no compression
default: echo ''; break;
}
$final_image = ob_get_contents();
ob_end_clean();
if ($image_type==1){
header("Content-Type: image/gif");
} else
if ($image_type==2){
header("Content-Type: image/jpeg");
} else
if ($image_type==3){
header("Content-Type: image/png");
}
print $final_image;
imagedestroy($dst_img);
imagedestroy($src_img);
}
?>