Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

imagecreatefromjpeg()

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

imagecreatefromjpeg()

Postby danparker » Thu Nov 22, 2012 3:30 am

Hi there, I am having a problem with a PHP script I am writing for an image gallery. The script should pull images from the directory: images/gallery display thumbnails and use javascript to show a large image when a thumbnail is clicked in a style similar to the locked desktop on windows vista / 7, ie. the backround greyed out.
When ran I get the following error:
Warning: imagecreatefromjpeg(emmaus001.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/a6125536/public_html/gallery.php on line 35


followed by:
Warning: imagesx(): supplied argument is not a valid Image resource in /home/a6125536/public_html/gallery.php on line 41


and:

Warning: imagesy(): supplied argument is not a valid Image resource in /home/a6125536/public_html/gallery.php on line 41


The image emmaus001.jpg is present in the directory images/gallery and is a jpg image.

Here is the code, can anyone help?
Code: Select all
<?php
   # SETTINGS
   $max_width = 5000;
   $max_height = 5000;
   
   function getPictureType($ext) {
      if ( preg_match('/jpg|jpeg/i', $ext) ) {
         return 'jpg';
      } else if ( preg_match('/png/i', $ext) ) {
         return 'png';
      } else if ( preg_match('/gif/i', $ext) ) {
         return 'gif';
      } else {
         return '';
      }
   }
   
   function getPictures() {
      global $max_width, $max_height;
      if ( $handle = opendir("images/gallery") ) {
         $lightbox = rand();
         echo '<ul id="pictures">';
         while ( ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) ) {
               $split = explode('.', $file);
               $ext = $split[count($split) - 1];
               if ( ($type = getPictureType($ext)) == '' ) {
                  continue;
               }
               if ( ! is_dir('thumbs') ) {
                  mkdir('thumbs');
               }
               if ( ! file_exists('thumbs/'.$file) ) {
                  if ( $type == 'jpg' ) {
                     $src = imagecreatefromjpeg($file);
                  } else if ( $type == 'png' ) {
                     $src = imagecreatefrompng($file);
                  } else if ( $type == 'gif' ) {
                     $src = imagecreatefromgif($file);
                  }
                  if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
                     $newW = $oldW * ($max_width / $oldH);
                     $newH = $max_height;
                  } else {
                     $newW = $max_width;
                     $newH = $oldH * ($max_height / $oldW);
                  }
                  $new = imagecreatetruecolor($newW, $newH);
                  imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
                  if ( $type == 'jpg' ) {
                     imagejpeg($new, 'thumbs/'.$file);
                  } else if ( $type == 'png' ) {
                     imagepng($new, 'thumbs/'.$file);
                  } else if ( $type == 'gif' ) {
                     imagegif($new, 'thumbs/'.$file);
                  }
                  imagedestroy($new);
                  imagedestroy($src);
               }
               echo '<li><a href="'.$file.'" rel="lightbox['.$lightbox.']">';
               echo '<img src="thumbs/'.$file.'" alt="" />';
               echo '</a></li>';
            }
         }
         echo '</ul>';
      }
   }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Emmaus Village Carlton - Gallery</title>
    <link rel="stylesheet" type="text/css" href="emmaus.css">
    <link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
    <script type="text/javascript" src="js/prototype.js"></script>
    <script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
    <script type="text/javascript" src="js/lightbox.js"></script>
   <link rel="shortcut icon" type="image/x-ico" href="images/favicon.ico">
    <style type="text/css">
#pictures li {
   float:left;
   height:<?php echo ($max_height + 10); ?>px;
   list-style:none outside;
   width:<?php echo ($max_width + 10); ?>px;
   text-align:center;
}
img {
   border:0;
   outline:none;
}
</style> 
</head>
<body background="Logo_Grey.jpg">
    <div id="header" align="center"><img src="Header.jpg" height="130" width="640" alt="Header Image" title="Welcome to the Website of Emmaus Village Carlton"><br></div>
    <!-- Navigation Table-->
    <table class="navigation">
        <tr class="navigation">
            <td class="navigation"><a class="navigation" href="index.html" title="Click Here to Visit the EVC Home Page">Home<a/></td><td class="navigation"><a class="navigation" href="about.html" title="Click Here to Visit the EVC About Us Page">About Us</a></td><td class="navigation"><a class="navigation" href="shops.html" title="Click Here for Info About the EVC Shops and Bistro">Shops and Bistro</a></td><td class="navigation"><a class="navigation" href="gallery.php" title="Click Here to View the EVC Image Gallery">Gallery</a></td>
        </tr>
        <tr class="navigation">
            <td class="navigation"><a class="navigation" href="news.html" title="Click Here to Visit the EVC News Page">News</a></td><td class="navigation"><a class="navigation" href="policy.html" title="Click Here to Learn More About Our Policies">Policies</a></td><td class="navigation"><a class="navigation" href="contact.php" title="Click Here to Contact our Office or for Directions to Our site">Contact Us</a></td><td class="navigation"><a class="navigation" href="links.html" title="Click Here to Visit the EVC Links Page">Links</a></td>
        </tr>
    </table><br>
    <!-- Content Start -->
<!--Wrapper Upper Section -->
<div id="wrapper">
<b class="b1h"></b><b class="b2h"></b><b class="b3h"></b><b class="b4h"></b>
<div class="heading"><b><h3>The EVC Image Gallery</h3></b></div>
<div class="content">

<?php getPictures(); ?>

</div>
<!--Wrapper lower section  -->
      <b class="b4bh"></b><b class="b3bh"></b><b class="b2bh"></b><b class="b1h"></b></div>
<!-- Footer -->
    <br><div id="footer" align="center">
        <br><br><br><br><p class="footer"><b><em>Emmaus Village Carlton</em></b><br>(A community of Emmaus
     Turvey Limited - Registered Charity No:1083113)<br>Registered Office: Emmaus Village
     Carlton, School Lane, Carlton, Beds MK43 7lQ<br>Vat Reg No.786691662</a></p>

    </div>
</body>
</html>


thanks in advance..
danparker
New php-forum User
New php-forum User
 
Posts: 1
Joined: Thu Nov 22, 2012 3:14 am

Re: imagecreatefromjpeg()

Postby seandisanti » Fri Nov 23, 2012 2:30 pm

for whatever reason it's not liking the value stored in $src for a filename. try using absolute paths instead of relative ones (http://whatever.com/images/gallery instead of images/gallery) and see if that works.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 773
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.