upload and resize

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scottwater
Forum Newbie
Posts: 18
Joined: Tue Nov 11, 2008 9:07 pm
Location: Richmond, VA

upload and resize

Post by scottwater »

I want to create a function to upload and resize image files. I found this on a website, but it crashes. I have years of database programming but am very new on internet programming. Thanks for help - scott

First file

<form action="uploadpic.php" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="userfile" size=40>
<input type="submit" name="Submit" value="Submit">
</form>

second file uploadpic.php

<?php require_once('Connections/SQLConnect.php');

if (@is_uploaded_file($_FILES["userfile"]["tmp_name"]))
{
$img_name = resizePic("player","userfile","../pics/",150,150,0,"destination_name.jpg");
}

function uploadPic // (this is where my error log says I have a sytax error)($prefix,$formSrc,$dDir,$maxWidth,$maxHeight,$resizeFlag,$tempName) ;

if (!copy($_FILES[$formSrc]["tmp_name"],$dDir.$_FILES[$formSrc]["name"])) {
print ("failed to copy userfile ...\n");
}

if ($tempName == "") {
$tempName = $prefix."_".mt_rand().".jpg";
}

//hard copy of picture
$src_img = $dDir.$_FILES[$formSrc]["name"];

//destination
$dest = $dDir.$tempName;

//get source dimentions
$src_dims = getImageSize($src_img);

//create appropriate temp image
switch ($src_dims[2]) {
case 1: //GIF
break;

case 2: //JPEG
$srcImage = imageCreateFromJpeg($src_img);
break;

case 3: //PNG
break;

default:
return false;
break;
}

$srcRatio = $src_dims[0]/$src_dims[1]; // width/height ratio
$destRatio = $maxWidth/$maxHeight;

if ($destRatio > $srcRatio) {
$destSize[1] = $maxHeight;
$destSize[0] = $maxHeight*$srcRatio;
}
else {
$destSize[0] = $maxWidth;
$destSize[1] = $maxWidth/$srcRatio;
}

//if set image dimensions are required:
if ($resizeFlag == 1) {
$destSize[0] = $maxWidth;
$destSize[1] = $maxHeight;

}




$thumb_w = $destSize[0];
$thumb_h = $destSize[1];
$dst_img = imageCreateTrueColor($thumb_w,$thumb_h);
imageCopyResampled($dst_img,$srcImage,0,0,0,0,$thumb_w,$thumb_h,$src_dims[0],$src_dims[1]);

switch ($src_dims[2]) {
case 1:
break;
case 2:
imageJpeg($dst_img, $dest, 75); //75 denotes image quality / compression ratio
break;

case 3:
break;
}
//$y++;
unlink($src_img);

return $tempName;

?>
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Re: upload and resize

Post by SBro »

Your function (uploadPic) is not enclosed in braces. You need an opening { after you define the function and it's parameters, and a closing } after the return statement. Also you will need to rename the function (or the call to it) so that they are one and the same. Currently you are calling resizePic() but are defining uploadPic().
scottwater
Forum Newbie
Posts: 18
Joined: Tue Nov 11, 2008 9:07 pm
Location: Richmond, VA

Re: upload and resize

Post by scottwater »

Thanks sBro,

That worked but still crashes, says

PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 9152 bytes) in /hermes/bosweb/......61/ipw.t/public_html/uploadpic.php on line 39

case 2: //JPEG
$srcImage = imageCreateFromJpeg($src_img); // line 39
break;

Thanks for the help
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: upload and resize

Post by infolock »

sounds like you have a file size maximum set. you'll have to either reduce the max size file allowed in the form, or up the memory usage in php.ini
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: upload and resize

Post by mmj »

scottwater wrote:Thanks sBro,

That worked but still crashes, says

PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 9152 bytes) in /hermes/bosweb/......61/ipw.t/public_html/uploadpic.php on line 39

case 2: //JPEG
$srcImage = imageCreateFromJpeg($src_img); // line 39
break;

Thanks for the help
That is 16 mb, should be more then enough for your purposes, seems like you are storing something in the memory. Otherwise something is going on and you need to talk to your host.

Also why are you using copy for the uploaded file, you should be using move_uploaded_file.
scottwater
Forum Newbie
Posts: 18
Joined: Tue Nov 11, 2008 9:07 pm
Location: Richmond, VA

