Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving a file in GD

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
Workng with GD library but have hit a wall. I have a script which resizes an image and then prints it to a browser with.
Code:
ImageJPEG($thumb);
That works fine but I want to save a copy of that image. Example scripts show the following code but I cannot get it to save the file.
Code:
imagejpeg($thumb, 'zytr.jpg', 100) ;
I have set the permissions to 'wide open' but no joy - can anyone see what I am missing?

Keith
 
I was not sure if it was a folder permissions issue so I set them to 777 as a test but the script still does not save a copy of the file.
Overview is - user uploads an image from their computer. If the image is over a certain size the picture is resized and displayed. Rather than have to resize the picture each time it is viewed, I figured it would be better to save the smaller picture at the time of uploading. So far my attempts to save the file have failed.

Keith
 
Hi

Interesting. Let us try some alternatives together with some debugging.
Code:
[url=http://php.net/ini_set/]ini_set[/url]('[url=http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors]display_errors[/url]','on');
[url=http://php.net/error_reporting/]error_reporting[/url](E_ALL);

[url=http://php.net/file_put_contents/]file_put_contents[/url]('zytr.txt','WORKS');

file_put_contents('zytr.jpg',imagejpeg($thumb,null,100));
Tell us what happens.

Feherke.
 
After further testing I have found it to be a permissions issue after all. I was also trying to over write an existing file which the script still had an open file handle for. I will have to upload the file to a different directory, process it and then move it to the working directory.

Thanks for your help.

Keith
 
Hi, just use the:
Code:
if (is_file($file)) {
  // code here to output the file (header)
  }
else {
  // code here for creating the thumb
  }

Also, if you have an upload form, I usually make a function that loops while is_file($filename), so a user cant overwrite another users pictures.
Inside the loop, I append md5(time()) to the file, like so:
Code:
while(is_file($file)) {
  $file = md5(time()) . $file;
  }
// after the loop, you put the code for moving the file from its TMP location, to the new file location and the new, unique filename.

I store the files in file-structure, but I store title and such in mysql. Then I use htaccess to retrieve and put the file in the header, for the browser to peak at it :-)

This means I can optimize all my images as I wish, with a "fake" filename on the fly, I can also on the fly watermark, etc. I always use the if_is_file a lot, so I dont regenerate thumbs and such, to minimize unwanted stress on the cpu/ram (shared hosting).

Olav Alexander Mjelde
 
remember to also check that it in deed is a picture, checking the extension alone, is not enough.

I usually get info like pixels in width, if that function fails, I would chmod the uploaded file to 0400 or something, or maybe just unlink() it at once.

Olav Alexander Mjelde
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top