Forum Moderators: coopster
ORIG CODE SNIPPET
function graphics_jpeg_resize($w,$h) {
$file = $_FILES['imgfile']['tmp_name'];
$tmpfile = "/tmp/" . rand(1000,9999);
system("djpeg $file >$tmpfile");
system("pnmscale -xysize $w $h $tmpfile ¦ cjpeg -smoo 5 -qual 75 >$file");
}
MY CODE
$w = 400;
$h = 300;
$tmpfile = "/tmp/" . rand(1000,9999);
system("djpeg $temp_file >$tmpfile");
system("pnmscale -xysize $w $h $tmpfile ¦ cjpeg -smoo 5 -qual 75 >$temp_jpg");
Where $temp_file is uploaded from a form:
$temp_file = $_FILES['uploadedfile1']['tmp_name'];
However when I try and run the script I get a number of errors which look to be based around $temp_jpg not being recognised as a jpg:
Am I missing something?
Can anyone help?
Thanks in advance
Alex
Can you please give us the errors that you are receiving.
I would take a look at the GD image functions [php.net]. I highly recommend them for basic image work like conversion and resizing [php.net].
Works like a charm.
[edited by: eelixduppy at 4:41 pm (utc) on Nov. 4, 2007]
[edit reason] changed link [/edit]
Warning: Division by zero in /homepages/34/d74824410/htdocs/Site3/Galleries/Gallery-IMAGE-Uploader.php on line 124
Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /homepages/34/d74824410/htdocs/Site3/Galleries/Gallery-IMAGE-Uploader.php on line 125
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /homepages/34/d74824410/htdocs/Site3/Galleries/Gallery-IMAGE-Uploader.php on line 127
Extracts from code
Line 124
$newwidth=($width/$height)*300;
$width & $height are generated from Line 121
list($width,$height)=getimagesize($temp_jpg);
Where (
$temp_jpg is output from the original code extract
Any thoughts or do you need more?
Thanks
Alex
My script also uses the following commands
imagecreatefromjpeg
imagecreatetruecolor
imagecopyresampled
imagejpeg
But I’m trying to reduce the size of the file they work on as if I upload an image > 500KB , the script times out due to the resource limits imposed by my hosting company
Thanks
Alex
I’m probably going to sound like a dummy here, but I’ve heard of php classes, but what exactly are they and how do I “install” / use them?
Is the class likely to use the same scripts that are tiimng out for me currently, namely:
Imagecreatefromjpeg, imagecreatetruecolor, imagecopyresampled & imagejpeg
Thanks
Alex
for some reason the original definition of $temp_jpg was failing. I've corrected this and the error page begins as follows (starting with $temp_jpg):
GalleryIMAGES/TEMP_Upload.jpg
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /homepages/34/d74824410/htdocs/Site3/Galleries/Gallery-IMAGE-Uploader.php on line 126
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'GalleryIMAGES/TEMP_Upload.jpg' is not a valid JPEG file in /homepages/34/d74824410/htdocs/Site3/Galleries/Gallery-IMAGE-Uploader.php on line 126
Line 126 i as follows:
$src = imagecreatefromjpeg($temp_jpg);
So it looks like the file I am creating is not the right format?
Note if I retrieve $temp_jpg (TEMP_Upload.jpg) from the website, Windows explorer sees it as a jpeg but its 0kb ad it can't be previewed
Any thoughts?
Thanks
Alex
Paths are not one of my strong points, do you mean I need to add the full domain i.e change
GalleryIMAGES/TEMP_Upload.jpg
to
http://www.example.co.uk/Galleries/ GalleryIMAGES/TEMP_Upload.jpg
I tried this and got the following (as the first of many errors)
Warning: imagecreatefromjpeg(http://www.site3.justtesting.co.uk/Galleries/GalleryIMAGES/TEMP_Upload.jpg) [function.imagecreatefromjpeg]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /homepages/34/d74824410/htdocs/Site3/Galleries/Gallery-IMAGE-Uploader.php on line 126
I’ve obviously got something wrong but I’m not sure what. So I did a little digging and tried
/homepages/34/d74824410/Galleries/GalleryIMAGES/TEMP_Upload.jpg
“/homepages/34/d74824410/” is quoted as my domain root in my hosting control panel but got the following
Warning: imagecreatefromjpeg(/homepages/34/d74824410/Galleries/GalleryIMAGES/TEMP_Upload.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /homepages/34/d74824410/htdocs/Site3/Galleries/Gallery-IMAGE-Uploader.php on line 129
Any thoughts,
Thanks
Alex
[edited by: jatar_k at 3:57 pm (utc) on Nov. 5, 2007]
[edit reason] examplified [/edit]
I’m not quite sure what you mean.
Currently I upload the image to $temp_file
$temp_file = $_FILES['uploadedfile1']['tmp_name'];
Decompress it to $tmpfile (Created by $tmpfile = "/tmp/" . rand(1000,9999);)
system("djpeg $temp_file >$tmpfile");
Resize it to $temp_jpg
system("pnmscale -xysize $w $h $tmpfile ¦ cjpeg -smoo 5 -qual 75 >$temp_jpg");
$temp_jpg is defined by the line:
$temp_jpg = "/homepages/34/d74824410/Galleries/GalleryIMAGES/TEMP_Upload.jpg";
And then carry out the image processing as before in the hope the resized image will not crash the script (timeout)
$src = imagecreatefromjpeg($temp_jpg);
list($width,$height)=getimagesize($temp_jpg);
$newheight=300;
$newwidth=($width/$height)*300;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($tmp,$target_L,75);
imagedestroy($tmp);
Looking at the code I’m now a little confused
I create both $tmpfile & $temp_jpg before using them but I don’t create $temp_file before uploading the image to it.
That seems wrong but $temp_file works in the raw from of my script (no pre-processing of the image using the system commands)
Help!
Thanks
Alex