Need help implementing script in to image host

Discussion in 'JavaScript' started by convex7, Mar 17, 2008.

  1. #1
    I have this script:

    	//Scale image if needed
    		$h = $file_info['1'];
    		$w = $file_info['0'];
    		
    		$oh = $file_info['1'];
    		$ow = $file_info['0'];
    		
    		//width - new_width
    		//height - new_height
    
    		if ($w > 830){
    			$new_width = 830;
    			$new_height = ($h * 830) / $w;
    			
    			$w = $new_width;
    			$h = $new_height;
    		};
    		
    		 		
    		if ($h > 650){
    			$new_height = 650;
    			$new_width = ($w * 650) / $h;
    			
    			$w = $new_width;
    			$h = $new_height;
    		};
    		
    print <<<HTML
    <script type="text/javascript">
      function scale_img(){		
    	  pic = document.getElementById('thepic');		
    	  pic.height = $h;
    	  pic.width = $w;
    	  
    	  link = document.getElementById('thelink');	
    	  link.href = "javascript: max_img()";	  
    	};
    	
      function max_img(){		
    	  pic = document.getElementById('thepic');		
    	  pic.height = $oh;
    	  pic.width = $ow;
    	  
    	  link = document.getElementById('thelink');	
    	  link.href = "javascript: scale_img()";
    	};
    </script>	
    HTML;
    Code (markup):
    	//resized
    	$href= "#";
    	if (($w <> $ow) or ($h <> $oh))
    	  $href = "javascript: max_img();";  
    	
    	$img = "<a id='thelink' href=\"$href\"><img id='thepic' src=\"{$path}{$file}\" width='$w' height='$h' alt=\"{$file}\" title=\"{$file}\" /></a>";
    Code (markup):
    I am using the mihalism.com script.

    I am willing to pay or exchange design work to anyone who can do it.

    Thanks
     
    convex7, Mar 17, 2008 IP
  2. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This code just needs to be added to your viewer.php file. It should be pretty straightforward.

    Post your viewer.php and I can do the edit if you like.
     
    stoli, Mar 19, 2008 IP
  3. convex7

    convex7 Peon

    Messages:
    157
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for the reply stoli,

    Here is the code:
    <?php
    	// ======================================== \
    	// Package: Mihalism Multi Host
    	// Version: 4.0.0
    	// Copyright (c) 2007, 2008 Mihalism, Inc.
    	// License: http://www.gnu.org/licenses/gpl.txt GNU Public License
    	// ======================================== /
    
    	require_once "./source/includes/data.php";
    
    	$mmhclass->templ->page_title = "{$mmhclass->info->config['site_name']} &raquo; File Viewer";
    
    	if ($mmhclass->funcs->is_null($mmhclass->input->get_vars['file']) == true) {
    		$mmhclass->templ->error("Please select a file to continue.", true);
    	} elseif (is_file($mmhclass->info->root_path.$mmhclass->info->config['upload_path'].basename($mmhclass->input->get_vars['file'])) == false) {
    		$mmhclass->templ->error("The requested file <b>{$mmhclass->input->get_vars['file']}</b> does not exist.", true);
    	} elseif (in_array($mmhclass->funcs->file_extension($mmhclass->input->get_vars['file']), array("jpg", "jpeg", "ico", "bmp", "gif", "png")) == false) {
    		$mmhclass->templ->error("The requested file is not a valid image format. Click <a href=\"download.php?file={$mmhclass->input->get_vars['file']}\">here</a> to download the requested file.", true);
    	} else {
    		$filename    = basename($mmhclass->input->get_vars['file']);
    		$rating_info = $mmhclass->db->fetch_array($mmhclass->db->query("SELECT * FROM `mmh_file_ratings` WHERE `filename` = '{$filename}';"));
    		$file_info   = getimagesize($mmhclass->info->root_path.$mmhclass->info->config['upload_path'].$filename);
    	
    		if ($mmhclass->input->get_vars['act'] == "rate_it" && $mmhclass->funcs->is_null($mmhclass->input->post_vars['rating_id']) == false) {
    			$mmhclass->templ->templ_globals['new_file_rating'] = true;
    			if (in_array($mmhclass->input->server_vars['remote_addr'], preg_split("/\|/", $rating_info['voted_by'])) == true) {
    				$new_rating_html = $mmhclass->templ->error("Your IP address has already placed a rating on this image.", false);
    			} else {
    				if ($mmhclass->funcs->is_null($rating_info['rating_id']) == true) {
    					$mmhclass->db->query("INSERT INTO `mmh_file_ratings` (`filename`, `total_rating`, `total_votes`, `voted_by`) VALUES ('{$filename}', '0', '0', '');");
    				}
    				$mmhclass->db->query("UPDATE `mmh_file_ratings` SET `total_rating` = `total_rating` + {$mmhclass->input->post_vars['rating_id']}, `total_votes` = `total_votes` + 1, `voted_by` = '{$rating_info['voted_by']}|{$mmhclass->input->server_vars['remote_addr']}' WHERE `filename` = '{$filename}';");
    				$new_rating_html = $mmhclass->templ->message("Your rating has been successfully submitted. Below is the updated file rating. <br /><br /> <img src=\"index.php?module=rating&file={$filename}\" alt=\"File Rating\" />", false);
    			}
    		}
    	
    		$mmhclass->templ->templ_globals['file_info'] = $file_info;
    		$mmhclass->templ->templ_vars[] = array(
    			"UPLOAD_PATH"        => $mmhclass->info->config['upload_path'],
    			"FILENAME"           => $filename,
    			"IMAGE_WIDTH"        => $file_info['0'],
    			"IMAGE_HEIGHT"       => $file_info['1'],
    			"FILE_EXTENSION"     => $mmhclass->funcs->file_extension($filename),
    			"MIME_TYPE"          => $file_info['mime'],
    			"DATE_UPLOADED"      => date($mmhclass->info->config['date_format'], filemtime($mmhclass->info->root_path.$mmhclass->info->config['upload_path'].$filename)),
    			"TOTAL_FILESIZE"     => $mmhclass->funcs->format_filesize(filesize($mmhclass->info->root_path.$mmhclass->info->config['upload_path'].$filename)),
    			"TOTAL_RATINGS"      => $mmhclass->funcs->format_number($rating_info['total_votes']),
    			"FILE_LINKS"         => $mmhclass->templ->file_results($filename),
    			"NEW_RATING_HTML"    => $new_rating_html,
    		);
    	
    		$mmhclass->templ->output("viewer");
    	}
    
    ?>
    
    Code (markup):
     
    convex7, Mar 19, 2008 IP
  4. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Because you are using version 4.0 you will need to make edits to viewer.php and also source/public_html/viewer.tpl.

    I have updated the files from the Mihalism web site.

    Here are updated versions of both those files:
    viewer.php:
    <?php
    	// ======================================== \
    
    	// Package: Mihalism Multi Host
    	// Version: 4.0.0
    	// Copyright (c) 2007, 2008 Mihalism, Inc.
    	// License: http://www.gnu.org/licenses/gpl.txt GNU Public License
    	// ======================================== /
    
    	require_once "./source/includes/data.php";
    
    	$mmhclass->templ->page_title = "{$mmhclass->info->config['site_name']} &raquo; File Viewer";
    
    	if ($mmhclass->funcs->is_null($mmhclass->input->get_vars['file']) == true) {
    		$mmhclass->templ->error("Please select a file to continue.", true);
    	} elseif (is_file($mmhclass->info->root_path.$mmhclass->info->config['upload_path'].basename($mmhclass->input->get_vars['file'])) == false) {
    		$mmhclass->templ->error("The requested file <b>{$mmhclass->input->get_vars['file']}</b> does not exist.", true);
    	} elseif (in_array($mmhclass->funcs->file_extension($mmhclass->input->get_vars['file']), array("jpg", "jpeg", "ico", "bmp", "gif", "png")) == false) {
    		$mmhclass->templ->error("The requested file is not a valid image format. Click <a href=\"download.php?file={$mmhclass->input->get_vars['file']}\">here</a> to download the requested file.", true);
    	} else {
    		$filename    = basename($mmhclass->input->get_vars['file']);
    		$rating_info = $mmhclass->db->fetch_array($mmhclass->db->query("SELECT * FROM `mmh_file_ratings` WHERE `filename` = '{$filename}';"));
    		$file_info   = getimagesize($mmhclass->info->root_path.$mmhclass->info->config['upload_path'].$filename);
    	
    		if ($mmhclass->input->get_vars['act'] == "rate_it" && $mmhclass->funcs->is_null($mmhclass->input->post_vars['rating_id']) == false) {
    			$mmhclass->templ->templ_globals['new_file_rating'] = true;
    			if (in_array($mmhclass->input->server_vars['remote_addr'], preg_split("/\|/", $rating_info['voted_by'])) == true) {
    				$new_rating_html = $mmhclass->templ->error("Your IP address has already placed a rating on this image.", false);
    			} else {
    				if ($mmhclass->funcs->is_null($rating_info['rating_id']) == true) {
    					$mmhclass->db->query("INSERT INTO `mmh_file_ratings` (`filename`, `total_rating`, `total_votes`, `voted_by`) VALUES ('{$filename}', '0', '0', '');");
    				}
    				$mmhclass->db->query("UPDATE `mmh_file_ratings` SET `total_rating` = `total_rating` + {$mmhclass->input->post_vars['rating_id']}, `total_votes` = `total_votes` + 1, `voted_by` = '{$rating_info['voted_by']}|{$mmhclass->input->server_vars['remote_addr']}' WHERE `filename` = '{$filename}';");
    				$new_rating_html = $mmhclass->templ->message("Your rating has been successfully submitted. Below is the updated file rating. <br /><br /> <img src=\"index.php?module=rating&file={$filename}\" alt=\"File Rating\" />", false);
    			}
    		}
    
      	//Scale image if needed
      	$image_scaled_height = $file_info['1'];
      	$image_scaled_width = $file_info['0'];
    		
      	$image_original_height = $file_info['1'];
      	$image_original_width = $file_info['0'];
    		
    		if ($image_scaled_width > 830) {
    			$image_scaled_height = ($image_scaled_height * 830) / $image_scaled_width;
    			$image_scaled_width = 830;
    		};
    		 		
    		if ($image_scaled_height > 650) {
    			$image_scaled_width = ($image_scaled_width * 650) / $image_scaled_height;
    			$image_scaled_height = 650;
    		};
    
        $image_href= "#";
    	  if (($image_scaled_width <> $image_original_width) or ($image_scaled_height <> $image_original_height)) $image_href = "javascript: max_img();";
    
    	
    		$mmhclass->templ->templ_globals['file_info'] = $file_info;
    		$mmhclass->templ->templ_vars[] = array(
    			"UPLOAD_PATH"        => $mmhclass->info->config['upload_path'],
    			"FILENAME"           => $filename,
    			"IMAGE_WIDTH"        => $file_info['0'],
    			"IMAGE_HEIGHT"       => $file_info['1'],
    			"IMAGE_SCALED_WIDTH" => $image_scaled_width,
    			"IMAGE_SCALED_HEIGHT"=> $image_scaled_height,
    			"IMAGE_HREF"         => $image_href,
    			"FILE_EXTENSION"     => $mmhclass->funcs->file_extension($filename),
    			"MIME_TYPE"          => $file_info['mime'],
    			"DATE_UPLOADED"      => date($mmhclass->info->config['date_format'], filemtime($mmhclass->info->root_path.$mmhclass->info->config['upload_path'].$filename)),
    			"TOTAL_FILESIZE"     => $mmhclass->funcs->format_filesize(filesize($mmhclass->info->root_path.$mmhclass->info->config['upload_path'].$filename)),
    			"TOTAL_RATINGS"      => $mmhclass->funcs->format_number($rating_info['total_votes']),
    			"FILE_LINKS"         => $mmhclass->templ->file_results($filename),
    			"NEW_RATING_HTML"    => $new_rating_html,
    		);
    	
    		$mmhclass->templ->output("viewer");
    	}
    
    ?>
    PHP:
    and source/public_html/viewer.tpl
    <!-- BEGIN: MAIN VIEWER PAGE -->
    
    <if="$mmhclass->templ->templ_globals['new_file_rating'] == true">
    	<# NEW_RATING_HTML #>
    	<hr />
    </endif>
    <div style="text-align: center;">
    	<if="$mmhclass->funcs->is_null($mmhclass->input->get_vars['is_random']) == false">
    		<a href="index.php?do_random=1" class="button1">Refresh Random Image</a>
    		<br /><br />
    	</endif>
    
    <script type="text/javascript">
      function scale_img(){		
    	  pic = document.getElementById('thepic');		
    	  pic.height = <# IMAGE_SCALED_HEIGHT #>;
    	  pic.width = <# IMAGE_SCALED_WIDTH #>;
    	  
    	  link = document.getElementById('thelink');	
    	  link.href = "javascript: max_img()";	  
    	};
    	
      function max_img(){		
    	  pic = document.getElementById('thepic');		
    	  pic.height = <# IMAGE_HEIGHT #>;
    	  pic.width = <# IMAGE_WIDTH #>;
    	  
    	  link = document.getElementById('thelink');	
    	  link.href = "javascript: scale_img()";
    	};
    </script>
    	
    	<a id="thelink" href="<# IMAGE_HREF #>"><img id="thepic" src="<# UPLOAD_PATH #><# FILENAME #>" alt="<# FILENAME #>" width="<# IMAGE_SCALED_WIDTH #>" height="<# IMAGE_SCALED_HEIGHT #>" style="border: 1px dashed #000000; padding: 2px;" /></a>
    
    	<if="$mmhclass->templ->templ_globals['file_info']['0'] > 940">
    		<br /><br />
    		<b>Resize</b>: The above image has been resized to better fit your screen. To view its true size, click the above image.
    	</endif>
    </div>
    <br /><hr />
    <a href="download.php?file=<# FILENAME #>" class="button1">Download File</a> 
    <a href="contact.php?act=file_report&amp;file=<# FILENAME #>" class="button1">Report File</a> 
    <a onclick="javascript:toggle('file_rating_block');" class="button1">Rate File</a>
    <a href="links.php?file=<# FILENAME #>" class="button1">File Links</a>
    <br /><br />
    <div id="file_rating_block" style="display: none;">
    	<div class="table_border">
    		<table cellpadding="4" cellspacing="1" border="0" width="100%">
    			<tr>
    				<th colspan="2">File Rating</th>
    			</tr>
    			<tr class="tdrow1">
    				<td style="width: 44%;">&nbsp;</td>
    				<td align="left" style="padding: 5px;">
    					Rate This File: 
    					<br /><br />
    					<form action="viewer.php?act=rate_it&amp;file=<# FILENAME #>" method="post">
    						<p>
    							<input type="radio" checked="checked" name="rating_id" value="5" /> <img src="css/images/ratings/22222.gif" alt="" /> Excellent!<br />
    							<input type="radio" name="rating_id" value="4" /> <img src="css/images/ratings/22220.gif" alt="" /> Very Good<br />
    							<input type="radio" name="rating_id" value="3" /> <img src="css/images/ratings/22200.gif" alt="" /> Good<br />
    							<input type="radio" name="rating_id" value="2" /> <img src="css/images/ratings/22000.gif" alt="" /> Fair<br />
    							<input type="radio" name="rating_id" value="1" /> <img src="css/images/ratings/20000.gif" alt="" /> Poor
    							<br /><br />
    							<input type="submit" class="button1" value="Rate It!" />
    						</p>
    					</form>
    				</td>
    			</tr>
    			<tr>
    				<td colspan="2" class="table_footer">&nbsp;</td>
    			</tr>
    		</table>
    	</div>
    	<br /><hr />
    </div>
    <div class="table_border">
    	<table cellpadding="4" cellspacing="1" border="0" width="100%">
    		<tr>
    			<th colspan="2">File Info</th>
    		</tr>
    		<tr>
    			<td style="width: 44%" class="tdrow1"><b>Filename:</b></td>
    			<td class="tdrow2"><a href="<# UPLOAD_PATH #><# FILENAME #>"><# FILENAME #></a></td>
    		</tr>
    		<tr>
    			<td style="width: 44%" class="tdrow1"><b>Dimensions:</b></td>
    			<td class="tdrow2"><# IMAGE_WIDTH #> x <# IMAGE_HEIGHT #> Pixels (Width x Height)</td>
    		</tr>
    		<tr>
    			<td style="width: 44%" class="tdrow1"><b>Extension:</b></td>
    			<td class="tdrow2"><# FILE_EXTENSION #></td>
    		</tr>
    		<tr>
    			<td style="width: 44%" class="tdrow1"><b>Mime Type:</b></td>
    			<td class="tdrow2"><# MIME_TYPE #></td>
    		</tr>
    		<tr>
    			<td style="width: 44%" class="tdrow1"><b>Date Uploaded:</b></td>
    			<td class="tdrow2"><# DATE_UPLOADED #></td>
    		</tr>
    		<tr>
    			<td style="width: 44%" class="tdrow1"><b>Filesize:</b></td>
    			<td class="tdrow2"><# TOTAL_FILESIZE #></td>
    		</tr>
    		<tr>
    			<td style="width: 44%" class="tdrow1"><b>Rating:</b></td>
    			<td class="tdrow2"><img style="vertical-align:middle;" src="index.php?module=rating&amp;file=<# FILENAME #>" alt="File Rating" /> ( <# TOTAL_RATINGS #> Vote(s) )</td>
    		</tr>
    		<tr>
    			<td colspan="2" class="tdrow2"><# FILE_LINKS #></td>
    		</tr>
    		<tr>
    			<td colspan="2" class="table_footer">&nbsp;</td>
    		</tr>
    	</table>
    </div>
    
    <!-- END: MAIN VIEWER PAGE -->
    Code (markup):
    As it should now be possible to view the full sized images within the page, if you want the image URL to redirect to the viewer page (to make sure users always see advertising for example) then you can add the following to the '.htaccess' file in the 'images' directory:
    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
    RewriteCond %{REQUEST_FILENAME} !.*_thumb.*
    RewriteRule ([^/]*\.jpg)$ /viewer.php?file=$1 [R=301,L]
    
    Code (markup):
    Change 'mysite' as appropriate to the domain you are hosting it on.
     
    stoli, Mar 19, 2008 IP
  5. convex7

    convex7 Peon

    Messages:
    157
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    WOW! From what I have seen from testing, it seems to work. Thank you!

    I have sent you a PM in regards to some work I will pay you for.
     
    convex7, Mar 19, 2008 IP