Jump to content

why wont jpg work with this image script?


jimmi8

Recommended Posts

Hi,

 

I have a script that takes an image, gets its dimensions, and reisizes it if its too big for its allocated space. It works fine with gifs but not with jpg's. I cant work out why and am hoping someone can shed light on it.

 

Heres the code that gets the image size, im doing the resizing later on...

 

 

//find out current size of article image and assign the resulting array to a variable
if(isset($image)) {
    if(substr($type, 6,4) == 'jpeg') {
        $type1 = 'jpg';
    }
    else { $type1 = 'gif';
    }
$file = $image.'.'.$type1;
echo $file;
$imagearray = getimagesize("images/$file");
}

So basically here $image is the image id, $type id the image type ( of which im doing a substr on to get just the image type because by default it comes out as images/jpg, images/gif etc). Now as i say it works with gifs...the images displays but with jpg i get "cannot find file or directory". The file is definately there though.

 

I cannot work this out. When i echo $file i get the correct return.......ie: 56.jpg. the echo of file for a gif is 56.gif for example and the image returns fine. So i know that the bit of code posted above is doing its job

 

Heres the rest of the code.....its a mess as ive been playing around with different bits of this page for about a week on and off.....im kinda embarressed to post it but what the hell!

 

<?php

session_start();
  if (!isset($_SESSION['first_name'])) {
  header ("Location:  http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php");
    exit(); // Quit the script.
  }

  
  //function to resize images
  function imageresize($width, $height, $target) {
    if($width > $height) {
      $percentage = ($target / $width);
    }
    else {
      $percentage = ($target / $height);
    }
    // Gets the new value and applies the percentage, then rounds the value
    $width = round($width * $percentage);
    $height = round($height * $percentage);
    return "width=\"$width\" height=\"$height\"";
  }
   




require_once ('mysql_connect.php'); // Connect to the db.

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
        <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1">
        <meta name="author" content="James Filtness">
        <title>Boudaki</title>
                <link rel="stylesheet" href="css.css" type="text/css">

        
            
    </head>
<body>
    <div id="frame">
       <div id="header">
       <div id="header_left">
       <p class="motto" style="margin: 2px 0 2px 0px;"><b>Boudaki.com</b></p>     <?php
  require_once ('mysql_connect.php'); // Connect to the db.
  $sql = "SELECT tagline FROM tagline ORDER BY tagline_id DESC LIMIT 1";
  $query = mysql_query($sql);
  while ($row = mysql_fetch_array($query)) {
      $tag = $row['tagline'];
  }
  
  ?>

  
    
         
         <?php echo '<p class="tease"><span class="accent">' . $tag . '</span></p>'; ?>
         <img class="heady" src="heady.gif" alt="heady" />

       
        
</div>
<div id="header_right">
<p class="motto"><span class="accent"><?php echo date('F j, Y'); ?></span></p>
<p class="tease">ONLINE:
        <?php
session_start();
        $name = ($_SESSION['first_name']);



if(!session_is_registered('online')){
    @mysql_query("INSERT INTO ppl_online (session_id, activity, ip_address, refurl, user_agent, user_name) VALUES ('".session_id()."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGENT']}', '$name')");
    session_register('online');
}  

if(session_is_registered('online')){         
    @mysql_query("UPDATE ppl_online SET activity=now() WHERE session_id='".session_id()."'");
}


$count == 1;
$limit_time = time() - 150; // 5 Minute time out. 60 * 5 = 300
$sql = "SELECT * FROM ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time";
$result = mysql_query($sql);
$amount = mysql_num_rows($result);
while ($visits = mysql_fetch_array($result)) {
$count ++;
$users = $visits['user_name'];

?>

<?php if($amount == 1) { echo  'its just you buckaroo';} else { echo $users; if($amount == $count) {echo '';} else {echo ',';} }  ?>


<?php
}
?>
</p>

             

</div>
</div>
<div id="main">
<div id="left">
    

<ul>
            
        
        <?php
$sql = "SELECT entries.*, categories.*, uploads.*, images.*, entries.blog_id, entries.category_id,
        ( SELECT COUNT(*) FROM comments WHERE comments.blog_id = entries.blog_id ) as comments FROM entries
        INNER JOIN categories ON(categories.category_id = entries.category_id)
        LEFT JOIN uploads ON (entries.blog_id = uploads.blog_id)
        LEFT JOIN images on (entries.blog_id = images.blog_id) ORDER BY entries.date_submitted DESC LIMIT 5";
$query = mysql_query($sql);