Re: upload and resize

Post by scottwater »

Tried to upload a small file..50kb it worked, but did not put it in the public_html/pics directory, it put it in public_html. Large files 500kb still have the problem.

phpinfo() shows:

max_execution_time 30
max_input_time 60
memory_limit 16M

What should I look for?

How do I edit php.ini?

again, Thanks for the help - scott
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: upload and resize

Post by infolock »

like i said earlier... up your memory limit..

change from 16 to 32..
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: upload and resize

Post by mmj »

infolock wrote:like i said earlier... up your memory limit..

change from 16 to 32..
16mb should be more then enough memory for php.

Just don't store large files in the memory, its not good practice either way.
scottwater
Forum Newbie
Posts: 18
Joined: Tue Nov 11, 2008 9:07 pm
Location: Richmond, VA

Re: upload and resize

Post by scottwater »

Thanks all,

I upped the memory limit and success. And learned alot about php.ini

again thanks -scott
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: upload and resize

Post by infolock »

scottwater wrote:Thanks all,

I upped the memory limit and success. And learned alot about php.ini

again thanks -scott
Glad it worked!
mmj wrote:
infolock wrote:like i said earlier... up your memory limit..

change from 16 to 32..
16mb should be more then enough memory for php.

Just don't store large files in the memory, its not good practice either way.
:offtopic:
Not to argue here, as topic is over and he has the solution, but this is not bad practice to up the memory limit from 16mb when the case calls for it. if he has to allow for filesizes larger than what a 16mb buffer can handle, then he should absolutely raise the memory limit. 16mb is the default, but it does not mean that it is dangerous to go above it or else it wouldn't be an option that one could change.

I'm not condoning going crazy and submitting php to 1000mb memory, but i am saying that if the situation absolutely demands it and you're server can handle it (just like it's going to be able to handle a 32mb php memory pretty easily, even if on a personal computer now adays), then go ahead.

but saying it's bad practice is quite off the mark.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: upload and resize

Post by mmj »

infolock wrote:Not to argue here, as topic is over and he has the solution, but this is not bad practice to up the memory limit from 16mb when the case calls for it. if he has to allow for filesizes larger than what a 16mb buffer can handle, then he should absolutely raise the memory limit. 16mb is the default, but it does not mean that it is dangerous to go above it or else it wouldn't be an option that one could change.

I'm not condoning going crazy and submitting php to 1000mb memory, but i am saying that if the situation absolutely demands it and you're server can handle it (just like it's going to be able to handle a 32mb php memory pretty easily, even if on a personal computer now adays), then go ahead.

but saying it's bad practice is quite off the mark.
If you are working with large files and you hit the mem limit it is not a good idea to just up it. Its bad practice and I've never seen any decent code which calls files which can be in the 10s of mb.

If you hit 16mb then you can almost just as easily hit 32mb.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: upload and resize

Post by infolock »

This will be my last post on the matter. If you want, we can continue this in PM.

It is not bad practice to handle large files. I've had clients who absolutely don't want to use FTP programs and want to be able to upload huge 15mb and 30 mb pdf's to their site, or 100mb movie files, but in this case, handling 20mb JPGS isn't uncommon either. But to say it's bad practice is going too far to configure the server to handle such a request. I have had the situation pop up twice in my career, and both times I did not hesitate to configure the memory limit. This is because at the end of the day, you either have an application that works, or you don't.

if we were talking about a server with 1gb ram, and the user is wanting to move the memory limit to 500mb, then yes I would agree that this could *potentially* be an issue. But the fact is that we're also talking about a server that has maybe 1 person active on it, 3 persons MAX. Otherwise, they wouldn't be using a 1gb server, they'd have a 20gb, or an array, or something crazy out there.

The point is, 1gb ram (or even 512mb ram for server) is very capable to handling reasonable changes to the php.ini file for memory, and 32mb, in my honest opinion, does not even constitute an argument here.

Is it bad practice to up the memory usage on PHP when an application is going past the 30mb memory buffer? Is it bad practice to up the time of execution for a file that is taking longer than 60 seconds to execute?

Both are debateable. If it's part of bad coding then I would agree it's a poor judgement call. But if it's on the rare occation of being because of the amount of data you're dealing with (in this case, a 16.5 mb file), then the need to up the maximum execution is absolutely needed.
Post Reply