<?php
require_once("mainfile.php");
$charsession=strlen(session_id());
if ((strlen($_GET['pid'])>36 && $charsession==26) || (strlen($_GET['pid'])>42 && $charsession==32)) {

//if (strlen($_GET['pid'])>42) {
    //require_once("mainfile.php");
    //$timestamp=substr($_GET['pid'], 0,10);
    //$sessionid=substr($_GET['pid'], 10,32);
    //$photoid=substr($_GET['pid'], -(strlen($_GET['pid'])-42)); // act as filename in case 8, 10, 12
    switch($_GET['s']){
        case "1": // create photo histogram
            $validate=10;
            break;
        case "2": // create fullsize photo
            $validate=45;
            break;
        case "3": // create top photos
            $validate=120;
            break;
        case "4": // create crop photo
            $validate=30;
            break;
        case "5": // create title text
            $validate=20;
            break;
        case "6": // create phone wallpaper! insane! 
            $validate=3600;
            break;
        case "7": // create box photo 100x100px
            $validate=0;
            break;
        case "8": // create resized photo for gallery slide view 450x350px
            $validate=60;
            break;
        case "9": // create watermark image with AF! Logo
            $validate=60;
            break;
        case "10": // create box blog screenshot 100x100px
            $validate=120;
            break;
        case "11": // create box market screenshot 100x100px
            $validate=120;
            break;
        case "12": // create resized image for e-paper view 970x530px
            $validate=60;
            break;
        case "13": // create photo stock images with watermark
            $validate=600;
            break;
        case "14": // create Chaptca
            $validate=60;
            break;
        case "15": // create view 450x350px with watermark
            $validate=0;
            break;
        case "16": // zoom photo
            $validate=0;
            break;					
        case "17": // create license file
            $validate=0;
            break;
        case "18": // create watermark image wallpapper with small AF! Logo 
            $validate=60;
            break;
        case "19": // create FB thumbnail with small AF! Logo 
            $validate=0;
            break;
        case "20": // create fullsize photo_mobile
            $validate=0;
            break;
        case "21": // create watermark image mobile with small AF! Logo
            $validate=120;
            break;
        case "22": // create box photo 250x250px
            $validate=0;
            break;
        case "23": // create cache image 250x250px
            $validate=0;
            break;
        case "24": // create box photo mobile 250x250px
            $validate=0;
            break;
        case "25": // create box photo mobile 100x100px
            $validate=0;
            break;
	    case "26": // create slide photo mobile 
            $validate=0;
            break;
        case "27": // create fullsize group photo
            $validate=45;
            break;
        case "28": // create group box photo 100x100px
            $validate=120;
            break;
        case "29": // create resized group photo for gallery slide view 450x350px
            $validate=60;
            break;
        case "30": // create group box photo 250x250px
            $validate=30;
            break;
        case "31": // create group photo histogram
            $validate=30;
            break;
	    case "32": // create box photo shop 250x250px
            $validate=0;
            break;
	    case "33": // create fullsize page photo
            $validate=45;
            break;
        case "34": // create page box photo 100x100px
            $validate=120;
            break;
        case "35": // create resized page photo for gallery slide view 450x350px
            $validate=60;
            break;
        case "36": // create page box photo 250x250px
            $validate=30;
            break;
        case "37": // create page photo histogram
            $validate=30;
            break;
	    case "38": // create FB thumbnail group with small AF! Logo 
            $validate=0;
            break;
	    case "39": // create FB thumbnail page with small AF! Logo 
            $validate=0;
            break;
        case "40": // create FB Slide with small AF! Logo 
            $validate=0;
            break;
	    case "41": // create FB Slide group with small AF! Logo 
            $validate=0;
            break;
	    case "42": // create FB Slide page with small AF! Logo 
            $validate=0;
            break;
        case "43": // create resized page photo for gallery thumb view MAXIMUM_THUMB_WIDTH x MAXIMUM_THUMB_HEIGHT
            $validate=0;
            break;
        case "50": // create page box photo 100x100px
            $validate=0;
            break;	
        case "51": // create full photo 930px
            $validate=0;
            break;  
        default:
            $validate=5;
    } // switch
	
    $timestamp=substr($_GET['pid'], 0,10);
	if ($validate==0){
		if (strlen($_GET['pid'])>42){
			$sessionid=substr($_GET['pid'], 10,32);
			$photoid=substr($_GET['pid'], -(strlen($_GET['pid'])-42)); // act as filename in case 8, 10, 12, 23			
		}else{
			$sessionid=substr($_GET['pid'], 10,26);
			$photoid=substr($_GET['pid'], -(strlen($_GET['pid'])-36)); // act as filename in case 8, 10, 12, 23
		}
	}else{
		if ($charsession==26){
			$sessionid=substr($_GET['pid'], 10,26);
				$photoid=substr($_GET['pid'], -(strlen($_GET['pid'])-36)); // act as filename in case 8, 10, 12, 23
		}elseif($charsession==32) {
			$sessionid=substr($_GET['pid'], 10,32);
				$photoid=substr($_GET['pid'], -(strlen($_GET['pid'])-42)); // act as filename in case 8, 10, 12, 23
		}
	}
    if ($validate==0 || ((time()-$timestamp)<$validate && session_id()==$sessionid)) {
            if ($_GET['s']=='1') {
                $img2show=@sql_fetch_assoc(sql_query("select image, image_dim, memberID, post from photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                    Header("Content-type: image/png");
                    $h = $_GET["h"]; // Histogram channel (r=Red, g=Green, b=Blue)
                    $folder_date=date("Ymd", $img2show['post']).'/';
                    $img_src = imagecreatefromjpeg(PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                    $histR = array();
                    $histG = array();
                    $histB = array();
                    $s=explode("x", $img2show['image_dim']);
                    // Iterate through the image pixel-by-pixel, line-by-line
                    for ( $y=0; $y<$s[1]; $y++ ) {
                        for ( $x=0; $x<$s[0]; $x++ ) {
                            $rgb = imagecolorat($img_src, $x, $y);
                            $hex = sprintf("%06x", $rgb);
                            $histR[substr($hex,0,2)] ++;
                            $histG[substr($hex,2,2)] ++;
                            $histB[substr($hex,4,2)] ++;
                        }
                    }
                    // Convert histograms hex to decimal representation and get peak value
                    for( $x=0; $x<256; $x++ ) {
                        $hex = sprintf("%02x", $x);
                        $histRdec[$x] = $histR[$hex];
                        $histGdec[$x] = $histG[$hex];
                        $histBdec[$x] = $histB[$hex];
    
                        $maxR = max($maxR, $histR[$hex]);
                        $maxG = max($maxG, $histG[$hex]);
                        $maxB = max($maxB, $histB[$hex]);
                    }
                    // Choose the histogram according to the channel
                    if ( strtolower($h) == "r" ) {
                        $max  = $maxR;
                        $hist = $histRdec;
                    }
                    elseif ( strtolower($h) == "g" ) {
                        $max  = $maxG;
                        $hist = $histGdec;
                    }
                    elseif ( strtolower($h) == "b" ) {
                        $max  = $maxB;
                        $hist = $histBdec;
                    }
    
                    // Create the histogram
                    $imHist = imagecreate(256, 120);
                    $white = imagecolorallocate($imHist, 255, 255, 255);
                    $black = imagecolorallocate($imHist, 0, 0, 0);
                    $originX = 0;
    
                    for( $x=0; $x<256; $x++ ) {
                        if ( strtolower($h) == "r" )
                            $color = imagecolorallocate($imHist, $x, 0, 0);
                        elseif ( strtolower($h) == "g" )
                            $color = imagecolorallocate($imHist, 0, $x, 0);
                        elseif ( strtolower($h) == "b" )
                            $color = imagecolorallocate($imHist, 0, 0, $x);
                        imageline($imHist, $originX, 0, $originX, 100, $black);
                        imageline($imHist, $originX+$x, 119, $originX+$x, 100, $color);
                        imageline($imHist, $originX+$x, 100, $originX+$x, 100-100*($hist[$x]/$max), $black);
                    }
                        imagestring($imHist, 1, 2, 0, $max, $black);
                        imagestring($imHist, 1, 2, 100, "0", $white);
    
                    // Return the histogram
                    $finalHist = HISTOGRAM_FILE_PATH.str_replace(".jpg", "_".$h.".png", $img2show['image']);
                    imagepng($imHist, $finalHist);
                    Header("Content-type: image/png");
                    imagepng($imHist);
                    imagedestroy($imHist);
                }
            } else if ($_GET['s']=='2') {
                $img2show=@sql_fetch_assoc(sql_query("select image, memberID, post from photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                    Header("Content-type: image/jpeg");
                    $folder_date=date("Ymd", $img2show['post']).'/';
                    if ($_GET['flip']) {
                    $img_src = imagecreatefromjpeg(PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                    imageinterlace($img_src, 1);
                    $img_src=ImageFlip($img_src, $_GET['flip']);
                    Imagejpeg($img_src);
                    ImageDestroy($img_src);
                    } else {
                    $filename=PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image'];
                    $size = @getimagesize($filename);
                    $fp = @fopen($filename, "rb");
                        if ($size && $fp) {
                            fpassthru($fp);
                            exit;
                        }     
                    }
                }
            } else if ($_GET['s']=='3') {
                $img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb, image_dim, thumb_dim, memberID, post from photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                    Header("Content-type: image/jpeg");
                    $folder_date=date("Ymd", $img2show['post']).'/';
                    $img_src = imagecreatetruecolor(MAXIMUM_THUMB_SQUARE,MAXIMUM_THUMB_SQUARE);
                    $ims=explode("x", $img2show['thumb_dim']);
                    if ($ims[0]<MAXIMUM_THUMB_SQUARE || $ims[1]<MAXIMUM_THUMB_SQUARE) {
                            $ims=explode("x", $img2show['image_dim']);
                            $org_img = imagecreatefromjpeg(PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                    $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_SQUARE)/$ims[1]) : MAXIMUM_THUMB_SQUARE; // landscape
                    $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_SQUARE)/$ims[0]) : MAXIMUM_THUMB_SQUARE;  // portrait
                    $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_SQUARE/2));
                    $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_SQUARE/2));
                            ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);
                    } else {
                            $ims=explode("x", $img2show['thumb_dim']);
                            $thm_img = imagecreatefromjpeg(THUMBNAIL_FILE_PATH.$img2show['thumb']);
                    $startx=round(($ims[0]/2)-(MAXIMUM_THUMB_SQUARE/2));
                    $starty=round(($ims[1]/2)-(MAXIMUM_THUMB_SQUARE/2));
                    imagecopy($img_src,$thm_img, 0, 0, $startx, $starty, $ims[0], $ims[1]);
                    }
                    $grey = imagecolorallocate($img_src, 153,153,153);
                    $white = imagecolorallocate($img_src, 255,255,255);
                    imagettftext($img_src, 18, 0, 6, 26, $grey, TTF_FILE_PATH."arial.ttf", $_GET['t']);
                    imagettftext($img_src, 18, 0, 5, 25, $white, TTF_FILE_PATH."arial.ttf", $_GET['t']);
                    imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_SQUARE-1), (MAXIMUM_THUMB_SQUARE-1), $grey);
                    Imagejpeg($img_src);
                    Imagejpeg($img_src,TOP_PHOTO_FILE_PATH.str_replace(".jpg", "_top.jpg", $img2show['image']), 90);
                    ImageDestroy($img_src);
                }
            } else if ($_GET['s']=='4') {
                $img2show=@sql_fetch_assoc(sql_query("select image, memberID, post from photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                    Header("Content-type: image/jpeg");
                    $folder_date=date("Ymd", $img2show['post']).'/';
                    $img_src = imagecreatefromjpeg(PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                    imageinterlace($img_src, 1);
                    if ($_GET['flip']) {
                    $img_src=ImageFlip($img_src, $_GET['flip']);
                    }
                    Imagejpeg($img_src);
                    ImageDestroy($img_src);
                }
            } else if ($_GET['s']=='5') {
                Header("Content-type: image/png");
                $realWidth = 800;
                $realHeight = 70;
                $img_dest = imagecreatetruecolor($realWidth, $realHeight);
                $grey = imagecolorallocate($img_dest, 102,102,102);
                $white = imagecolorallocate($img_dest, 255,255,255);
                $black = imagecolorallocate($img_dest, 0, 0, 0);
                if ($_SESSION['style_color']=="white") {
                    imagefilledrectangle($img_dest, 0, 0, $realWidth, $realHeight, $white);
                } else {
                    imagefilledrectangle($img_dest, 0, 0, $realWidth, $realHeight, $black);
                }
                imagettftext($img_dest, 36, 0, 0, 40, $grey, TTF_FILE_PATH."pristine.ttf", rawurldecode($_GET['t']));
                Imagepng($img_dest);
                ImageDestroy($img_dest);
            } else if ($_GET['s']=='6') {
                $img2show=@sql_fetch_assoc(sql_query("select image, width, height, dst_x, dst_y, dst_w, dst_h, src_w, src_h from cellphone where ID='".$photoid."' limit 0,1"));
                $wallpaper=@sql_fetch_assoc(sql_query("select image from wallpaper where code='".$_GET['code']."' limit 0,1"));
                if ($img2show['image'] && $wallpaper['image']) {
                Header('Content-type: image/jpeg');
                // Phone Image
                list($width_cp, $height_cp) = getimagesize(CELLPHONE_FILE_PATH.$img2show['image']);
                $image_cp = imagecreatefromjpeg(CELLPHONE_FILE_PATH.$img2show['image']);
                $cp_img = imagecreatetruecolor($width_cp,$height_cp);
                imagecopy ($cp_img, $image_cp, 0, 0, 0, 0, $width_cp, $height_cp);
                // Resample
                $width_wp=$img2show['width'];
                $height_wp=$img2show['height'];
                $new_width=$img2show['dst_w'];
                $new_height=$img2show['dst_h'];
                $image_wp = imagecreatefromjpeg(WALLPAPER_FILE_PATH.$wallpaper['image']);
                ImageCopyResampled($cp_img, $image_wp, $img2show['dst_x'], $img2show['dst_y'], 0, 0, $new_width, $new_height, $width_wp, $height_wp);
                // Output
                Imagejpeg($cp_img);
                ImageDestroy($cp_img);
                }
            } else if ($_GET['s']=='7') {
                $img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb, image_dim, thumb_dim, memberID, post from photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_SQUARE,MAXIMUM_THUMB_SQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey);
		        $ims=explode("x", $img2show['thumb_dim']);
                if ($ims[0]<MAXIMUM_THUMB_SQUARE || $ims[1]<MAXIMUM_THUMB_SQUARE) {
                $ims=array();$ims=explode("x", $img2show['image_dim']);
                $org_img = imagecreatefromjpeg(PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_SQUARE)/$ims[1]) : MAXIMUM_THUMB_SQUARE; // landscape
                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_SQUARE)/$ims[0]) : MAXIMUM_THUMB_SQUARE;  // portrait
                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_SQUARE/2));
                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_SQUARE/2));
                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);
                } else {
                $thm_img = imagecreatefromjpeg(THUMBNAIL_FILE_PATH.$img2show['thumb']);
                $startx=round(($ims[0]/2)-(MAXIMUM_THUMB_SQUARE/2));
                $starty=round(($ims[1]/2)-(MAXIMUM_THUMB_SQUARE/2));
                imagecopy($img_src,$thm_img, 0, 0, $startx, $starty, $ims[0], $ims[1]);
                }
                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_SQUARE-1), (MAXIMUM_THUMB_SQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,BOX_PHOTO_FILE_PATH.str_replace(".jpg", "_box.jpg", $img2show['image']), 90);
                ImageDestroy($img_src);
                }
            } else if ($_GET['s']=='8') {
                Header("Content-type: image/jpeg");
                if ($_GET['fid']>0) {
                $ims=getimagesize(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
                $width_orig=$ims[0];
                $height_orig=$ims[1];
                if ($ims[mime]=="image/png") $image = imagecreatefrompng(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
                if ($ims[mime]=="image/jpeg") $image = imagecreatefromjpeg(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
                $filename=CACHE_IMG_FILE_PATH.'/file_'.$_GET['fid'].'_slide.jpg';
                } else if ($_GET['aid']>0) {
                $ims=getimagesize(ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
                $width_orig=$ims[0];
                $height_orig=$ims[1];
                if ($ims[mime]=="image/png") $image = imagecreatefrompng(ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
                if ($ims[mime]=="image/jpeg") $image = imagecreatefromjpeg(ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
                $filename=CACHE_IMG_FILE_PATH.'/blog_'.$_GET['aid'].'_slide.jpg'; 
                } else {
                $img2show=sql_fetch_assoc(sql_query("select image, image_dim, memberID, post from photo where ID='".$photoid."'"));
                $folder_date=date("Ymd", $img2show['post']).'/';
                list($width_orig, $height_orig) = explode('x', $img2show['image_dim']);
                $image = imagecreatefromjpeg(PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                $filename=SLIDE_PHOTO_FILE_PATH.str_replace(".jpg", "_slide.jpg", $img2show['image']);
                }

                $ratio_orig = $width_orig/$height_orig;
                $width=MAXIMUM_PHOTO_SLIDE_WIDTH;$height=MAXIMUM_PHOTO_SLIDE_HEIGHT;
                if ($width/$height > $ratio_orig) {
                    $width = $height*$ratio_orig;
                } else {
                    $height = $width/$ratio_orig;
                }
                $img_src = imagecreatetruecolor($width, $height);
                
                imagecopyresampled($img_src, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                Imagejpeg($img_src); 
                Imagejpeg($img_src, $filename, 90);
                ImageDestroy($img_src);
            } else if ($_GET['s']=='9') {
                $img2show=@sql_fetch_assoc(sql_query("select image from wallpaper where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                $ih = getimagesize(WALLPAPER_FILE_PATH.$img2show['image']);
                    $watermark = imagecreatefrompng('images/ayofoto_watermark.png');
			         $watermark_width = imagesx($watermark);
			         $watermark_height = imagesy($watermark);
                    $alpha=4;
                    $image = imagecreatetruecolor($watermark_width, $watermark_height);
                
                $ispng = $ih[2] == 3;
                if ($ispng) {
                    $image = imagecreatefrompng(WALLPAPER_FILE_PATH.$img2show['image']);
                } else {
                    $image = imagecreatefromjpeg(WALLPAPER_FILE_PATH.$img2show['image']);
                }
 
				$dest_x = $ih[0];
				$dest_y = $ih[1];  

				$c_dest_x = 0;
				$c_dest_y = 0;

				// loop/tile watermark until image full
				while ($dest_x > $c_dest_x && $dest_y >= $c_dest_y) {
					imagecopymerge($image, $watermark, $c_dest_x, $c_dest_y, 0, 0, $watermark_width, $watermark_height, $alpha);

					if ($c_dest_x < $dest_x){
						$c_dest_x = $c_dest_x + $watermark_width;
					}

					if ($c_dest_x >= $dest_x && $c_dest_y <= $dest_y){
						$c_dest_x = 0;
						$c_dest_y = $c_dest_y + $watermark_height;
					}
				}

                    if ($ispng) {
                    Header("Content-type: image/png");
                    imagepng($image);
                    } else {
                    Header("Content-type: image/jpeg");
                    imagejpeg($image);
                    }
                    imagedestroy($image);
                    imagedestroy($watermark);
                }
            } else if ($_GET['s']=='10') {
                Header("Content-type: image/jpeg");
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_SQUARE,MAXIMUM_THUMB_SQUARE);
                if ($_GET['aid']>0) {
                $ims=getimagesize(ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
                if ($ims[mime]=="image/png") $org_img = imagecreatefrompng(ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
                if ($ims[mime]=="image/jpeg") $org_img = imagecreatefromjpeg(ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
                $filename=CACHE_IMG_FILE_PATH.'/blog_'.$_GET['aid'].'_screenshot.jpg';
                } else if ($_GET['fid']>0) {
                $ims=getimagesize(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
                if ($ims[mime]=="image/png") $org_img = imagecreatefrompng(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
                if ($ims[mime]=="image/jpeg") $org_img = imagecreatefromjpeg(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
                $filename=CACHE_IMG_FILE_PATH.'/file_'.$_GET['fid'].'_screenshot.jpg';
                } else if ($_GET['eid']>0) {
                $ims=getimagesize(EVENT_PROFILE_FILE_PATH.$_GET['eid'].'/'.$photoid);
                if ($ims[mime]=="image/png") $org_img = imagecreatefrompng(EVENT_PROFILE_FILE_PATH.$_GET['eid'].'/'.$photoid);
                if ($ims[mime]=="image/jpeg") $org_img = imagecreatefromjpeg(EVENT_PROFILE_FILE_PATH.$_GET['eid'].'/'.$photoid);
                $filename=CACHE_IMG_FILE_PATH.'/event_'.$_GET['eid'].'_screenshot.jpg';
                }
                                $grey = imagecolorallocate($img_src, 153,153,153);
                                $white = imagecolorallocate($img_src, 255,255,255);
                                Imagefill($img_src, 0, 0, $grey);
                                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_SQUARE)/$ims[1]) : MAXIMUM_THUMB_SQUARE; // landscape
                                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_SQUARE)/$ims[0]) : MAXIMUM_THUMB_SQUARE;  // portrait
                                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_SQUARE/2));
                                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_SQUARE/2));
                                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);
                                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_SQUARE-1), (MAXIMUM_THUMB_SQUARE-1), $grey);
                                imageinterlace($img_src, 1);
                Imagejpeg($img_src);              
                Imagejpeg($img_src, $filename, 90);  
                ImageDestroy($img_src);
            } else if ($_GET['s']=='11') {
                Header("Content-type: image/jpeg");
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_SQUARE,MAXIMUM_THUMB_SQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey);
                $org_img = imagecreatefromjpeg(MARKET_IMAGE_FILE_PATH.$photoid.'.jpg');
                $ims=getimagesize(MARKET_IMAGE_FILE_PATH.$photoid.'.jpg');
                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_SQUARE)/$ims[1]) : MAXIMUM_THUMB_SQUARE; // landscape
                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_SQUARE)/$ims[0]) : MAXIMUM_THUMB_SQUARE;  // portrait
                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_SQUARE/2));
                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_SQUARE/2));
                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);
                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_SQUARE-1), (MAXIMUM_THUMB_SQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,CACHE_IMG_FILE_PATH."market_".$photoid."_screenshot.jpg", 90);
                ImageDestroy($img_src);
            } else if ($_GET['s']=='12' && $_GET['fid']>0) {
                Header("Content-type: image/jpeg");
                if (file_exists(LIB_FILE_PATH.$_GET['fid'].'/preview_'.$photoid) && file_exists(LIB_FILE_PATH.$_GET['fid'].'/thm_'.$photoid)) {
                //t=thumbnail, p=preview, f=full
                        if ($_GET['t']=="p") {
                        $filename=LIB_FILE_PATH.$_GET['fid'].'/preview_'.$photoid;
                        } else if ($_GET['t']=="f") {
                        $filename=LIB_FILE_PATH.$_GET['fid'].'/'.$photoid;
                        } else {
                        $filename=LIB_FILE_PATH.$_GET['fid'].'/thm_'.$photoid;
                        }
                $fp = @fopen($filename, "rb");
                    if ($fp) {
                        fpassthru($fp);
                        exit;
                    }
                    
                } else {
                $ims=getimagesize(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
                $width_orig=$ims[0];
                $height_orig=$ims[1];
                if ($ims[mime]=="image/png") $image = imagecreatefrompng(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
                if ($ims[mime]=="image/jpeg") $image = imagecreatefromjpeg(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
                $filename=LIB_FILE_PATH.$_GET['fid'].'/preview_'.$photoid;
                $ratio_orig = $width_orig/$height_orig;
                $width=MAXIMUM_LIB_PREVIEW_WIDTH;$height=MAXIMUM_LIB_PREVIEW_HEIGHT;
                if ($width/$height > $ratio_orig) {
                    $width = $height*$ratio_orig;
                } else {
                    $height = $width/$ratio_orig;
                }
                $img_src = imagecreatetruecolor($width, $height);
                
                imagecopyresampled($img_src, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);                
                Imagejpeg($img_src,$filename, 90);

                $img_thm = imagecreatetruecolor(48,48);
                $grey = imagecolorallocate($img_thm, 153,153,153);
                Imagefill($img_thm, 0, 0, $grey);
                $thumb_x = ($width_orig >= $height_orig) ? round(($width_orig * 48)/$height_orig) : 48; // landscape
                $thumb_y = ($width_orig < $height_orig) ? round(($height_orig * 48)/$width_orig) : 48;  // portrait
                $startx=round(($thumb_x/2)-(48/2));
                $starty=round(($thumb_y/2)-(48/2));
                ImageCopyResampled($img_thm, $image, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $width_orig, $height_orig);
                imagerectangle($img_thm, 0 , 0, 47, 47, $grey);
                Imagejpeg($img_src);
                Imagejpeg($img_thm,LIB_FILE_PATH.$_GET['fid'].'/thm_'.$photoid);
                ImageDestroy($img_src);
                }

            } else if ($_GET['s']=='13') {
                $img2show=@sql_fetch_assoc(sql_query("select image, memberID, image_dim, post from photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
                                list($dest_x, $dest_y) = explode('x', $img2show['image_dim']);
				$watermark = imagecreatefromgif('images/ayofoto_watermark.gif');
				$watermark_width = imagesx($watermark);
				$watermark_height = imagesy($watermark);
				$alpha=15;
				$image = imagecreatetruecolor($watermark_width, $watermark_height);
				$image = imagecreatefromjpeg(PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);

				$c_dest_x = 0;
				$c_dest_y = 0;

                                if ($_GET['flip']) {
                                $image=ImageFlip($image, $_GET['flip']);
                                }

				// loop/tile watermark until image full
				while ($dest_x > $c_dest_x && $dest_y >= $c_dest_y) {
					imagecopymerge($image, $watermark, $c_dest_x, $c_dest_y, 0, 0, $watermark_width, $watermark_height, $alpha);

					if ($c_dest_x < $dest_x){
						$c_dest_x = $c_dest_x + $watermark_width;
					}

					if ($c_dest_x >= $dest_x && $c_dest_y <= $dest_y){
						$c_dest_x = 0;
						$c_dest_y = $c_dest_y + $watermark_height;
					}
				}

				imagejpeg($image);
				imagedestroy($image);
				imagedestroy($watermark);
                }
            } else if($_GET['s']=='14'){
				
			class CaptchaSecurityImages {
				  /* select the type of font, must be used in directoy in which script is being called into */
				  var $font = 'includes/ttf/tahoma.ttf';

				  function generateCode($characters) {
				    $possible = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
				    $possible = $possible.$possible.'2345678923456789';
				    $code = '';
				    $i = 0;
				    while ($i < $characters) {
				      $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
				      $i++;
				    }
				    return $code;
				  }

				  function CaptchaSecurityImages($width = 145,$height = 35, $characters = 6) {
				    $code = $this->generateCode($characters);
				    $font_size = $height * 0.60;
				    $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');

				    /* set the colours */
				    //$bgR = mt_rand(0, 255); $bgG = mt_rand(0, 255); $bgB = mt_rand(0, 255);
				    //$background_color = imagecolorallocate($image, $bgR, $bgG, $bgB);
				    //$noise_color = imagecolorallocate($image, abs(100 - $bgR), abs(100 - $bgG), abs(100 - $bgB));
				    //$text_color = imagecolorallocate($image, abs(255 - $bgR), abs(255 - $bgG), abs(255 - $bgB));

				    //membuat bg putih
				    $background_color = imagecolorallocate($image, 128, 128, 128);
				    $noise_color = imagecolorallocate($image, 128, 128, 128);
				    $text_color = imagecolorallocate($image, 255, 255, 255);

				    /* generate random dots in background */
				    for($i = 0; $i < ($width*$height) / 3; $i++) {
				      imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
				    }

				    /* generate random lines in background */
				    for($i = 0; $i < ($width*$height) / 150; $i++) {
				      imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
				    }

				    /* set random colors */
				    $w = imagecolorallocate($image, abs(100 - $bgR), abs(100 - $bgG), abs(100 - $bgB));
				    $r = imagecolorallocate($image, abs(100 - $bgR), abs(100 - $bgG), abs(100 - $bgB));

				    /* Draw a dashed line, 5 red pixels, 5 white pixels */
				    $style = array($r, $r, $r, $r, $r, $w, $w, $w, $w, $w);
				    imagesetstyle($image, $style);
				    imageline($image, 0, 0, $width, $height, IMG_COLOR_STYLED);
				    imageline($image, $width, 0, 0, $height, IMG_COLOR_STYLED);

				    /* create random polygon points */
				    $values = array(
				        mt_rand(0, $width), mt_rand(0, $height),
				        mt_rand(0, $height), mt_rand(0, $width),
				        mt_rand(0, $width), mt_rand(0, $height),
				        mt_rand(0, $height), mt_rand(0, $width),
				        mt_rand(0, $width), mt_rand(0, $height),
				        mt_rand(0, $height), mt_rand(0, $width),
				        mt_rand(0, $width), mt_rand(0, $height),
				        mt_rand(0, $height), mt_rand(0, $width),
				        mt_rand(0, $width), mt_rand(0, $height),
				        mt_rand(0, $height), mt_rand(0, $width),
				        mt_rand(0, $width), mt_rand(0, $height),
				        mt_rand(0, $height), mt_rand(0, $width),);

				    /* create Random Colors then set it to $clr */
				    $r = abs(100 - mt_rand(0, 255));
				    $g = abs(100 - mt_rand(0, 255));
				    $b = abs(100 - mt_rand(0, 255));
				    //$clr = imagecolorallocate($image, $r, $g, $b);
				    $clr = imagecolorallocate($image, 128, 128, 128);

				    /* create filled polygon with random points */
				    imagefilledpolygon($image, $values, 6, $clr);

				    /* set starting CenterX and CenterY
				    $w = $width-($width/2);
				    $h = $height-($height/2);

				    /* create "bulls-eye" type ellipses
				    imageellipse($image, ($width/2), ($height/2), $w, $h, $clr); $w -= 7; $h -= 7;
				    imageellipse($image, ($width/2), ($height/2), $w, $h, $clr); $w -= 7; $h -= 7;
				    imageellipse($image, ($width/2), ($height/2), $w, $h, $clr); $w -= 7; $h -= 7;
				    imageellipse($image, ($width/2), ($height/2), $w, $h, $clr);

				    /* create textbox and add text */
				    $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
				    $x = ($width - $textbox[4])/2;
				    $y = ($height - $textbox[5])/2;
				    imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');

				    /* pretty it */
				    //imageantialias($image, 100);
				    //imagealphablending($image, 1);
				    //imagelayereffect($image, IMG_EFFECT_OVERLAY);

				    /* output captcha image to browser */
				    header('Content-Type: image/jpeg');
				    imagejpeg($image);
				    imagedestroy($image);
				    $_SESSION['key'] = $code;
				  }
				}

				$captcha = new CaptchaSecurityImages(110, 20, rand(4, 6));	
            } else if ($_GET['s']=='15') {
				$img2show=@sql_fetch_assoc(sql_query("select image, image_dim, memberID, post from photo where ID='".$photoid."' limit 0,1"));
				if ($img2show['image']) {
				Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
				$watermark = imagecreatefrompng('images/ayofoto_watermark2.png');
				$watermark_width = imagesx($watermark);
				$watermark_height = imagesy($watermark);
				$filename=PREVIEW_PHOTO_FILE_PATH.str_replace(".jpg", "_prev.jpg", $img2show['image']);

				list($width_orig, $height_orig) = explode('x', $img2show['image_dim']);
				$ratio_orig = $width_orig/$height_orig;
				$width=MAXIMUM_PHOTO_SLIDE_WIDTH;$height=MAXIMUM_PHOTO_SLIDE_HEIGHT;
				if ($width/$height > $ratio_orig) {
					$width = $height*$ratio_orig;
				} else {
					$height = $width/$ratio_orig;
				}
				$image = imagecreatefromjpeg(PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
				$c_dest_x=($width/2)-($watermark_width/2);
				$c_dest_y=($height/2)-($watermark_height/2);
				
				$img_src = imagecreatetruecolor($width, $height);
				imagecopyresampled($img_src, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);				
				imagecopy($img_src, $watermark, $c_dest_x, $c_dest_y, 0, 0, $watermark_width, $watermark_height); 
				imagejpeg($img_src);
				Imagejpeg($img_src,$filename, 90);
				imagedestroy($img_src);
				imagedestroy($watermark);
                }
	       } else if ($_GET['s']=='16') {
                $img2show=@sql_fetch_assoc(sql_query("select p.ID, p.title, p.image, p.image_dim, p.memberID, p.post, s.pixel_width, s.pixel_height from (photo p, photo_stock s) where p.ID='".$photoid."' and s.photoID=p.ID limit 0,1"));				
                if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
				$watermark = imagecreatefromgif('images/ayofoto_watermark.gif');
				$watermark_width = imagesx($watermark);
				$watermark_height = imagesy($watermark);
				$alpha=15;	

				
				$img_dim=explode("x", $img2show['image_dim']);
				$img_src = imagecreatetruecolor($img_dim[0],$img_dim[1]);
				$org_img = imagecreatefromjpeg(STOCK_PHOTO_FILE_PATH.$folder_date.$img2show['memberID']."/".$img2show['image']);

				$ratio=$img2show['pixel_width']/$img_dim[0];
				$pos=explode("_", base64_decode($_GET['n']));

				$source_x=round($pos[0]*$ratio);$source_y=round($pos[1]*$ratio);
				$source_w=$img_dim[0]; $source_h=$img_dim[1];
				
				ImageCopy($img_src, $org_img, 0, 0, $source_x, $source_y, $img_dim[0], $img_dim[1]);

				$dest_x = $img_dim[0];
				$dest_y = $img_dim[1];

				$c_dest_x = 0;
				$c_dest_y = 0;

				// loop/tile watermark until image full
				while ($dest_x > $c_dest_x && $dest_y >= $c_dest_y) {
					imagecopymerge($img_src, $watermark, $c_dest_x, $c_dest_y, 0, 0, $watermark_width, $watermark_height, $alpha);
					if ($c_dest_x < $dest_x){
						$c_dest_x = $c_dest_x + $watermark_width;
					}
					if ($c_dest_x >= $dest_x && $c_dest_y <= $dest_y){
						$c_dest_x = 0;
						$c_dest_y = $c_dest_y + $watermark_height;
					}
				}
				imagejpeg($img_src);
				imagedestroy($img_src);
				imagedestroy($watermark);
                }
	       } else if ($_GET['s']=='17') {	
				if ($_SESSION['login_memberID']==$_GET['mid']){
					$img2show=sql_fetch_assoc(sql_query("select s.ID, p.memberID, p.image, p.post, s.release_file from photo p, photo_stock s where p.ID='".$photoid."' and s.photoID=p.ID limit 0,1"));
					if ($img2show['image']) {
                        $folder_date=date("Ymd", $img2show['post']).'/';
                        $filename=STOCK_PHOTO_LICENSE_FILE_PATH.$img2show['memberID'].'/'.$img2show['release_file'];
                        $fp = @fopen($filename, "rb");
                        if ($fp) {
                                header ("Content-type: image/jpeg");
                                header("Content-Disposition: inline; filename=\"".$imgshow['ID']."\"");
                                fpassthru($fp);
                                fclose($fp);
                                exit;
                        }
                    }
				}
            } else if ($_GET['s']=='18') {
                $img2show=@sql_fetch_assoc(sql_query("select image from wallpaper where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                $ih = getimagesize(WALLPAPER_FILE_PATH.$img2show['image']);
                
                        //$watermark = imagecreatefrompng('images/ayofoto_watermark.png');
                        $watermark = imagecreatefromgif('images/ayofoto_watermark150.gif');
                        $watermark_width = imagesx($watermark);
                        $watermark_height = imagesy($watermark);
                        $alpha=40;
                        $image = imagecreatetruecolor($watermark_width, $watermark_height);
                
                $ispng = $ih[2] == 3;
                if ($ispng) {
                    $image = imagecreatefrompng(WALLPAPER_FILE_PATH.$img2show['image']);
                } else {
                    $image = imagecreatefromjpeg(WALLPAPER_FILE_PATH.$img2show['image']);
                }
				$dest_x = $ih[0];
				$dest_y = $ih[1];  

				$c_dest_x = 0;
				$c_dest_y = 0;

				// loop/tile watermark until image full
				while ($dest_x > $c_dest_x && $dest_y >= $c_dest_y) {
					imagecopymerge($image, $watermark, $c_dest_x, $c_dest_y, 0, 0, $watermark_width, $watermark_height, $alpha);

					if ($c_dest_x < $dest_x){
						$c_dest_x = $c_dest_x + $watermark_width;
					}

					if ($c_dest_x >= $dest_x && $c_dest_y <= $dest_y){
						$c_dest_x = 0;
						$c_dest_y = $c_dest_y + $watermark_height;
					}
				}

                    if ($ispng) {
                    Header("Content-type: image/png");
                    imagepng($image);
                    } else {
                    Header("Content-type: image/jpeg");
                    imagejpeg($image);
                    }
                    imagedestroy($image);
                    imagedestroy($watermark);
                }
	    } else if ($_GET['s']=='19') {
                $img2show=sql_fetch_assoc(sql_query("select p.title, CONCAT(SUBSTRING_INDEX(p.image,'.',1), '_thm.jpg') as thumb, p.thumb_dim, p.memberID, p.topdaily as pod, p.topmonthly as pom, p.download from photo p where p.ID='".$photoid."' limit 0,1"));
                if ($img2show['thumb']) {
		  Header("Content-type: image/jpeg");
                unset($img_stamp, $img_logo, $img_mobile);
                    $img_thumb=THUMBNAIL_FILE_PATH.$img2show['thumb'];
                    if ($img2show['pom']!='0000-00-00') {
                        $img_stamp=ABS_PATH.'images/p-pom.png';
                    } else if ($img2show['pod']!='0000-00-00') {
                        $img_stamp=ABS_PATH.'images/p-pod.png';
                    } else {
                        $img_stamp='';
                    }
                    $img_logo=ABS_PATH.'images/af_box_icon.jpg';
                    $img_mobile=ABS_PATH.'images/mobile_icon.png';
                    
                        $logo = new imagick($img_logo);
                        $im = new imagick($img_thumb);
                        $im->setImageCompression(Imagick::COMPRESSION_JPEG);
                        $im->setImageCompressionQuality(90); 
                        $im->setImageColorspace($logo->getImageColorspace() );
                        $im->cropThumbnailImage( 95, 112);
			             $im->adaptiveSharpenImage(0,1);
                        $identify = $im->identifyImage();
                        $im->compositeImage($logo, $logo->getImageCompose(), ($identify['geometry']['width']-20), ($identify['geometry']['height']-20));
                        if ($img_stamp) {
                            $stamp = new imagick($img_stamp);
                            $im->setImageColorspace($stamp->getImageColorspace() );
                            $im->compositeImage($stamp, $stamp->getImageCompose(), 0, 0); 
                        }
                        if ($img2show['download']=='3') {
                            $hp = new imagick($img_mobile);
                            $im->setImageColorspace($hp->getImageColorspace() );
                            $im->compositeImage($hp, $hp->getImageCompose(), ($identify['geometry']['width']-43), ($identify['geometry']['height']-23));
                        }

                        echo $im;
                        $im->clear();$im->destroy();
                }
            } else if ($_GET['s']=='20') {
                $img2show=@sql_fetch_assoc(sql_query("select image, memberID, post from photo_mobile where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                    Header("Content-type: image/jpeg");
                    $folder_date=date("Ymd", $img2show['post']).'/';
                    if ($_GET['flip']) {
                    $img_src = imagecreatefromjpeg(PHOTO_MOBILE_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                    imageinterlace($img_src, 1);
                    $img_src=ImageFlip($img_src, $_GET['flip']);
                    Imagejpeg($img_src);
                    ImageDestroy($img_src);
                    } else {
                    $filename=PHOTO_MOBILE_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image'];
                    $size = @getimagesize($filename);
                    $fp = @fopen($filename, "rb");
                        if ($size && $fp) {
                            fpassthru($fp);
                            exit;
                        }     
                    }
                }				
            } else if ($_GET['s']=='21') {
                $img2show=sql_fetch_assoc(sql_query("select CONCAT(SUBSTRING_INDEX(p.image,'.',1), '_thm.jpg') as thumb, p.thumb_dim from photo_mobile p where p.ID='".$photoid."' limit 0,1"));
                if ($img2show['thumb']) {
		Header("Content-type: image/jpeg");
                unset($img_logo);
                    $img_thumb=THUMBNAIL_MOBILE_FILE_PATH.$img2show['thumb'];
                    $img_logo=ABS_PATH.'images/af_box_icon.jpg';
                    
                        $logo = new imagick($img_logo);
                        $im = new imagick($img_thumb);
                        $im->setImageCompression(Imagick::COMPRESSION_JPEG);
                        $im->setImageCompressionQuality(90); 
                        $im->setImageColorspace($logo->getImageColorspace() );
                        $im->ThumbnailImage( 130, 130, true);
                        $identify = $im->identifyImage();
                        $im->compositeImage($logo, $logo->getImageCompose(), ($identify['geometry']['width']-20), ($identify['geometry']['height']-20));
                        echo $im;
                        $im->clear();$im->destroy();
                }
            } else if ($_GET['s']=='22') {
                $img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb, image_dim, thumb_dim, memberID, post from photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_BIGSQUARE,MAXIMUM_THUMB_BIGSQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey);
				$ims=explode("x", $img2show['image_dim']);

                $org_img = imagecreatefromjpeg(PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_BIGSQUARE)/$ims[1]) : MAXIMUM_THUMB_BIGSQUARE; // landscape
                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_BIGSQUARE)/$ims[0]) : MAXIMUM_THUMB_BIGSQUARE;  // portrait
                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);

                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_BIGSQUARE-1), (MAXIMUM_THUMB_BIGSQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,BIGBOX_PHOTO_FILE_PATH.str_replace(".jpg", "_bigbox.jpg", $img2show['image']), 90);
                ImageDestroy($img_src);
                }
            } else if ($_GET['s']=='23') {
                Header("Content-type: image/jpeg");
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_BIGSQUARE,MAXIMUM_THUMB_BIGSQUARE);
                if ($_GET['aid']>0) {
					$ims=getimagesize(ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
					if ($ims[mime]=="image/png") $org_img = imagecreatefrompng(ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
					if ($ims[mime]=="image/jpeg") $org_img = imagecreatefromjpeg(ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
					$filename=CACHE_IMG_FILE_PATH.'/blog_'.$_GET['aid'].'_bigscreenshot.jpg';
                } else if ($_GET['fid']>0) {
					$ims=getimagesize(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
					if ($ims[mime]=="image/png") $org_img = imagecreatefrompng(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
					if ($ims[mime]=="image/jpeg") $org_img = imagecreatefromjpeg(LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
					$filename=CACHE_IMG_FILE_PATH.'/file_'.$_GET['fid'].'_bigscreenshot.jpg';
                } else if ($_GET['eid']>0) {
					$ims=getimagesize(EVENT_PROFILE_FILE_PATH.$_GET['eid'].'/'.$photoid);
					if ($ims[mime]=="image/png") $org_img = imagecreatefrompng(EVENT_PROFILE_FILE_PATH.$_GET['eid'].'/'.$photoid);
					if ($ims[mime]=="image/jpeg") $org_img = imagecreatefromjpeg(EVENT_PROFILE_FILE_PATH.$_GET['eid'].'/'.$photoid);
					$filename=CACHE_IMG_FILE_PATH.'/event_'.$_GET['eid'].'_bigscreenshot.jpg';
                } else if ($_GET['mid']>0) {
					$ims=getimagesize(MARKET_IMAGE_FILE_PATH.'/'.$_GET['mid'].'.jpg');
					$org_img = imagecreatefromjpeg(MARKET_IMAGE_FILE_PATH.'/'.$_GET['mid'].'.jpg');
					$filename=CACHE_IMG_FILE_PATH.'/market_'.$_GET['mid'].'_bigscreenshot.jpg';
                } else if ($_GET['geid']>0) {
					$ims=getimagesize(GROUP_EVENT_FILE_PATH.$_GET['geid'].'/'.$photoid);
					if ($ims[mime]=="image/png") $org_img = imagecreatefrompng(GROUP_EVENT_FILE_PATH.$_GET['geid'].'/'.$photoid);
					if ($ims[mime]=="image/jpeg") $org_img = imagecreatefromjpeg(GROUP_EVENT_FILE_PATH.$_GET['geid'].'/'.$photoid);
					$filename=CACHE_IMG_FILE_PATH.'/group_event_'.$_GET['geid'].'_bigscreenshot.jpg';
                } else if ($_GET['peid']>0) {
					$ims=getimagesize(PAGE_EVENT_FILE_PATH.$_GET['peid'].'/'.$photoid);
					if ($ims[mime]=="image/png") $org_img = imagecreatefrompng(PAGE_EVENT_FILE_PATH.$_GET['peid'].'/'.$photoid);
					if ($ims[mime]=="image/jpeg") $org_img = imagecreatefromjpeg(PAGE_EVENT_FILE_PATH.$_GET['peid'].'/'.$photoid);
					$filename=CACHE_IMG_FILE_PATH.'/page_event_'.$_GET['peid'].'_bigscreenshot.jpg';				
                }
                                $grey = imagecolorallocate($img_src, 153,153,153);
                                $white = imagecolorallocate($img_src, 255,255,255);
                                Imagefill($img_src, 0, 0, $grey);
                                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_BIGSQUARE)/$ims[1]) : MAXIMUM_THUMB_BIGSQUARE; // landscape
                                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_BIGSQUARE)/$ims[0]) : MAXIMUM_THUMB_BIGSQUARE;  // portrait
                                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);
                                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_BIGSQUARE-1), (MAXIMUM_THUMB_BIGSQUARE-1), $grey);
                                imageinterlace($img_src, 1);
                Imagejpeg($img_src);              
                Imagejpeg($img_src, $filename, 90);  
                ImageDestroy($img_src);
            } else if ($_GET['s']=='24') {
                $img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb, image_dim, thumb_dim, memberID, post from photo_mobile where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_BIGSQUARE,MAXIMUM_THUMB_BIGSQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey);

                $ims=explode("x", $img2show['image_dim']);
                $org_img = imagecreatefromjpeg(PHOTO_MOBILE_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_BIGSQUARE)/$ims[1]) : MAXIMUM_THUMB_BIGSQUARE; // landscape
                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_BIGSQUARE)/$ims[0]) : MAXIMUM_THUMB_BIGSQUARE;  // portrait
                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);

                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_BIGSQUARE-1), (MAXIMUM_THUMB_BIGSQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,BIGBOX_PHOTO_MOBILE_FILE_PATH.str_replace(".jpg", "_bigbox.jpg", $img2show['image']), 90);
                ImageDestroy($img_src);
                }
            } else if ($_GET['s']=='25') {
                $img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb, image_dim, thumb_dim, memberID, post from photo_mobile where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_SQUARE,MAXIMUM_THUMB_SQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey);
				$ims=explode("x", $img2show['thumb_dim']);
                if ($ims[0]<MAXIMUM_THUMB_SQUARE || $ims[1]<MAXIMUM_THUMB_SQUARE) {
				$ims=array();$ims=explode("x", $img2show['image_dim']);
                $org_img = imagecreatefromjpeg(PHOTO_MOBILE_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_SQUARE)/$ims[1]) : MAXIMUM_THUMB_SQUARE; // landscape
                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_SQUARE)/$ims[0]) : MAXIMUM_THUMB_SQUARE;  // portrait
                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_SQUARE/2));
                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_SQUARE/2));
                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);
                } else {
                $thm_img = imagecreatefromjpeg(THUMBNAIL_MOBILE_FILE_PATH.$img2show['thumb']);
                $startx=round(($ims[0]/2)-(MAXIMUM_THUMB_SQUARE/2));
                $starty=round(($ims[1]/2)-(MAXIMUM_THUMB_SQUARE/2));
                imagecopy($img_src,$thm_img, 0, 0, $startx, $starty, $ims[0], $ims[1]);
                }
                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_SQUARE-1), (MAXIMUM_THUMB_SQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,BOX_PHOTO_MOBILE_FILE_PATH.str_replace(".jpg", "_box.jpg", $img2show['image']), 90);
                ImageDestroy($img_src);
                }
		} else if ($_GET['s']=='26') {
                Header("Content-type: image/jpeg");
                
                $img2show=sql_fetch_assoc(sql_query("select image, image_dim, memberID, post from photo_mobile where ID='".$photoid."'"));
                $folder_date=date("Ymd", $img2show['post']).'/';
                list($width_orig, $height_orig) = explode('x', $img2show['image_dim']);
                $image = imagecreatefromjpeg(PHOTO_MOBILE_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                $filename=SLIDE_PHOTO_MOBILE_FILE_PATH.str_replace(".jpg", "_slide.jpg", $img2show['image']);
                

                $ratio_orig = $width_orig/$height_orig;
                $width=MAXIMUM_PHOTO_SLIDE_WIDTH;$height=MAXIMUM_PHOTO_SLIDE_HEIGHT;
                if ($width/$height > $ratio_orig) {
                    $width = $height*$ratio_orig;
                } else {
                    $height = $width/$ratio_orig;
                }
                $img_src = imagecreatetruecolor($width, $height);
                
                imagecopyresampled($img_src, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                Imagejpeg($img_src); 
                Imagejpeg($img_src, $filename, 90);
                ImageDestroy($img_src);
            } else if ($_GET['s']=='27') {
                $img2show=@sql_fetch_assoc(sql_query("select image, memberID, post from group_photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                    Header("Content-type: image/jpeg");
                    $folder_date=date("Ymd", $img2show['post']).'/';
                    if ($_GET['flip']) {
                    $img_src = imagecreatefromjpeg(GROUP_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                    imageinterlace($img_src, 1);
                    $img_src=ImageFlip($img_src, $_GET['flip']);
                    Imagejpeg($img_src);
                    ImageDestroy($img_src);
                    } else {
                    $filename=GROUP_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image'];
                    $size = @getimagesize($filename);
                    $fp = @fopen($filename, "rb");
                        if ($size && $fp) {
                            fpassthru($fp);
                            exit;
                        }     
                    }
                }
            } else if ($_GET['s']=='28') {
                $img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb, image_dim, thumb_dim, memberID, post from group_photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_SQUARE,MAXIMUM_THUMB_SQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey);
				$ims=explode("x", $img2show['thumb_dim']);
                if ($ims[0]<MAXIMUM_THUMB_SQUARE || $ims[1]<MAXIMUM_THUMB_SQUARE) {
                $ims=array();$ims=explode("x", $img2show['image_dim']);
                $org_img = imagecreatefromjpeg(GROUP_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_SQUARE)/$ims[1]) : MAXIMUM_THUMB_SQUARE; // landscape
                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_SQUARE)/$ims[0]) : MAXIMUM_THUMB_SQUARE;  // portrait
                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_SQUARE/2));
                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_SQUARE/2));
                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);
                } else {
                $thm_img = imagecreatefromjpeg(GROUP_THUMBNAIL_FILE_PATH.$img2show['thumb']);
                $startx=round(($ims[0]/2)-(MAXIMUM_THUMB_SQUARE/2));
                $starty=round(($ims[1]/2)-(MAXIMUM_THUMB_SQUARE/2));
                imagecopy($img_src,$thm_img, 0, 0, $startx, $starty, $ims[0], $ims[1]);
                }
                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_SQUARE-1), (MAXIMUM_THUMB_SQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,GROUP_BOX_PHOTO_FILE_PATH.str_replace("_thm.jpg", "_box.jpg", $img2show['thumb']), 90);
                ImageDestroy($img_src);
                }
            } else if ($_GET['s']=='29') {
                Header("Content-type: image/jpeg");
                if ($_GET['fid']>0) {
					$ims=getimagesize(GROUP_LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
					$width_orig=$ims[0];
					$height_orig=$ims[1];
					if ($ims[mime]=="image/png") $image = imagecreatefrompng(GROUP_LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
					if ($ims[mime]=="image/jpeg") $image = imagecreatefromjpeg(GROUP_LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
					$filename=CACHE_IMG_FILE_PATH.'/page_file_'.$_GET['fid'].'_slide.jpg';
                } else if ($_GET['aid']>0) {
					$ims=getimagesize(GROUP_ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
					$width_orig=$ims[0];
					$height_orig=$ims[1];
					if ($ims[mime]=="image/png") $image = imagecreatefrompng(GROUP_ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
					if ($ims[mime]=="image/jpeg") $image = imagecreatefromjpeg(GROUP_ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
					$filename=CACHE_IMG_FILE_PATH.'/page_blog_'.$_GET['aid'].'_slide.jpg'; 
                } else {
					$img2show=sql_fetch_assoc(sql_query("select image, image_dim, memberID, post from group_photo where ID='".$photoid."'"));
					$folder_date=date("Ymd", $img2show['post']).'/';
					list($width_orig, $height_orig) = explode('x', $img2show['image_dim']);
					list($file_name, $ext) = explode('.', $img2show['image']);
					$image = imagecreatefromjpeg(GROUP_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
					$filename=GROUP_SLIDE_PHOTO_FILE_PATH.$file_name.'_slide.jpg';
                }

                $ratio_orig = $width_orig/$height_orig;
                $width=MAXIMUM_PHOTO_SLIDE_WIDTH;$height=MAXIMUM_PHOTO_SLIDE_HEIGHT;
                if ($width/$height > $ratio_orig) {
                    $width = $height*$ratio_orig;
                } else {
                    $height = $width/$ratio_orig;
                }
                $img_src = imagecreatetruecolor($width, $height);
                
                imagecopyresampled($img_src, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                Imagejpeg($img_src); 
                Imagejpeg($img_src, $filename, 90);
                ImageDestroy($img_src);
            } else if ($_GET['s']=='30') {
                $img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb, image_dim, thumb_dim, memberID, post from group_photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_BIGSQUARE,MAXIMUM_THUMB_BIGSQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey);
				$ims=explode("x", $img2show['image_dim']);
                $org_img = imagecreatefromjpeg(GROUP_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_BIGSQUARE)/$ims[1]) : MAXIMUM_THUMB_BIGSQUARE; // landscape
                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_BIGSQUARE)/$ims[0]) : MAXIMUM_THUMB_BIGSQUARE;  // portrait
                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);

                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_BIGSQUARE-1), (MAXIMUM_THUMB_BIGSQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,GROUP_BIGBOX_PHOTO_FILE_PATH.str_replace("_thm.jpg", "_bigbox.jpg", $img2show['thumb']), 90);
                ImageDestroy($img_src);
                }
	    }elseif ($_GET['s']=='31') {
                $img2show=@sql_fetch_assoc(sql_query("select image, image_dim, memberID, post from group_photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                    Header("Content-type: image/png");
                    $h = $_GET["h"]; // Histogram channel (r=Red, g=Green, b=Blue)
                    $folder_date=date("Ymd", $img2show['post']).'/';
                    $img_src = imagecreatefromjpeg(GROUP_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                    $histR = array();
                    $histG = array();
                    $histB = array();
                    $s=explode("x", $img2show['image_dim']);
                    // Iterate through the image pixel-by-pixel, line-by-line
                    for ( $y=0; $y<$s[1]; $y++ ) {
                        for ( $x=0; $x<$s[0]; $x++ ) {
                            $rgb = imagecolorat($img_src, $x, $y);
                            $hex = sprintf("%06x", $rgb);
                            $histR[substr($hex,0,2)] ++;
                            $histG[substr($hex,2,2)] ++;
                            $histB[substr($hex,4,2)] ++;
                        }
                    }
                    // Convert histograms hex to decimal representation and get peak value
                    for( $x=0; $x<256; $x++ ) {
                        $hex = sprintf("%02x", $x);
                        $histRdec[$x] = $histR[$hex];
                        $histGdec[$x] = $histG[$hex];
                        $histBdec[$x] = $histB[$hex];
    
                        $maxR = max($maxR, $histR[$hex]);
                        $maxG = max($maxG, $histG[$hex]);
                        $maxB = max($maxB, $histB[$hex]);
                    }
                    // Choose the histogram according to the channel
                    if ( strtolower($h) == "r" ) {
                        $max  = $maxR;
                        $hist = $histRdec;
                    }
                    elseif ( strtolower($h) == "g" ) {
                        $max  = $maxG;
                        $hist = $histGdec;
                    }
                    elseif ( strtolower($h) == "b" ) {
                        $max  = $maxB;
                        $hist = $histBdec;
                    }
    
                    // Create the histogram
                    $imHist = imagecreate(256, 120);
                    $white = imagecolorallocate($imHist, 255, 255, 255);
                    $black = imagecolorallocate($imHist, 0, 0, 0);
                    $originX = 0;
    
                    for( $x=0; $x<256; $x++ ) {
                        if ( strtolower($h) == "r" )
                            $color = imagecolorallocate($imHist, $x, 0, 0);
                        elseif ( strtolower($h) == "g" )
                            $color = imagecolorallocate($imHist, 0, $x, 0);
                        elseif ( strtolower($h) == "b" )
                            $color = imagecolorallocate($imHist, 0, 0, $x);
                        imageline($imHist, $originX, 0, $originX, 100, $black);
                        imageline($imHist, $originX+$x, 119, $originX+$x, 100, $color);
                        imageline($imHist, $originX+$x, 100, $originX+$x, 100-100*($hist[$x]/$max), $black);
                    }
                        imagestring($imHist, 1, 2, 0, $max, $black);
                        imagestring($imHist, 1, 2, 100, "0", $white);
    
                    // Return the histogram
                    $finalHist = GROUP_HISTOGRAM_FILE_PATH.str_replace(".jpg", "_".$h.".png", $img2show['image']);
                    imagepng($imHist, $finalHist);
                    Header("Content-type: image/png");
                    imagepng($imHist);
                    imagedestroy($imHist);
                }				
	    } else if ($_GET['s']=='32') {
                //ini_set("display_errors",1);
		$img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb  from product where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
		
                Header("Content-type: image/jpeg");
               // $folder_date=date("Ymd", $img2show['post']).'/';
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_BIGSQUARE,MAXIMUM_THUMB_BIGSQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey); 
				$ims=getimagesize(PRODUCT_FILE_PATH.'/'.$img2show['image']);

                $org_img = imagecreatefromjpeg(PRODUCT_FILE_PATH.'/'.$img2show['image']);
                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_BIGSQUARE)/$ims[1]) : MAXIMUM_THUMB_BIGSQUARE; // landscape
                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_BIGSQUARE)/$ims[0]) : MAXIMUM_THUMB_BIGSQUARE;  // portrait
                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);

                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_BIGSQUARE-1), (MAXIMUM_THUMB_BIGSQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,PRODUCT_FILE_PATH.str_replace(".jpg", "_bigbox.jpg", $img2show['image']), 90);
                ImageDestroy($img_src);
                }
	    }else if ($_GET['s']=='33') {
                $img2show=@sql_fetch_assoc(sql_query("select image, memberID, post from page_photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                    Header("Content-type: image/jpeg");
                    $folder_date=date("Ymd", $img2show['post']).'/';
                    if ($_GET['flip']) {
                    $img_src = imagecreatefromjpeg(PAGE_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                    imageinterlace($img_src, 1);
                    $img_src=ImageFlip($img_src, $_GET['flip']);
                    Imagejpeg($img_src);
                    ImageDestroy($img_src);
                    } else {
                    $filename=PAGE_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image'];
                    $size = @getimagesize($filename);
                    $fp = @fopen($filename, "rb");
                        if ($size && $fp) {
                            fpassthru($fp);
                            exit;
                        }     
                    }
                }
            } else if ($_GET['s']=='34') {
                $img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb, image_dim, thumb_dim, memberID, post from page_photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_SQUARE,MAXIMUM_THUMB_SQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey);
				$ims=explode("x", $img2show['thumb_dim']);
                if ($ims[0]<MAXIMUM_THUMB_SQUARE || $ims[1]<MAXIMUM_THUMB_SQUARE) {
                $ims=array();$ims=explode("x", $img2show['image_dim']);
                $org_img = imagecreatefromjpeg(PAGE_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_SQUARE)/$ims[1]) : MAXIMUM_THUMB_SQUARE; // landscape
                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_SQUARE)/$ims[0]) : MAXIMUM_THUMB_SQUARE;  // portrait
                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_SQUARE/2));
                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_SQUARE/2));
                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);
                } else {
                $thm_img = imagecreatefromjpeg(PAGE_THUMBNAIL_FILE_PATH.$img2show['thumb']);
                $startx=round(($ims[0]/2)-(MAXIMUM_THUMB_SQUARE/2));
                $starty=round(($ims[1]/2)-(MAXIMUM_THUMB_SQUARE/2));
                imagecopy($img_src,$thm_img, 0, 0, $startx, $starty, $ims[0], $ims[1]);
                }
                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_SQUARE-1), (MAXIMUM_THUMB_SQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,PAGE_BOX_PHOTO_FILE_PATH.str_replace("_thm.jpg", "_box.jpg", $img2show['thumb']), 90);
                ImageDestroy($img_src);
                }
            } else if ($_GET['s']=='35') {
                Header("Content-type: image/jpeg");
                if ($_GET['fid']>0) {
					$ims=getimagesize(PAGE_LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
					$width_orig=$ims[0];
					$height_orig=$ims[1];
					if ($ims[mime]=="image/png") $image = imagecreatefrompng(PAGE_LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
					if ($ims[mime]=="image/jpeg") $image = imagecreatefromjpeg(PAGE_LIB_FILE_PATH.$_GET['fid'].'/'.$photoid);
					$filename=CACHE_IMG_FILE_PATH.'/group_file_'.$_GET['fid'].'_slide.jpg';
                } else if ($_GET['aid']>0) {
					$ims=getimagesize(PAGE_ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
					$width_orig=$ims[0];
					$height_orig=$ims[1];
					if ($ims[mime]=="image/png") $image = imagecreatefrompng(PAGE_ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
					if ($ims[mime]=="image/jpeg") $image = imagecreatefromjpeg(PAGE_ARTICLE_FILE_PATH.$_GET['aid'].'/'.$photoid);
					$filename=CACHE_IMG_FILE_PATH.'/group_blog_'.$_GET['aid'].'_slide.jpg'; 
                } else {
					$img2show=sql_fetch_assoc(sql_query("select image, image_dim, memberID, post from page_photo where ID='".$photoid."'"));
					$folder_date=date("Ymd", $img2show['post']).'/';
					list($width_orig, $height_orig) = explode('x', $img2show['image_dim']);
					list($file_name, $ext) = explode('.', $img2show['image']);
					$image = imagecreatefromjpeg(PAGE_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
					$filename=PAGE_SLIDE_PHOTO_FILE_PATH.$file_name.'_slide.jpg';
                }

                $ratio_orig = $width_orig/$height_orig;
                $width=MAXIMUM_PHOTO_SLIDE_WIDTH;$height=MAXIMUM_PHOTO_SLIDE_HEIGHT;
                if ($width/$height > $ratio_orig) {
                    $width = $height*$ratio_orig;
                } else {
                    $height = $width/$ratio_orig;
                }
                $img_src = imagecreatetruecolor($width, $height);
                
                imagecopyresampled($img_src, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                Imagejpeg($img_src); 
                Imagejpeg($img_src, $filename, 90);
                ImageDestroy($img_src);
            } else if ($_GET['s']=='36') {
                $img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb, image_dim, thumb_dim, memberID, post from page_photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_BIGSQUARE,MAXIMUM_THUMB_BIGSQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey);
				$ims=explode("x", $img2show['image_dim']);
                $org_img = imagecreatefromjpeg(PAGE_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                $thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_BIGSQUARE)/$ims[1]) : MAXIMUM_THUMB_BIGSQUARE; // landscape
                $thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_BIGSQUARE)/$ims[0]) : MAXIMUM_THUMB_BIGSQUARE;  // portrait
                $startx=round(($thumb_x/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                $starty=round(($thumb_y/2)-(MAXIMUM_THUMB_BIGSQUARE/2));
                ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);

                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_BIGSQUARE-1), (MAXIMUM_THUMB_BIGSQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,PAGE_BIGBOX_PHOTO_FILE_PATH.str_replace("_thm.jpg", "_bigbox.jpg", $img2show['thumb']), 90);
                ImageDestroy($img_src);
                }
	    }elseif ($_GET['s']=='37') {
                $img2show=@sql_fetch_assoc(sql_query("select image, image_dim, memberID, post from page_photo where ID='".$photoid."' limit 0,1"));
                if ($img2show['image']) {
                    Header("Content-type: image/png");
                    $h = $_GET["h"]; // Histogram channel (r=Red, g=Green, b=Blue)
                    $folder_date=date("Ymd", $img2show['post']).'/';
                    $img_src = imagecreatefromjpeg(PAGE_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
                    $histR = array();
                    $histG = array();
                    $histB = array();
                    $s=explode("x", $img2show['image_dim']);
                    // Iterate through the image pixel-by-pixel, line-by-line
                    for ( $y=0; $y<$s[1]; $y++ ) {
                        for ( $x=0; $x<$s[0]; $x++ ) {
                            $rgb = imagecolorat($img_src, $x, $y);
                            $hex = sprintf("%06x", $rgb);
                            $histR[substr($hex,0,2)] ++;
                            $histG[substr($hex,2,2)] ++;
                            $histB[substr($hex,4,2)] ++;
                        }
                    }
                    // Convert histograms hex to decimal representation and get peak value
                    for( $x=0; $x<256; $x++ ) {
                        $hex = sprintf("%02x", $x);
                        $histRdec[$x] = $histR[$hex];
                        $histGdec[$x] = $histG[$hex];
                        $histBdec[$x] = $histB[$hex];
    
                        $maxR = max($maxR, $histR[$hex]);
                        $maxG = max($maxG, $histG[$hex]);
                        $maxB = max($maxB, $histB[$hex]);
                    }
                    // Choose the histogram according to the channel
                    if ( strtolower($h) == "r" ) {
                        $max  = $maxR;
                        $hist = $histRdec;
                    }
                    elseif ( strtolower($h) == "g" ) {
                        $max  = $maxG;
                        $hist = $histGdec;
                    }
                    elseif ( strtolower($h) == "b" ) {
                        $max  = $maxB;
                        $hist = $histBdec;
                    }
    
                    // Create the histogram
                    $imHist = imagecreate(256, 120);
                    $white = imagecolorallocate($imHist, 255, 255, 255);
                    $black = imagecolorallocate($imHist, 0, 0, 0);
                    $originX = 0;
    
                    for( $x=0; $x<256; $x++ ) {
                        if ( strtolower($h) == "r" )
                            $color = imagecolorallocate($imHist, $x, 0, 0);
                        elseif ( strtolower($h) == "g" )
                            $color = imagecolorallocate($imHist, 0, $x, 0);
                        elseif ( strtolower($h) == "b" )
                            $color = imagecolorallocate($imHist, 0, 0, $x);
                        imageline($imHist, $originX, 0, $originX, 100, $black);
                        imageline($imHist, $originX+$x, 119, $originX+$x, 100, $color);
                        imageline($imHist, $originX+$x, 100, $originX+$x, 100-100*($hist[$x]/$max), $black);
                    }
                        imagestring($imHist, 1, 2, 0, $max, $black);
                        imagestring($imHist, 1, 2, 100, "0", $white);
    
                    // Return the histogram
                    $finalHist = PAGE_HISTOGRAM_FILE_PATH.str_replace(".jpg", "_".$h.".png", $img2show['image']);
                    imagepng($imHist, $finalHist);
                    Header("Content-type: image/png");
                    imagepng($imHist);
                    imagedestroy($imHist);
                }
	    } else if ($_GET['s']=='38') {
                $img2show=sql_fetch_assoc(sql_query("select p.title, CONCAT(SUBSTRING_INDEX(p.image,'.',1), '_thm.jpg') as thumb, p.thumb_dim, p.memberID, p.topdaily as pod, p.topmonthly as pom from group_photo p where p.ID='".$photoid."' limit 0,1"));
                if ($img2show['thumb']) {
		Header("Content-type: image/jpeg");
                unset($img_stamp, $img_logo, $img_mobile);
                    $img_thumb=GROUP_THUMBNAIL_FILE_PATH.$img2show['thumb'];
                    if ($img2show['pom']!='0000-00-00') {
                        $img_stamp=ABS_PATH.'images/p-pom.png';
                    } else if ($img2show['pod']!='0000-00-00') {
                        $img_stamp=ABS_PATH.'images/p-pod.png';
                    } else {
                        $img_stamp='';
                    }
                    $img_logo=ABS_PATH.'images/af_box_icon.jpg';
                    $img_mobile=ABS_PATH.'images/mobile_icon.png';
                    
                        $logo = new imagick($img_logo);
                        $im = new imagick($img_thumb);
                        $im->setImageCompression(Imagick::COMPRESSION_JPEG);
                        $im->setImageCompressionQuality(90); 
                        $im->setImageColorspace($logo->getImageColorspace() );
                        $im->cropThumbnailImage( 95, 112);
			$im->adaptiveSharpenImage(0,1);
                        $identify = $im->identifyImage();
                        $im->compositeImage($logo, $logo->getImageCompose(), ($identify['geometry']['width']-20), ($identify['geometry']['height']-20));
                        if ($img_stamp) {
                            $stamp = new imagick($img_stamp);
                            $im->setImageColorspace($stamp->getImageColorspace() );
                            $im->compositeImage($stamp, $stamp->getImageCompose(), 0, 0); 
                        }
                        if ($img2show['download']=='3') {
                            $hp = new imagick($img_mobile);
                            $im->setImageColorspace($hp->getImageColorspace() );
                            $im->compositeImage($hp, $hp->getImageCompose(), ($identify['geometry']['width']-43), ($identify['geometry']['height']-23));
                        }

                        echo $im;
                        $im->clear();$im->destroy();
                }
	    } else if ($_GET['s']=='39') {
            $img2show=sql_fetch_assoc(sql_query("select p.title, CONCAT(SUBSTRING_INDEX(p.image,'.',1), '_thm.jpg') as thumb, p.thumb_dim, p.memberID, p.topdaily as pod, p.topmonthly as pom from page_photo p where p.ID='".$photoid."' limit 0,1"));
            if ($img2show['thumb']) {
				Header("Content-type: image/jpeg");
                unset($img_stamp, $img_logo, $img_mobile);
                $img_thumb=PAGE_THUMBNAIL_FILE_PATH.$img2show['thumb'];
                    if ($img2show['pom']!='0000-00-00') {
                        $img_stamp=ABS_PATH.'images/p-pom.png';
                    } else if ($img2show['pod']!='0000-00-00') {
                        $img_stamp=ABS_PATH.'images/p-pod.png';
                    } else {
                        $img_stamp='';
                    }
                    $img_logo=ABS_PATH.'images/af_box_icon.jpg';
                    $img_mobile=ABS_PATH.'images/mobile_icon.png';
                    
                        $logo = new imagick($img_logo);
                        $im = new imagick($img_thumb);
                        $im->setImageCompression(Imagick::COMPRESSION_JPEG);
                        $im->setImageCompressionQuality(90); 
                        $im->setImageColorspace($logo->getImageColorspace() );
                        $im->cropThumbnailImage( 95, 112);
						$im->adaptiveSharpenImage(0,1);
                        $identify = $im->identifyImage();
                        $im->compositeImage($logo, $logo->getImageCompose(), ($identify['geometry']['width']-20), ($identify['geometry']['height']-20));
                        if ($img_stamp) {
                            $stamp = new imagick($img_stamp);
                            $im->setImageColorspace($stamp->getImageColorspace() );
                            $im->compositeImage($stamp, $stamp->getImageCompose(), 0, 0); 
                        }
                        if ($img2show['download']=='3') {
                            $hp = new imagick($img_mobile);
                            $im->setImageColorspace($hp->getImageColorspace() );
                            $im->compositeImage($hp, $hp->getImageCompose(), ($identify['geometry']['width']-43), ($identify['geometry']['height']-23));
                        }

                        echo $im;
                        $im->clear();$im->destroy();
                }
	    } else if ($_GET['s']=='40') {
			
            $img2show=sql_fetch_assoc(sql_query("select p.title, CONCAT(SUBSTRING_INDEX(p.image,'.',1), '_slide.jpg') as slide, p.thumb_dim, p.memberID, p.topdaily as pod, p.topmonthly as pom, p.download from photo p where p.ID='".$photoid."' limit 0,1"));
			//echo "select p.title, CONCAT(SUBSTRING_INDEX(p.image,'.',1), '_slide.jpg') as slide, p.thumb_dim, p.memberID, p.topdaily as pod, p.topmonthly as pom, p.download from photo p where p.ID='".$photoid."' limit 0,1";
            if ($img2show['slide']) {
				Header("Content-type: image/jpeg");
                unset($img_stamp, $img_logo, $img_mobile);
                $img_thumb=SLIDE_PHOTO_FILE_PATH.$img2show['slide'];
                if ($img2show['pom']!='0000-00-00') {
                    $img_stamp=ABS_PATH.'images/p-pom.png';
                } else if ($img2show['pod']!='0000-00-00') {
                    $img_stamp=ABS_PATH.'images/p-pod.png';
                } else {
                    $img_stamp='';
                }
                $img_logo=ABS_PATH.'images/af_box_icon.jpg';
                $img_mobile=ABS_PATH.'images/mobile_icon.png';
                    
                $logo = new imagick($img_logo);
                $im = new imagick($img_thumb);
                $im->setImageCompression(Imagick::COMPRESSION_JPEG);
                $im->setImageCompressionQuality(90); 
                $im->setImageColorspace($logo->getImageColorspace() );
                //$im->cropThumbnailImage( 95, 112);
				$im->adaptiveSharpenImage(0,1);
                $identify = $im->identifyImage();
                $im->compositeImage($logo, $logo->getImageCompose(), ($identify['geometry']['width']-20), ($identify['geometry']['height']-20));
                if ($img_stamp) {
                    $stamp = new imagick($img_stamp);
                    $im->setImageColorspace($stamp->getImageColorspace() );
                    $im->compositeImage($stamp, $stamp->getImageCompose(), 0, 0); 
                }
                if ($img2show['download']=='3') {
                    $hp = new imagick($img_mobile);
                    $im->setImageColorspace($hp->getImageColorspace() );
                    $im->compositeImage($hp, $hp->getImageCompose(), ($identify['geometry']['width']-43), ($identify['geometry']['height']-23));
                }

                echo $im;
                $im->clear();$im->destroy();
            }			
			
			
		} else if ($_GET['s']=='41') {
            $img2show=sql_fetch_assoc(sql_query("select p.title, CONCAT(SUBSTRING_INDEX(p.image,'.',1), '_thm.jpg') as thumb, p.thumb_dim, p.memberID, p.topdaily as pod, p.topmonthly as pom from group_photo p where p.ID='".$photoid."' limit 0,1"));
            if ($img2show['thumb']) {
				Header("Content-type: image/jpeg");
                unset($img_stamp, $img_logo, $img_mobile);
                    $img_thumb=GROUP_THUMBNAIL_FILE_PATH.$img2show['thumb'];
                    if ($img2show['pom']!='0000-00-00') {
                        $img_stamp=ABS_PATH.'images/p-pom.png';
                    } else if ($img2show['pod']!='0000-00-00') {
                        $img_stamp=ABS_PATH.'images/p-pod.png';
                    } else {
                        $img_stamp='';
                    }
                    $img_logo=ABS_PATH.'images/af_box_icon.jpg';
                    $img_mobile=ABS_PATH.'images/mobile_icon.png';
                    
                        $logo = new imagick($img_logo);
                        $im = new imagick($img_thumb);
                        $im->setImageCompression(Imagick::COMPRESSION_JPEG);
                        $im->setImageCompressionQuality(90); 
                        $im->setImageColorspace($logo->getImageColorspace() );
                        $im->cropThumbnailImage( 95, 112);
			$im->adaptiveSharpenImage(0,1);
                        $identify = $im->identifyImage();
                        $im->compositeImage($logo, $logo->getImageCompose(), ($identify['geometry']['width']-20), ($identify['geometry']['height']-20));
                        if ($img_stamp) {
                            $stamp = new imagick($img_stamp);
                            $im->setImageColorspace($stamp->getImageColorspace() );
                            $im->compositeImage($stamp, $stamp->getImageCompose(), 0, 0); 
                        }
                        if ($img2show['download']=='3') {
                            $hp = new imagick($img_mobile);
                            $im->setImageColorspace($hp->getImageColorspace() );
                            $im->compositeImage($hp, $hp->getImageCompose(), ($identify['geometry']['width']-43), ($identify['geometry']['height']-23));
                        }

                        echo $im;
                        $im->clear();$im->destroy();
                }
	    } else if ($_GET['s']=='42') {
            $img2show=sql_fetch_assoc(sql_query("select p.title, CONCAT(SUBSTRING_INDEX(p.image,'.',1), '_slide.jpg') as slide, p.memberID, p.topdaily as pod, p.topmonthly as pom from page_photo p where p.ID='".$photoid."' limit 0,1"));
            if ($img2show['thumb']) {
				Header("Content-type: image/jpeg");
                unset($img_stamp, $img_logo, $img_mobile);
                $img_thumb=PAGE_SLIDE_PHOTO_FILE_PATH.$img2show['slide'];
                    if ($img2show['pom']!='0000-00-00') {
                        $img_stamp=ABS_PATH.'images/p-pom.png';
                    } else if ($img2show['pod']!='0000-00-00') {
                        $img_stamp=ABS_PATH.'images/p-pod.png';
                    } else {
                        $img_stamp='';
                    }
                    $img_logo=ABS_PATH.'images/af_box_icon.jpg';
                    $img_mobile=ABS_PATH.'images/mobile_icon.png';
                    
                        $logo = new imagick($img_logo);
                        $im = new imagick($img_thumb);
                        $im->setImageCompression(Imagick::COMPRESSION_JPEG);
                        $im->setImageCompressionQuality(90); 
                        $im->setImageColorspace($logo->getImageColorspace() );
                        $im->cropThumbnailImage( 95, 112);
						$im->adaptiveSharpenImage(0,1);
                        $identify = $im->identifyImage();
                        $im->compositeImage($logo, $logo->getImageCompose(), ($identify['geometry']['width']-20), ($identify['geometry']['height']-20));
                        if ($img_stamp) {
                            $stamp = new imagick($img_stamp);
                            $im->setImageColorspace($stamp->getImageColorspace() );
                            $im->compositeImage($stamp, $stamp->getImageCompose(), 0, 0); 
                        }
                        if ($img2show['download']=='3') {
                            $hp = new imagick($img_mobile);
                            $im->setImageColorspace($hp->getImageColorspace() );
                            $im->compositeImage($hp, $hp->getImageCompose(), ($identify['geometry']['width']-43), ($identify['geometry']['height']-23));
                        }

                        echo $im;
                        $im->clear();$im->destroy();
            }
			
        } else if ($_GET['s']=='43') {
                Header("Content-type: image/jpeg");

				$img2show=sql_fetch_assoc(sql_query("select image, image_dim, memberID, post from page_photo where ID='".$photoid."'"));
				$folder_date=date("Ymd", $img2show['post']).'/';
				list($width_orig, $height_orig) = explode('x', $img2show['image_dim']);
				list($file_name, $ext) = explode('.', $img2show['image']);
				$image = imagecreatefromjpeg(PAGE_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
				$filename=PAGE_THUMBNAIL_FILE_PATH.$file_name.'_thm.jpg';


                $ratio_orig = $width_orig/$height_orig;
                $width=MAXIMUM_THUMB_WIDTH;$height=MAXIMUM_THUMB_HEIGHT;
                if ($width/$height > $ratio_orig) {
                    $width = $height*$ratio_orig;
                } else {
                    $height = $width/$ratio_orig;
                }
                $img_src = imagecreatetruecolor($width, $height);
                
                imagecopyresampled($img_src, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                Imagejpeg($img_src); 
                Imagejpeg($img_src, $filename, 90);
                ImageDestroy($img_src);			



        } else if ($_GET['s']=='50') {
            $img2show=@sql_fetch_assoc(sql_query("select image, CONCAT(SUBSTRING_INDEX(image,'.',1), '_thm.jpg') as thumb, image_dim, thumb_dim, memberID, post from page_photo where ID='".$photoid."' limit 0,1"));
            if ($img2show['image']) {
                Header("Content-type: image/jpeg");
                $folder_date=date("Ymd", $img2show['post']).'/';
                $img_src = imagecreatetruecolor(MAXIMUM_THUMB_SQUARE,MAXIMUM_THUMB_SQUARE);
                $grey = imagecolorallocate($img_src, 153,153,153);
                $white = imagecolorallocate($img_src, 255,255,255);
                Imagefill($img_src, 0, 0, $grey);
				$ims=explode("x", $img2show['thumb_dim']);
				if ($ims[0]<MAXIMUM_THUMB_SQUARE || $ims[1]<MAXIMUM_THUMB_SQUARE) {
					$ims=array();$ims=explode("x", $img2show['image_dim']);
					$org_img = imagecreatefromjpeg(PAGE_PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image']);
					$thumb_x = ($ims[0] >= $ims[1]) ? round(($ims[0] * MAXIMUM_THUMB_SQUARE)/$ims[1]) : MAXIMUM_THUMB_SQUARE; // landscape
					$thumb_y = ($ims[0] < $ims[1]) ? round(($ims[1] * MAXIMUM_THUMB_SQUARE)/$ims[0]) : MAXIMUM_THUMB_SQUARE;  // portrait
					$startx=round(($thumb_x/2)-(MAXIMUM_THUMB_SQUARE/2));
					$starty=round(($thumb_y/2)-(MAXIMUM_THUMB_SQUARE/2));
					ImageCopyResampled($img_src, $org_img, 0, 0, $startx, $starty, $thumb_x, $thumb_y, $ims[0], $ims[1]);
				} else {
					$thm_img = imagecreatefromjpeg(PAGE_THUMBNAIL_FILE_PATH.$img2show['thumb']);
					$startx=round(($ims[0]/2)-(MAXIMUM_THUMB_SQUARE/2));
					$starty=round(($ims[1]/2)-(MAXIMUM_THUMB_SQUARE/2));
					imagecopy($img_src,$thm_img, 0, 0, $startx, $starty, $ims[0], $ims[1]);
				}
                imagerectangle($img_src, 0 , 0, (MAXIMUM_THUMB_SQUARE-1), (MAXIMUM_THUMB_SQUARE-1), $grey);
                imageinterlace($img_src, 1);
                Imagejpeg($img_src);
                Imagejpeg($img_src,PAGE_BOX_PHOTO_FILE_PATH.str_replace("_thm.jpg", "_box.jpg", $img2show['thumb']), 90);
                ImageDestroy($img_src);
            }
			
			
        } else if ($_GET['s']=='51') {

            Header("Content-type: image/jpeg");
            $img2show=sql_fetch_assoc(sql_query("SELECT image, image_dim, memberID, post FROM photo WHERE ID='".$photoid."' LIMIT 1"));
            $folder_date=date("Ymd", $img2show['post']).'/';
            $output=CACHE_IMG_FILE_PATH."preview_".$photoid."_930.jpg";
            if(file_exists($output)){
                $image = new Imagick($output);
                echo $image;
            }else{

                $dimension=explode("x", $img2show['image_dim']);
                if ($dimension[0]>930) {
                    $dimension[2]=930;
                    $dimension[3]=(930*$dimension[1])/$dimension[0];
                } else {
                    $dimension[2]=$dimension[0];
                    $dimension[3]=$dimension[1];
                }
                            
                $input=PHOTO_FILE_PATH.$folder_date.$img2show['memberID'].'/'.$img2show['image'];
                $image = new Imagick($input);
                $image->scaleImage($dimension[2], $dimension[3], true);
                $image->writeImage($output);
                echo $image;
                $image->destroy();
            }

        }
    } else {
    die();
    }
} else {
die();
}
?>