Solved

can't get imagejpeg() to save file.

Posted on 2007-12-03
3
1,018 Views
Last Modified: 2012-06-27
I have a form on http://123fresno.com/crop that collects info where in a picture I want to start cropping and how big the new img should be.

<form name='cropform' action='http://123fresno.com/crop/crop.php' 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;'>
</form>

The form opens up a new php page with code:
<?php

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'>";
}
cropimage($_POST['src_x'],$_POST['src_y'],$_POST['src_w'],$_POST['src_h'],$_POST['imgaddress']);
      
?>

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.


0
Comment
Question by:kalleanka
3 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 125 total points
ID: 20401886
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,

ZOPPO
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 20401919
check the access permission to the folder ... give write permission to the folder / crop

more discussed about this at, http://in2.php.net/function.imagejpeg
0
 
LVL 1

Author Closing Comment

by:kalleanka
ID: 31412538
totally forgot about setting the folder to writable. thanks. I was tearing my hair :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I edit this Drupal page? 9 30
Time difference 10 35
PHP string issue 5 18
wordpress issue 2 22
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now