Link to home
Start Free TrialLog in
Avatar of jimfrith
jimfrith

asked on

gd library problem

I have a script that uploads an image and then resizes the image.  however I get the following error:

chmod() [function.chmod]: No such file or directory in...

Fatal error: Call to undefined function: imagecreatefromjpeg()

if I try uploading a gif I get this:

Warning: imagegif() [function.imagegif]: Unable to open '/usr/local/etc/httpd/htdocs...

when I upload a png file it seems to work correctly.

when I do a php_info I get this for gd

GD Support       enabled
GD Version       bundled (2.0.28 compatible)
GIF Read Support       enabled
GIF Create Support       enabled
PNG Support       enabled
WBMP Support       enabled
XBM Support       enabled

Any ideas what the problem is?
Avatar of CaveyCoUk
CaveyCoUk

First guess would be that the working directory is probably chmod'd to 755 which means apache cant write to the directory.  However, if it "works" for png files its probably not that.

Any chance you could post the code or at least the imagecreatefromgif/imagecreatefrompng etc.... lines and the imagegif/imagepng etc... lines?  That's my next guess at where the error is....
Avatar of jimfrith

ASKER

here is the entire code I am using for resizing the image.

$GD2_available = true;

// maximum filesixe
$maxsize = 102416;

// maximum dimensions
$maxwidth = 200;
$maxheight = 200;

// the directory where all the user's folders will go.
// set permissions to 777 or 755 depening on your webhost
$Photo_Dir = "./photos/";

// this allows you to set individual directories
// for users based on something unique like username, user id or email address
$User_Dir = $userid."/";

/// THIS PART IS ONLY FOR TESTING ------------------------
/// remove it or adapt it in your own way
if(isset($_GET["view"]))
{

     
     // link back to the normal page
     print "<a href='upload.php'>Back to upload</a>";
     
     // not nice but its for debuging :)
     exit();
}
///---------------------------------------------------------

// resizes the picture
// parameters are:
// 1] the path to the original picture
// 2] the maximum width
// 3] the maximum height
function resize_pic($pic,$max_width,$max_height)
{
    $img=0;
    // get size and type
    // $size[0] will be the pic's width
    // $size[1] will be the pic's height
    // $size[2] will be the pic's type
    $size = getimagesize( $pic );
   
    // debug
   // print "Checking size<br>";
   
    // if it is too big
    // we can not save GIFS :(
    if(($size[0] > $max_width || $size[1] > $max_height) && $size[2] != "GIF")
    {
         // debug
         print "Pic needs resizing<br>";
            // load the original pic NOTE: PHP does currently not support GIF
         switch( $size[2] )
               {
             // jpeg
             case 2:
                 $img = imagecreatefromjpeg($pic);
                 $img_type = "JPG";
             break;
             // png
             case 3:
                 $img = imagecreatefrompng($pic);
                 $img_type = "PNG";
             break;
             default:
                 // unsupported format
                           print "Unsupported format, could not resize<br>";
                       $img = 0;
         }
   
         // if the load was succesfull
         if($img)
               {
                 // check if the width is larger than the height
                    if($size[0] > $size[1])
                    {
                           // set the width to the maximum possible
                              $new_width = $max_width;
                              // change the height in proportion of the other
                              // adjustment
                              $new_height = ($max_width/$size[0]) * $size[1];
                    }
                    else
                    {
                              // set the height to the maximum possible
                              $new_height = $max_height;
                              /// change the width in proportion of the other
                              // adjustment
                              $new_width = ($max_height/$size[1]) * $size[0];
                    }
                   
                    print "New X = ".$new_width."; Y = ".$new_height."<br>";
                   
                    // if GD 2 function is available
                    if ($GD2_available)
                    {
                         $new_img = imagecreatetruecolor($new_width, $new_height);
                    }
                    else
                    {
                         // else use old version
                         $new_img = imagecreate($new_width, $new_height);
                    }          
                   
                    if($new_img)
                    {
                         // if GD 2 function is available
                         if ($GD2_available)
                         {
                           $res = imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
                         }
                         else
                         {
                              // use old function
                              $res = imagecopyresized($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
                         }

                         // debug
                         print "Resize OK, saving new version<br>";
                         
                         //save the pic
                          switch($img_type)
                      {
            case 'JPG':
                          imagejpeg($new_img, $pic, 90);  // here you have a quality setting
              break;
            case 'PNG':
                 imagepng($new_img, $pic);
              break;
          }
          imagedestroy($new_img);
           }
        imagedestroy($img);
         }  
          }
}
if (isset($_POST["submit"]) && isset($_FILES["userfile"]))
{
     // debug
    // print "Checking type and filesize<br>";
     if ($_FILES['userfile']['type'] == "image/jpg" || $_FILES['userfile']['type'] == "image/jpeg" || $_FILES['userfile']['type'] == "image/pjpeg" || $_FILES['userfile']['type'] == "image/png")
     {
          // debug
          //print "Checking filesize for incompatible browsers<br>";
          // check again
          if ($_FILES['userfile']['size'] <= $maxsize && $_FILES['userfile']['size'] > 0)
          {
               // debug
              // print "Cheking for directory".$Photo_Dir.$User_Dir."<br>";
               // if the users directory does not exist we create it
                             // debug
              // print "Moving file to its folder<br>";
               
               if (move_uploaded_file($_FILES['userfile']['tmp_name'], "./logo/" . $use .'.jpg'))
               {
                    // chmod to allow webserver to display the pic
                    chmod( $Photo_Dir. $use . '.jpg',0755);
                   
                    // debug
                    //print "Checking picture size<br>";
                   
                    // resizing the pic
                    resize_pic( "./logo/" . $use . '.jpg',$maxwidth,$maxheight);

                    // display preview

                         
                            // connect to database
                  mysql_query("UPDATE Company SET logo='yes' WHERE user_id = '$use' " );

                        echo "<script language=javascript>window.location.href=\"http://www.extremejobs.ca/change_u.php\"</script>";  
               }
               else
               {
                    // error could not move file
                    echo "Error: File could not be moved to ".$Photo_Dir.$_FILES['userfile']['name'];
               }
          }
          else
          {
               // filesize too big
               echo 'Error: Maximum filesize is '.(intval($maxsize/1024)).' KB';
          }
     }
     else
     {
          // unsupported format
          echo 'Error: Either this file is in an unsupported format or it is too large';
     }
}    
else
{
     // display the upload form
     echo '<form enctype="multipart/form-data" method="post">
              Maximum accepted filesize is '.(intval($maxsize/1024)).' KB.<br>
       

        Upload:<br><input name="userfile" type="file" size="20"><br><br>
           <input type="submit" value="  Upload File  " name="submit">
              </form>';
}

?>
ASKER CERTIFIED SOLUTION
Avatar of CaveyCoUk
CaveyCoUk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok thanks for that.  chmoding the directory to 0777 got rid of the error messages but when I resize a jpeg now it changes it to black and white instead of keeping the original colors. any suggestion there?  with png files the image keeps its original look.
I notice in my php_info it lists gif support and png support as enabled but it doesn't show jpg should it be listed there?
Thanks CaveyCoUk I really appreciate all your help.

My largest problem was the permissions setting.  the trouble with the quality was fixed when I removed all those if statements and defaulted it to GD2.

Thanks again