Jump to content

Recommended Posts

I am using the following script to detect the width of the users display to size the pictures properly

 

<?php
if(!isset($_GET['screen_check']))
{
/* This code will be executed if screen resolution has not been detected.*/
echo "<script language='JavaScript'>
<!-- 
document.location=\"?ID=$PropertyID&screen_check=done&Width=\"+screen.width+\"&Height=\"+screen.height;
//-->
</script>";
}
else 
{    
/* This code will be executed after screen resolution is detected.*/
    if(isset($_GET['Width'])){// && isset($_GET['Height'])) 
	// Resolution  detected
$ScreenWidth=$_GET['Width'];

     }
     else {
               // Resolution not detected
               	$ScreenWidth=235;
     }
}
echo "Screen width = ".$ScreenWidth;

?> 

 

My telephone is 320 x480px but sometimes it shows the width of 800, almost like it is adding these two values, other times it shows the correct values.

 

Can anyone tell me why is it so and what can be done to prevent this issue?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/264570-display-width-in-php/
Share on other sites

Does an exit command help?

 

if(!isset($_GET['screen_check']))
{
/* This code will be executed if screen resolution has not been detected.*/
echo "<script language='JavaScript'>
<!-- 
document.location=\"?ID=$PropertyID&screen_check=done&Width=\"+screen.width+\"&Height=\"+screen.height;
//-->
</script>";
       exit;
}

Try this:

 

<?php
$ScreenWidth = "undefined";
if(!isset($_GET['screen_check'])){
echo <<<JS
	<script>
	document.location="?ID=$PropertyID&screen_check=done&Width="+screen.width+"&Height="+screen.height;
	</script>
JS;
exit;
}
if(isset($_GET['Width'])){
$ScreenWidth = $_GET['Width'];
} else {
$ScreenWidth = 235;
}
echo "Screen width = $ScreenWidth";
?>

<?php

include "db/db_textback.inc.php";

/**
* Include twilio-php, the offical Twilio PHP Helper Library, which can be found at
* http://www.twilio.com/docs/libraries
*/ 
require "Services/Twilio.php";





function sendSMS($ReplytoNumber, $Message, $NotifyAgent, $SendFinancial)
{

// Set AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "*******";
$AuthToken = "*******";
//Instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);
   if($msgLen>160){
   
   }
	$sms = $client->account->sms_messages->create("*******", $ReplytoNumber, $Message);
	if($SendFinancial==1)
	{
		$sms = $client->account->sms_messages->create("********", $ReplytoNumber, "Financial Info");
	}
}

