PDA

View Full Version : PHP imagejpeg() function not working




larswik
Jul 4, 2012, 08:47 PM
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
OK


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 = "ftp://user:pass@mysite.com/public_html/somefolder/test/images/photos/photo9.jpg";
//$toTouch = "uploads/photo15.jpg"; // I use this to test my local storage

imagejpeg($resizedImage,$toTouch, 100);
mageDestroy($resizedImage);



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?



larswik
Jul 5, 2012, 03:12 PM
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?

ob_start();
imagejpeg($resizedImage, null, 100);
$data = ob_get_clean();
$bytesWritten = file_put_contents($toTouch, $data);

jared_kipe
Jul 5, 2012, 05:51 PM
Possibly related to your other thread http://forums.macrumors.com/showthread.php?p=15144950#post15144950

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.

larswik
Jul 5, 2012, 08:08 PM
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 http://www.php.net/manual/en/function.imagejpeg.php

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.

$con_id=ftp_connect($ftp_server,$ftp_port);
$login_result = ftp_login($con_id, $ftp_user_name, $ftp_user_pass);

$upload = ftp_put($con_id, $writeLocation, $temp_name, FTP_BINARY);
ftp_close($con_id);



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.

hummm.

jared_kipe
Jul 5, 2012, 09:35 PM
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 http://www.php.net/manual/en/function.imagejpeg.php

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.

$con_id=ftp_connect($ftp_server,$ftp_port);
$login_result = ftp_login($con_id, $ftp_user_name, $ftp_user_pass);

$upload = ftp_put($con_id, $writeLocation, $temp_name, FTP_BINARY);
ftp_close($con_id);



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.

hummm.

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?

jared_kipe
Jul 5, 2012, 09:46 PM
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?

ob_start();
imagejpeg($resizedImage, null, 100);
$data = ob_get_clean();
$bytesWritten = file_put_contents($toTouch, $data);


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);
imagedestroy($resizedImage);
header("Location: http://www.example.com/{$path}");
exit();

larswik
Jul 5, 2012, 10:10 PM
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.

larswik
Jul 5, 2012, 10:30 PM
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.