PHP imagejpeg() function not working

Discussion in 'Mac Programming' started by larswik, Jul 4, 2012.

  1. larswik macrumors 68000

    Sep 8, 2006
    I am using a php script to upload a jpeg file and then resize it to something smaller. When I test the code locally it works great and I end up with the uploaded file and reduced file. But when trying to test this from my local machine and uploading to my server I get a error. But the first file I upload before resizing uploads fine? the creation of this second file is causing a problem?

    Warning: imagejpeg() [function.imagejpeg]: Unable to open 'ftp://user:pass@mySite/public_html/somefolder/test/images/photos/photo9.jpg' for writing: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/upload_test.php on line 219
    Here is the code that I use to save the resized version

    $src = imagecreatefromjpeg($imageToResize);
    if($src == false) {
          $error = "Unknown problem trying to open uploaded image.";
          return false;
    $resizedImage = imagecreatetruecolor(320, 213);
    imagecopyresampled($resizedImage, $src, 0, 0, $removeAmount, 0, 320, 213, $width, $height);
    $toTouch = "";
    //$toTouch = "uploads/photo15.jpg"; // I use this to test my local storage
    [B]imagejpeg($resizedImage,$toTouch, 100);[/B]
    It's writing to the exact same folder. There were a few work arounds that I found on the web like writing to a buffer first ob_start() to using an fopen and fclose to create the file to write to but nothing worked. When I created a file using fopen it created the file but it was 0K and I still get the error above?

    any ideas?
  2. larswik thread starter macrumors 68000

    Sep 8, 2006
    Bump. Still need help with this. Everything works perfectly when I write it locally. I am using a different technique writing everything to a buffer first and then trying to upload it to the server. I get no errors anymore but the data stops writing and my photos look like this attached image I took a screen shot of. I am using a buffer to capture the data and trying to write it out.

    What would cause the code to stop writing and think it is finished?
       imagejpeg($resizedImage, null, 100);
       $data = ob_get_clean();
       $bytesWritten = file_put_contents($toTouch, $data);

    Attached Files:

  3. jared_kipe, Jul 5, 2012
    Last edited: Jul 5, 2012

    jared_kipe macrumors 68030


    Dec 8, 2003
    Possibly related to your other thread

    When you used fopen() to write buffered output, did you use a stream_context? It may be FTP protocol related. Try opening and writing to a local file on the same server.

    On a side note, why are you even bothering with FTP in the first place? If you are writing something to upload, resize and image, and download the result, you can do that all with PHP and HTTP alone.

    EDIT: Also "ftp://user: pass@mySite/public_html/somefolder/test/images/photos/photo9.jpg" does not SEEM like a particularly good url. Namely, the protocol (ftp://) and the uri fragment 'public_html' don't seem to mix.
  4. larswik thread starter macrumors 68000

    Sep 8, 2006
    Thanks for your reply! I have been struggling with this for days now. It seems other people have had an issue with imagejpeg(); which I read about here

    Bellow on that link people mention that they get an error like I was that the directory or file could not be found? I tried version with fopen, file_put_content and then I read about some writing to a buffer first ob_start() and outputting it. That seemed to get me the furthest.

    When I upload my script I can upload my file via FTP but it won't resize and save the file as another jpg file. But from what you say with the FTP:// name I guess it makes sense that I would not use a user and pass with FTP if the resizing is happening on the server where the file is.

    I have 5 websites 1 is my main site and the others are called addons and sit in the public_html folder and are folders. so in Public_html I have a folder called

    I upload the file like this, which works.
    $login_result = ftp_login($con_id, $ftp_user_name, $ftp_user_pass);
    $upload = ftp_put($con_id, $writeLocation, $temp_name, FTP_BINARY);
    My write location is this. It has 2 variables that are replaced.

    $writeLocation = "public_html/myWebSiteFolder/app/clients/$clientName/images/photos/$bName.jpg";
    But once the file is uploaded like you say. I guess I don't need to log in again with FTP://. just convert the file and save it since it is already there.

  5. jared_kipe macrumors 68030


    Dec 8, 2003
    OK, I obviously didn't read the function declaration of imagejpeg().
    imagejpeg() is used in combination with header('Content-Type: image/jpeg'); to literally return the image resource to the browser. As in to download it.

    Can you dumb down what you are actually trying to accomplish, and why?
  6. jared_kipe macrumors 68030


    Dec 8, 2003
    Also now that I reread this it makes more sense. You're turning on output buffering to try to 'capture' the data that is being sent to the browser with imagejpeg().

    However, ob_get_clean() returns a bool, so thats not going to be particularly useful at writing with file_put_contents(). I'll assume you wanted ob_get_contents(), but this doesn't seem like a particularly GREAT way to do this.

    I would guess this would be a much better idea.
    $path = 'some/path/you/can/write/to.jpg';
    imagejpeg($resizedImage, $path, 100);
  7. larswik thread starter macrumors 68000

    Sep 8, 2006
    I have tonight available. Let me try it out and see what works and get back to you. I am really thankful for the help. The end result of this is a website for my iPhone app that will allow my users that are on my app to manage there own images buy uploading and replacing them.

    I will look through the code and work in your example.

    I will let you know.
  8. larswik thread starter macrumors 68000

    Sep 8, 2006
    WOW! that took all of 16 minutes to test with success! Thanks for always begin there with my PHP questions!

    Here is where I went wrong.

    The code that I listed above was used to upload the image from the host computer to the server and that worked fine and then I closed the connection.

    I then used a function to re size it but in order to resave the new image I had to make an FTP connection again to write it. When I uploaded this code to the computer I never even thought or changing how it logs into the FTP site.

    I wrote one simple line of code that was the new path starting where the php script was located to it's destination folder

    $path = "clients/test/images/photos/photo9.jpg";
    I then just deleted all the unnecessary buffering code, and uploaded the new script to my server. When I ran it it uploaded the selected image and converted it to a smaller file and save that one.

    I have been banging my head against the wall since Tuesday to figure out why it was not working. I got so focused at solving this problem I did not step away from it and try to tackle it with a clear train of thought.

    Thanks again for all your help as I am learning PHP.

Share This Page