while ($row = mysql_fetch_array($query)) {
    $blog_id  = $row['blog_id'];
    $title = $row['title'];
    $body = substr($row['body'],0,400);
    $author = $row['author_id'];
    $date = $row['date_submitted'];
    $cat = $row['category_id'];
    $cat1 = $row['category'];
   $upload = $row['upload_id'];
   $image = $row['image_id'];
   $type = $row['file_type'];
   $no_of_comments = $row['comments'];

//find out current size of article image and assign the resulting array to a variable
if(isset($image)) {
    if(substr($type, 6,4) == 'jpeg') {
        $type1 = 'jpg';
    }
    else { $type1 = 'gif';
    }
$file = $image.'.'.$type1;
echo $file;
$imagearray = getimagesize("images/$file");
}


if (($imagearray[0] || $imagearray[1]) < 5) {
    
    
$sizeup = '100';
}


elseif (($imagearray[0] || $imagearray[1]) > 20) {

$sizeup = '190';
}

elseif (($imagearray[0] || $imagearray[1] > 600)) {
$sizeup = '20';
}
    
        
?>
<li>
    

<h3> <?php if(!isset($upload)) {
    
              
                 echo '<a href="individualarticle.php?f=0&blog_id=' .$row['blog_id'] .'">';
             } else {
              

                 echo '<a href="individualarticle.php?f=1&blog_id=' .$blog_id .'">';
             }
             ?>
             <?php echo $title; ?>
        
             </a>
      
</h3>

<h4>written by <span class="accent"><?php echo $author; ?> on <?php echo $date; ?> in <?php echo '<a href="categorypage.php?category_id=' .$cat. '">'; ?></span><?php echo $cat1; ?></a>  <?php if(isset($upload)) { echo '<img class="noborder" src="pin.gif" />'; } ?></h4>

<?php if(isset($image)) { echo '<p class="image"><img class="article_image" src="images/'.$image.'.'.(substr($type, 6,4)).'"'.imageresize($imagearray[0],$imagearray[1], $sizeup);} ?>

     <p><?php echo $body; ?>....</p>
     <?php if(!isset($upload)) {
              
                 echo '<a class="read" href="individualarticle.php?f=0&blog_id=' .$row['blog_id'] .'">read on...</a>';
             } else {
              

                 echo '<a class="read" href="individualarticle.php?f=1&blog_id=' .$blog_id .'">read on...</a>';
             }
             ?>

     <?php if(isset($no_of_comments)) {

         if(!isset($upload)) {
              
                 echo '<a class="read" href="individualarticle.php?f=0&blog_id=' .$row['blog_id'] .'">comments ('. $no_of_comments . ')</a>';
             } else {
              

                 echo '<a class="read" href="individualarticle.php?f=1&blog_id=' .$blog_id .'">comments ('. $no_of_comments . ')</a>';
             }
     }
   

             ?>

     

             


</li>
<?php }

?>
</ul>
</div>
<div id="right">
    <script language="JavaScript" type="text/javascript">
            var sendReq = getXmlHttpRequestObject();
            var receiveReq = getXmlHttpRequestObject();
            var lastMessage = 0;
            var mTimer;
            //Function for initializating the page.
            function startChat() {
                //Set the focus to the Message Box.
                document.getElementById('txt_message').focus();
                //Start Recieving Messages.
                getChatText();
            }        
            //Gets the browser specific XmlHttpRequest Object
            function getXmlHttpRequestObject() {
                if (window.XMLHttpRequest) {
                    return new XMLHttpRequest();
                } else if(window.ActiveXObject) {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } else {
                    document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';
                }
            }
            
            //Gets the current messages from the server
            function getChatText() {
                if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
                    receiveReq.open("GET", 'getChat.php?chat=1&last=' + lastMessage, true);
                    receiveReq.onreadystatechange = handleReceiveChat;
                    receiveReq.send(null);
                }            
            }
            //Add a message to the chat server.
            function sendChatText() {
                if(document.getElementById('txt_message').value == '') {
                    alert("You have not entered a message");
                    return;
                }
                if (sendReq.readyState == 4 || sendReq.readyState == 0) {
                    sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
                    sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                    sendReq.onreadystatechange = handleSendChat;
                    var param = 'message=' + document.getElementById('txt_message').value;
                  param += '&name=<?php echo $_SESSION['first_name'];  ?>';
                    param += '&chat=1';
                    sendReq.send(param);
                    document.getElementById('txt_message').value = '';
                }                            
            }
            //When our message has been sent, update our page.
            function handleSendChat() {
                //Clear out the existing timer so we don't have
                //multiple timer instances running.
                clearInterval(mTimer);
                getChatText();
            }
            //Function for handling the return of chat text
            function handleReceiveChat() {
                if (receiveReq.readyState == 4) {
                    var chat_div = document.getElementById('div_chat');
                    var xmldoc = receiveReq.responseXML;
                    var message_nodes = xmldoc.getElementsByTagName("message");
                    var n_messages = message_nodes.length
                    for (i = 0; i < n_messages; i++) {
                        var user_node = message_nodes[i].getElementsByTagName("user");
                        var text_node = message_nodes[i].getElementsByTagName("text");
                        var time_node = message_nodes[i].getElementsByTagName("time");
                        chat_div.innerHTML += user_node[0].firstChild.nodeValue + ' ';
                        chat_div.innerHTML += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font><br />';
                        chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
                        chat_div.scrollTop = chat_div.scrollHeight;
                        lastMessage = (message_nodes[i].getAttribute('id'));
                    }
                    mTimer = setTimeout('getChatText();',2000); //Refresh our chat in 2 seconds
                }
            }
            //This functions handles when the user presses enter.  Instead of submitting the form, we
            //send a new message to the server and return false.
            function blockSubmit() {
                sendChatText();
                return false;
            }
            //This cleans out the database so we can start a new chat session.
            function resetChat() {
                if (sendReq.readyState == 4 || sendReq.readyState == 0) {
                    sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
                    sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                    sendReq.onreadystatechange = handleResetChat;
                    var param = 'action=reset';
                    sendReq.send(param);
                    document.getElementById('txt_message').value = '';
                }                            
            }
            //This function handles the response after the page has been refreshed.
            function handleResetChat() {
                document.getElementById('div_chat').innerHTML = '';
                getChatText();
            }    
        </script>
    </head>
    <body onload="javascript:startChat();">
    <h2>recently...</h2>
