can't get imagejpeg() to save file.

I have a form on that collects info where in a picture I want to start cropping and how big the new img should be.

<form name='cropform' action='' method='POST' target='_blank'>
<input type='hidden' name='imgaddress' value='bigblack.jpg'>
src_x<input type='text' class='xy' name='src_x'> src_y<input type='text' class='xy' name='src_y'><br>
src_w<input type='text' class='xy' name='src_w'> src_h<input type='text' class='xy' name='src_h'><br>
<input type='submit' value='Crop'><input type='reset' value='Clear' onclick='Mclick=3;'>

The form opens up a new php page with code:

function cropimage($src_x,$src_y,$src_w,$src_h,$imgaddress){
      $documentroot = $_SERVER['DOCUMENT_ROOT'];
      $dst_im = imagecreatetruecolor ( $src_w, $src_h );
      $src_im = imagecreatefromjpeg($documentroot."/crop/".$imgaddress);
      $dst_x = 0; // 0px to the left on $dst_im
      $dst_y = 0; // 0px to the top on $dst_im
      imagecopy ( $dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h );
      $newimg = "temp.jpg";
      imagejpeg ( $dst_im, $documentroot."/crop/".$newimg );
      echo "<img src='$newimg'>";

But every time I get the error message:
Warning: imagejpeg() [function.imagejpeg]: Unable to open '/home/fresocom/public_html/crop/temp.jpg' for writing in /home/fresocom/public_html/crop/crop.php on line 11

It does not want to save the image to temp.jpg using the function imagejpeg($dst_im,$documentroot."/crop/".$newimg ); and I cant understand what I am missing.

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

ZoppoConnect With a Mentor Commented:
Hi kalleanka,

maybe you have no write access in '/home/fresocom/public_html/crop/'? To ocheck this you could try to first open that file using 'fopen' with a "w" param ...

BTW, I'm not sure if it's a good idea to hardcode the filename 'temp.jpg' - if i.e. two or more users use that code the 'temp.jpg' for the first user maybe overwritten by the script running for the next user.

Maybe you should call 'tempnam()' to create a unique, writable temp file and retrieve its name - then you can write the JPG into this file.

Hope that helps,

Loganathan NatarajanLAMP DeveloperCommented:
check the access permission to the folder ... give write permission to the folder / crop

more discussed about this at,
kalleankaAuthor Commented:
totally forgot about setting the folder to writable. thanks. I was tearing my hair :)
All Courses

From novice to tech pro — start learning today.