Forum Moderators: coopster

Message Too Old, No Replies

Uploading & resizing images

uploading images in PHP and using commans such as

         

alexpetrie

11:05 pm on Oct 30, 2007 (gmt 0)

10+ Year Member



I found the following cose code in a previous post and tried to use it as follows

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

eelixduppy

9:13 pm on Nov 2, 2007 (gmt 0)



Hello, and Welcome to WebmasterWorld!

Can you please give us the errors that you are receiving.

whoisgregg

10:24 pm on Nov 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com], alexpetrie!

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].

FourDegreez

3:55 pm on Nov 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey there, I recently needed to do image manipulation and found this wonderful script: [phpclasses.org ]

Works like a charm.

[edited by: eelixduppy at 4:41 pm (utc) on Nov. 4, 2007]
[edit reason] changed link [/edit]

alexpetrie

10:41 am on Nov 5, 2007 (gmt 0)

10+ Year Member



Hi Eelixduppy
Here’s the first three lines of quite a few errors, which I believe come from the fcta my output file is not “correct”

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

alexpetrie

10:46 am on Nov 5, 2007 (gmt 0)

10+ Year Member



Hi whoisgregg

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

Habtom

10:48 am on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It seems $temp_jpg is not holding the correct image path.

echo $temp_jpg and see the result you get, it might give you some hint.

alexpetrie

10:48 am on Nov 5, 2007 (gmt 0)

10+ Year Member



Hi FourDegreez

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

alexpetrie

12:44 pm on Nov 5, 2007 (gmt 0)

10+ Year Member



Hi Habtom

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

whoisgregg

2:02 pm on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$temp_jpg should contain a full path to the file. If it only contains GalleryIMAGES/TEMP_Upload.jpg, then it might be looking for it in the wrong place.

phranque

2:43 pm on Nov 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, alex!

if your uploads are limited by your host to 500KB then you will not be able to resize the image because it will fail to upload to the temp directory.

alexpetrie

3:10 pm on Nov 5, 2007 (gmt 0)

10+ Year Member



Hi Phranque,

thanks for you feedback, but I can upload ok, the problem occurs when I "process" it, and it just happens to start failing on files > 500KB (approx)

Thanks
Alex

alexpetrie

3:23 pm on Nov 5, 2007 (gmt 0)

10+ Year Member



Hi whoisgregg.

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]

phranque

12:31 am on Nov 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you could try something like this:
$tmpfile = "/tmp/" . rand(1000,9999);
move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $tmpfile);

and then start processing $tmpfile...

alexpetrie

10:20 am on Nov 6, 2007 (gmt 0)

10+ Year Member



Hi phranque,

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