function timeDiff($firstTime,$lastTime)
{
// convert to unix timestamp
$firstTime=strtotime($firstTime);
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

function NewKeywordReceived($result1,$fromNumber1)
{
   $sql="SELECT * FROM Properties WHERE Keyword = '$result1'";
   $KeywordQuery = mysql_query($sql); 
   $KeywordNum=mysql_num_rows($KeywordQuery);
   $KeywordInfo = mysql_fetch_array($KeywordQuery);
   $Description = $KeywordInfo['Description'];
   if($KeywordNum>0){
      mysql_query("INSERT INTO Txtlog (`ID`, `TelNumber`, `Keyword`, `Extended`, `TimeStamp`) VALUES (NULL, '$fromNumber1', '$result1', '0', CURRENT_TIMESTAMP)");
       $Msg = "Please reply with house number.";
       sendSMS($fromNumber1,$Msg,0,0);
   }
   else{
      $Msg = "Keyword ".$result1." not found. Please try again or reply with CLEAR to remove previous keywords from system and start over. Std msg rates may apply.";
      sendSMS($fromNumber1,$Msg,0,0);
   }
}


/* Read the contents of the 'Body' field of the Request. */

$body = $_REQUEST['Body'];
$fromNumber = $_REQUEST['From'];

/* Remove formatting from $body until it is just lowercase characters without punctuation or spaces. */
//$result = preg_replace("/[^A-Za-z0-9]/u", " ", $body);
$result = $body;
$result = trim($result);
$result = strtolower($result);
//$result = '111';
//$fromNumber = '+********';

/* ##Router## */
if($result == 'clear'){
   mysql_query("DELETE FROM Txtlog WHERE TelNumber = '$fromNumber'");
   mysql_query("DELETE FROM TxtlogExtended WHERE Telephone = '$fromNumber'");
   sendSMS($fromNumber,"Your keywords have been cleared, please send valid keyword to get more info.\nStd msg rates may apply.",0,0);
}
else{
   $TelNumberQuery = mysql_query("SELECT * FROM Txtlog WHERE TelNumber = '$fromNumber'");
   $TelNumberNum = mysql_num_rows($TelNumberQuery);
   $TelNumberInfo = mysql_fetch_array($TelNumberQuery);
   $extended=$TelNumberInfo['Extended'];
   if($TelNumberNum > 0){
      $CurrTime = time();
      $PrevTime = $TelNumberInfo['TimeStamp'];
      $TimeDiff = timeDiff($PrevTime,$CurrTime);
      if($TimeDiff < 600){
         $CurrKeyword = $TelNumberInfo['Keyword'];
         $PropertyInfoQuery = mysql_query("SELECT * FROM Properties WHERE Keyword = '$CurrKeyword' AND Subkeyword = '$result'");
         $PropertyInfoNum = mysql_num_rows($PropertyInfoQuery);
         if($PropertyInfoNum == 1){
            $PropertyInfo2 = mysql_fetch_array($PropertyInfoQuery);
    $PropertyID=$PropertyInfo2['ID'];
            $PropertyDescription = $PropertyInfo2['Description'];
            $AgentNotify=0;
            $Financial=$PropertyInfo2['FinancialNotify'];
            $MLS = $PropertyInfo2['MLS'];
            $Number = $PropertyInfo2['Number'];
            $Street = $PropertyInfo2['Street'];
            $City = $PropertyInfo2['City'];
            $Province = $PropertyInfo2['Province'];
            $Agent = $PropertyInfo2['Agent'];
            $AgentID = $PropertyInfo2['AgentID'];    
            $Price = $PropertyInfo2['Price'];        
            $Date = date("d.m.y");           
            if($FinancialNotify=NULL) $FinancialNotify=0;
            $AgentInfoQuery = mysql_query("SELECT * FROM Agents WHERE ID = '$AgentID'");
    $AgentInfoNum = mysql_num_rows($AgentInfoQuery);
    else{
    	$AgentInfo = mysql_fetch_array($AgentInfoQuery);
    	$AgentName = $AgentInfo['DisplayName'];
    	$AgentTelephone = $AgentInfo['Telephone'];
    	$AgentBrokerage = $AgentInfo['Brokerage'];
    	$AgentInfoMessage = $AgentName." ".$AgentTelephone;
    	if($AgentBrokerage!=NULL){
    		$AgentInfoMessage = $AgentInfoMessage."\n".$AgentBrokerage;
    	}
    }
    	$MessageText=$Number." ".$Street."\n".$PropertyDescription." a411.co/index.php?ID=".$PropertyID;
    if($MLS!=NULL){
    	$MessageText="MLS# ".$MLS."\n".$MessageText;
    }
    if($Price != NULL){
    	$MessageText=$MessageText." Price \$".$Price;
    }
    if($AgentInfoMessage!=NULL){
    	$MessageText = $MessageText."\n".$AgentInfoMessage;
    }
            sendSMS($fromNumber,$MessageText,$AgentNotify,$Financial);

            mysql_query("INSERT INTO Propertylog SET
                Active='Y',
	MLS='$MLS',
	Number='$Number',
	Street='$Street',
	City='$City',
	Province='$Province',
	Agent='$Agent',
	Telephone='$fromNumber',
	Notify=1,
	Date='$Date',
	TimeStamp=CURRENT_TIMESTAMP");

         }
         else if($PropertyInfoNum > 1 OR $extended==1){
            $id = $TelNumberInfo['ID'];

            if($extended==0){
               mysql_query("UPDATE Txtlog SET TimeStamp = CURRENT_TIMESTAMP, Extended = '1' WHERE ID = $id");
               $Index = 1;
               $Msg="Multiple Properties found\n";
               while($PropertyInfo3 = mysql_fetch_array($PropertyInfoQuery)){
                  $HouseID[$Index]=$PropertyInfo3['ID'];
                  $HouseKeyword[$Index]=$PropertyInfo3['Keyword'];
                  $HouseSubkeyword[$Index]=$PropertyInfo3['Subkeyword'];
                  $HouseNumber[$Index]=$PropertyInfo3['Number'];
                  $HouseStreet[$Index]=$PropertyInfo3['Street'];
                  $HouseCity[$Index]=$PropertyInfo3['City'];
                  $HouseProvince[$Index]=$PropertyInfo3['Province'];
                  $HouseTempKeyword[$Index]=$HouseSubkeyword[$Index].$Index;
                  mysql_query("INSERT INTO TxtlogExtended (`ID`, `Telephone`, `Keyword`, `Subkeyword`, `TempKeyword`, `Index`) VALUES (NULL, '$fromNumber', '$HouseKeyword[$Index]', '$HouseSubkeyword[$Index]', '$HouseTempKeyword[$Index]', '$HouseID[$Index]')");
    
                  $Msg=$Msg.$HouseNumber[$Index]." ".$HouseStreet[$Index]." text ".$HouseTempKeyword[$Index]."\n";
                  $Index+=1;    
               }
               
               sendSMS($fromNumber,$Msg,0,0);
            }
            else{
               $MulitPropertyInfoQuery = mysql_query("SELECT * FROM TxtlogExtended WHERE TempKeyword = `$result`");
               $MulitPropertyInfoNum = mysql_num_rows($MultiPropertyInfoQuery);
               if($MultiPropertyInfoNum == 1){
                  $MultiPropertyInfo = mysql_fetch_array($MultiPropertyInfoQuery);
                  $MultiPropertyID = $MultiPropertyInfo['Index'];
                  $FinalMultiPropertyInfoQuery=mysql_query("SELECT * FROM Properties WHERE ID = '$MultiPropertyID'");
                  $FinalMultiPropertyInfo=mysql_fetch_array($FinalMultiPropertyInfoQuery);
                  $FinalMultiPropertyDescription=$FinalMultiProprtyInfo[Description];
                  $AgentNotify=0;
            $Financial=$FinalMultiPropertyInfo['FinancialNotify'];
            if($FinancialNotify=NULL) $FinancialNotify=0;
                  sendSMS($fromNumber,$FinalMultiPropertyDescription,$AgentNotify,$Financial);
                  mysql_query("UPDATE Txtlog SET Extended = '0' WHERE TelNumber = $fromNumber");
                  mysql_query("DELETE FROM TxtlogExtended WHERE Telephone = '$fromNumber'");
               }
               else{
                  $Msg = "Property ".$result." not found. Please check the keyword and try again or reply with CLEAR to remove previous keywords from system and start over.";
                  sendSMS($fromNumber,$Msg,0,0);
               }      
            }
         }
         else{
             $Msg = "Property number ". $result." not found, please try again or reply with CLEAR to remove previous keywords from system and start over. Std msg rates may apply.";
             sendSMS($fromNumber,$Msg,0,0);
         }
         
      }
      else{
         mysql_query("DELETE FROM Txtlog WHERE TelNumber = '$fromNumber'");
         NewKeywordReceived($result,$fromNumber);
      }
   }
   else{
      NewKeywordReceived($result,$fromNumber);   
   }
}

?>

 

I just tried it again without any changes to the script detecting the width and it showed width of 480 although the device was held vertical and it should have showed 320px

 

The url show the width of 480px now, the full url looks like this:

http://a411.co/index.php?ID=4&screen_check=done&Width=480&Height=241

 

Dont know where it takes the values for width and height, the display of my phone is 320px wide and 480px high

 

mod edit: removed account/phone numbers

I posted wrong file but on the good note I think I got it.

The screen width detection  script was under the <head> section which contains the viewport meta tag and it seem that this was the issue. I moved it above the head section of the file and now everything works good, at least for the time being.

 

I would like to thank everyone who took time to help me solve this issue.

 

I think I was too happy too quickly but it seems that the issue occurs when the same page is reloaded or loaded again without closing the browser. If the page is loaded first time or the browser is closed and opened again before second page viewing everything is ok.

It is almost like the values are adding up somehow if the cache is not cleared. Wonder if it can be resolved somehow.

My actual code is below.

<?php
$PropertyID = $_GET["ID"];
include "./db/db_textback.inc.php";
?>

<?php

if(!isset($_GET['screen_check']))
{
//	$_GET['Width']=0;
/* This code will be executed if screen resolution has not been detected.*/
echo "<script type='text/javascript'>
<!-- 
document.location=\"?ID=$PropertyID&screen_check=done&Width=\"+screen.width+\"&Height=\"+screen.height;
//-->
</script>";
}
else 
{    
/* This code will be executed after screen resolution is detected.*/
    if(isset($_GET['Width'])){// && isset($_GET['Height'])) 
	// Resolution  detected
$ScreenWidth=$_GET['Width'];

     }
     else {
               // Resolution not detected
               	$ScreenWidth=235;
     }
}
echo "Screen width = ".$ScreenWidth;

?> 
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"/>
</head>


<?php

//$TelNumberQuery = mysql_query("SELECT * FROM Txtlog WHERE TelNumber = '$fromNumber'");
// $TelNumberNum = mysql_num_rows($TelNumberQuery);
//   $TelNumberInfo = mysql_fetch_array($TelNumberQuery);

function ImageSize($img, $maxwidth){
list($width, $height) = getimagesize($img);
if ($width>$maxwidth){
	$ratio=$maxwidth/$width;
	$newwidth=$maxwidth;
	$newheight=$height*$ratio;
}
else{
	$newwidth=$width;
	$newheight=$height;
}
echo $newwidth."  ".$newheight;
return array($newwidth, $newheight);
}

function AgentMessage(){
	if($AgentInfo['DisplayName']!=NULL){
	echo $AgentInfo['DisplayName'];
}
else{
	echo $AgentInfo['FirstName']." ".$AgentInfo['LastName'];
}
?></br><?php
if($AgentInfo['Title']!=NULL){
	echo $AgentInfo['Title'];
?></br><?php
}
if($AgentInfo['Brokerage']!=NULL){
	echo $AgentInfo['Brokerage'];
?></br><?php
}
if($AgentInfo['Telephone']!=NULL && $AgentInfo['DisplayTelephone']==1){
	echo "Telephone: ".$AgentInfo['Telephone'];
?></br><?php
}
if($AgentInfo['Cell']!=NULL && $AgentInfo['DisplayCell']==1){
	echo "Cell: ".$AgentInfo['Cell'];
?></br><?php
}
if($AgentInfo['Fax']!=NULL && $AgentInfo['DisplayFax']==1){
	echo "Fax: ".$AgentInfo['Fax'];
?></br><?php
}
if($AgentInfo['Email']!=NULL && $AgentInfo['DisplayEmail']==1){
	echo "Email: ".$AgentInfo['Email'];
?></br><?php
}
}

//$PropertyID=2;
$PropertyPicsQuery=mysql_query("SELECT * FROM PropertyPics WHERE PropertyID = '$PropertyID'");
$PropertyPicsNum=mysql_num_rows($PropertyPicsQuery);
$Index=0;
while($PropertyPicsInfo=mysql_fetch_array($PropertyPicsQuery)){
$PictureURL[$Index]=$PropertyPicsInfo['PicURL'];
$PictureAlt[$Index]=$PropertyPicsInfo['PicAlt'];


//START OF IMAGE CHANGE ROUTINE
/*
$maxwidth = 120;
$maxheight = 150;

$img = imagecreatefromjpeg($jpgimage); 
//or imagecreatefrompng,imagecreatefromgif,etc. depending on user's uploaded file extension

$width = imagesx($img); //get width and height of original image
$height = imagesy($img);

//determine which side is the longest to use in calculating length of the shorter side, since the longest will be the max size for whichever side is longest.    
if ($height > $width) 
{   
$ratio = $maxheight / $height;  
$newheight = $maxheight;
$newwidth = $width * $ratio; 
{
else 
{
$ratio = $maxwidth / $width;   
$newwidth = $maxwidth;  
$newheight = $height * $ratio;   
}

//create new image resource to hold the resized image
$newimg = imagecreatetruecolor($newwidth,$newheight); 

$palsize = ImageColorsTotal($img);  //Get palette size for original image
for ($i = 0; $i < $palsize; $i++) //Assign color palette to new image
{ 
$colors = ImageColorsForIndex($img, $i);   
ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']);
} 

//copy original image into new image at new size.
imagecopyresized($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($newimg,$outputfile); //$output file is the path/filename where you wish to save the file.  
//Have to figure that one out yourself using whatever rules you want.  Can use imagegif() or imagepng() or whatever.
*/
//END OF IMAGE CHANGE ROUTINE

$Index+=1;
}
$Count=$Index;
$Index=0;
$PropertyInfoQuery=mysql_query("SELECT * FROM Properties WHERE ID = $PropertyID");
$PropertyInfo=mysql_fetch_array($PropertyInfoQuery);
//echo "Number";
//echo $PropertyPicsNum;
$Title = $PropertyInfo['Number']." ".$PropertyInfo['Street'].", ".$PropertyInfo['City'].", ".$PropertyInfo['Province'];
?>
<body>
<center><h3><?php echo $Title ?></h3></center>
<?php
//echo "Property id ".$PropertyID;
while($Index<4 && $Index<$Count){
if($PropertyPicsNum>0){
	$image=$PictureURL[$Index];
	$alt=$PictureAlt[$Index];
	list($newwidth, $newheight)=ImageSize($PictureURL[$Index],$ScreenWidth);

?>

<center> <img src="<?php echo $PictureURL[$Index];?>" alt="<?php echo $PictureAlt[$Index]; ?>" width="<?php echo $newwidth; ?>" height="<?php echo $newheight; ?>" /> </center></br>
<?php
	$Index+=1;
}
?>


<?php
//$Index+=1;
}
if($PropertyInfo['MLS']!=NULL){
?>
<center><?php echo "MLS# ".$PropertyInfo['MLS']; ?></center></br>

<?php
}
if($PropertyInfo['Price']!=NULL){
?>
<center><?php echo "Price $".$PropertyInfo['Price']; ?></center></br>

<?php
}
if($PropertyInfo['WebDescription']!=NULL){
?>
<center><?php echo $PropertyInfo['WebDescription']; ?></center></br>
<?php
}
$AgentID=$PropertyInfo['AgentID'];
$AgentInfoQuery=mysql_query("SELECT * FROM Agents WHERE ID = $AgentID");
$AgentNum=mysql_num_rows($AgentInfoQuery);
if($AgentNum>0){
$AgentInfo=mysql_fetch_array($AgentInfoQuery);
if($AgentInfo['Picture']==NULL){
	AgentMessage();
}
else{?>
<table border="0" width="100%">
<tr>
<td width="50%">row 1, cell 1</td>
<td width="50%">row 1, cell 2</td>
</tr>
</table>
<?php	}
}

while($Index<$Count){
if($PropertyPicsNum>0){
	$image=$PictureURL[$Index];
	$alt=$PictureAlt[$Index];
	list($newwidth, $newheight)=ImageSize($PictureURL[$Index],$ScreenWidth);

?>

<center> <img src="<?php echo $PictureURL[$Index];?>" alt="<?php echo $PictureAlt[$Index]; ?>" width="<?php echo $newwidth; ?>" height="<?php echo $newheight; ?>" /> </center></br>
<?php
	$Index+=1;
}
}
?>

</body>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.