Jump to content

Imagecreatefromjpeg()


danparker

Recommended Posts

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?

<?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>

Link to comment
https://forums.phpfreaks.com/topic/271031-imagecreatefromjpeg/
Share on other sites

You are reading the folder images/gallery, but only using the filename in $file in the remainder of the code. $file isn't where the file is at, it's just the filename. You would need to add images/gallery/ to get the actual location of the file.

Link to comment
https://forums.phpfreaks.com/topic/271031-imagecreatefromjpeg/#findComment-1394438
Share on other sites

You need to include the directory along with the filename when you try and open the file, since you're reading the files from a different directory.

 

Because your script is located at /home/a6125536/public_html/gallery.php, when you are doing imagecreatefromfile($file) (where $file=emmaus001.jpg) it is going to try and locate that file at /home/a6125536/public_html/emmaus001.jpg.  That of course is not the correct location.

 

The correct way to try and access the file would be:

$src = imagecreatefromfile("images/gallery/$file");

 

You would need to include the directory in any other places where you're trying to reference the file as well (ie, your other imagecreatefrom* lines).

 

Link to comment
https://forums.phpfreaks.com/topic/271031-imagecreatefromjpeg/#findComment-1394446
Share on other sites

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.