[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

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?
0
jimfrith
Asked:
jimfrith
  • 4
  • 2
1 Solution
 
CaveyCoUkCommented:
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....
0
 
jimfrithAuthor Commented:
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>';
}

?>
0
 
CaveyCoUkCommented:
Code looks fine and works fine for me, I cant see why it would work for PNG and not JPG, so back to it being an issue with file permissions (which is the only way I managed to recreate the error).

Set ./photos/ to 0777.  Note - You dont define $use (I take it you meant userid) and you use "./logo" instead of the directories you specified at the top of the file - you might have meant this but it doesn't seem 'right' so I replaced them with things you do define.  Anyway, make sure you set that directory to 0777 (so +xrw for all users) else this probably will not work.

Change

               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>";  
               }

to something more like

             if (!is_dir($Photo_Dir . $User_Dir)){
                  mkdir($Photo_Dir . $User_Dir);
                  chmod($Photo_Dir . $User_Dir,0777);
             }
               if (move_uploaded_file($_FILES['userfile']['tmp_name'], $Photo_Dir . $User_Dir .$_FILES['userfile']['name'] .'.jpg'))
               {
                    // chmod to allow webserver to display the pic
                    chmod( $Photo_Dir. $User_Dir  .$_FILES['userfile']['name']. '.jpg',0755);
                   
                    // debug
                    //print "Checking picture size<br>";
                   
                    // resizing the pic
                    resize_pic( $Photo_Dir. $User_Dir  .$_FILES['userfile']['name']. '.jpg',$maxwidth,$maxheight);

                    // display preview

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

                    echo "<script language=javascript>window.location.href=\"http://www.extremejobs.ca/change_u.php\"</script>";  
               }

and it should start working.  The first if statement will automatically create the users directory if it does not already exist and chmod it to 0777 (which means other users can access it).  If you chmod it to 0755, chances are the webserver will still be able to read/write to the directory, but ftp or shell users will not.

Hope that helps.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jimfrithAuthor Commented:
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.
0
 
jimfrithAuthor Commented:
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?
0
 
jimfrithAuthor Commented:
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now