<ul>
    

<?php

$sql = "SELECT entries.*, uploads.*, entries.blog_id FROM entries LEFT JOIN uploads ON (entries.blog_id = uploads.blog_id) order by date_submitted DESC LIMIT 3, 6";

$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $title = $row['title'];
    $blog_id = $row['blog_id'];
    $upload = $row['upload_id'];

    ?>
    <li><?php if(!isset($upload)) {
              
                 echo '<a href="individualarticle.php?f=0&blog_id=' .$row['blog_id'] .'">' . $title . '</a>';
             } else {
              

                 echo '<a href="individualarticle.php?f=1&blog_id=' .$blog_id .'">' . $title . '</a>';
             }
             ?></li>
    
<?php
}
?>


</ul>



      <h2 class="top">Chat</h2>
      <div id="div_chat">
            
            </div>
                

        <form id="frmmain" name="frmmain" onsubmit="return blockSubmit();">
            
            <input type="text" id="txt_message" name="txt_message" style="width: 200px; margin: 0 0 0 -5px; padding: 0; border: 2px solid #888; " />
            <input type="button" name="btn_reset_chat" id="btn_reset_chat" class="submit" value="reset chat" onclick="javascript:resetChat();" /><br />
            <input type="button" name="btn_send_chat" id="btn_send_chat" class="submit" value="send" onclick="javascript:sendChatText();" />
        </form>

            <h2>Files</h2>
    <ul>

            
<?php
require_once ('mysql_connect.php'); // Connect to the db.

$sql = "SELECT upload_id, file_name FROM uploads ORDER BY upload_date DESC LIMIT 10 ";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$upload_id = $row['upload_id'];
$file_name = $row['file_name'];


?>
<li><a href="downloadfile.php?file_id=<?php echo $upload_id; ?>"><?php echo $file_name; ?></a></li>
<?php
}

?>
        
    </ul>
            <h2>categories</h2>
<ul>
    

<?php

$sql = "SELECT category, category_id FROM categories order by category asc";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {

$cat = $row['category'];
$cat_id = $row['category_id'];


?>
<li><a href="categorypage.php?category_id=<?php echo $cat_id; ?>"><?php echo $cat; ?></a></li>
<?php
}
?>
</ul>

<h2>Links</h2>
        <p>
            
            
<?php

require_once ('mysql_connect.php'); // Connect to the db.

$sql = "SELECT link_title, url FROM LINKS LIMIT 30";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);

while ($row = mysql_fetch_array($query)) {
$linktitle = $row['link_title'];
$url = $row['url'];
?>
<a href="<?php echo $url; ?>"><?php echo $linktitle; ?></a>
<?php
}

?>

    


        </p>

</div>


    


</div>

      
  
  <div id="footer">

<p class="motto">
<span class="accent">
    
© 2007 BOUDAKI All Rights | <script type="text/javascript">
</script> | <a href="http://coudalpartners.projectpath.com/login/">VALIDATE</a> |<a href="http://feeds.feedburner.com/CoudalFreshSignals">RSS Feed</a>

<script type='text/javascript' src='http://track.mybloglog.com/js?mblID=2005030318271048'></script></p>

</div>
</div>

        
</body>
</html> 

Link to comment
https://forums.phpfreaks.com/topic/41259-why-wont-jpg-work-with-this-image-script/
Share on other sites

I'm not positive if I am right or not, but I will take a stab it at. This line of code:

 

if(substr($type, 6,4) == 'jpeg') {

 

The substr() paramaters you have in there is like saying the file HAS to look something like this:

 

123654.jpg - Your saying you need a 6 letter filename with the .jpg file type.

 

You would probably be better off using REGEX to determin the file extension.

hi thanks for the reply. Sorry i really should have mentioned this but im taking a substr of $type as by default from the database the $type variable comes out as images/jpg. My script kinda works as i say. When i echo out $file i get 45.jpg or whatever. NOw .gif works and i get a image but jpg does not. If i print_r the ($imagearray) i dot get anything back from .jpg images.

 

arrggghh!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.