Log in

View Full Version : uploading pictures and time limits



captainjustin
08-04-2010, 11:45 PM
I using a script that I found online that lets you resize and upload pictures. The script works fine but, large pictures will not work. The script will stop running after about a minute and the page will reload blank.... I thought it might be a time limit setting so I tried this
<?php set_time_limit(0);?>[CODE].. That didn't fix my problem. I use yahoo webhosting and I can't access my php.ini file because the good folks at yahoo don't allow access to it. I have tried about 20 different upload scripts trying to get this to work, but I seem to get the same result... I'll add the script below, but I'm not sure if thats the problem. Please keep in mind I'm very new to php and any help would be greatly appreciated..
[code]

<?php





ob_start();

function calculate_width_height($width, $height,$width_needed, $height_needed)
{
if ($width <= $width_needed and $height <= $height_needed)
{
//Estimate if both Width and Height of the image are smaller then
//the given values for Width and Height
//if this is true dan keep the original size of the image
$l_new_width=$width;
$l_new_height=$height;
}
else
{
if ($height > $height_needed)
{
//If the value of the height of the image is greater then the given value for height
//then estimate the percentage of the given value for height in relation to
//the original height value of the image
//Use this percentage to calculate the new width value for the image
//in relation to original width value of the image

$l_percentage_height =$height_needed/($height/100);
$l_new_width=($width/100)*$l_percentage_height;
$l_new_height=$height_needed;

if ($l_new_width > $width_needed)
{
//After calculation the new width value can stil be greater then the needed width value
//do the same calculation as above but then for the width needed

$l_percentage_width =$width_needed/($width/100);
$l_new_height=($height/100)*$l_percentage_width;
$l_new_width=$width_needed;
}
}
else
{
//The height value of the image is not greater then the needed height value
//but original width value of the image is
//calculate the new values for width and height

$l_percentage_width =$width_needed/($width/100);
$l_new_height=($height/100)*$l_percentage_width;
$l_new_width=$width_needed;

if ($l_new_height > $height_needed)
{
$l_percentage_height =$height_needed/($height/100);
$l_new_width=($width/100)*$l_percentage_height;
$l_new_height=$height_needed;
}
}


}
$calculated_width_height[] = round($l_new_width);
$calculated_width_height[] = round($l_new_height);
return $calculated_width_height;
}


function resize($filename, $dest, $width, $height, $type=''){
if(@imagecreatefromgif($filename)){
$img=imagecreatefromgif($filename);
$type_r='gif';
}
elseif(@imagecreatefromjpeg($filename)){
$img=imagecreatefromjpeg($filename);
$type_r='jpg';
}
elseif(@imagecreatefrompng($filename)){
$img=imagecreatefrompng($filename);
$type_r='png';
}
elseif(@imagecreatefromwbmp($filename)){
$img=imagecreatefromwbmp($filename);
$type_r='bmp';
}
else{
return 'Cannot open file';
}
$type=($type=='')?$type_r:'';
$img_n= @ imagecreatetruecolor ($width, $height);
@ imagecopyresampled($img_n, $img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img));
if($type=='gif'){
imagegif($img_n, $dest);
}
elseif($type=='jpg'){
@ imagejpeg($img_n, $dest);
}
elseif($type=='png'){
@ imagepng($img_n, $dest);
}
elseif($type=='bmp'){
@ imagewbmp($img_n, $dest);
}
Return true;
}



function upload_file($root_dir,$file_name,$file_name_tmp,$lc_width,$lc_height)
{

$lc_max_filesize=2001000;
$filesize=filesize($file_name_tmp);
$sub_server=substr($_SERVER["HTTP_REFERER"],7,9);
$uploaddir = "images";

if (
!file_exists($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) ||
2 != (fileperms($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) & 2) ||
1 != (fileperms($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) & 1)
)
{
$error="tets";
}

$fileext = substr($file_name, -4);

if (strtolower($fileext) != ".jpg" and strtolower($fileext) != ".gif")
{
print "<b>For security reasons only the uploading of .jpg and .gif files is allowed.</b><br>\n";
print "<a href=\"javascript:history.back();\">Click here to go back.</a><br>";
exit;
};

$loc = $uploaddir."/".$file_name;
$fullloc = $root_dir.$loc;
$upfile = $file_name_tmp;

if ($filesize>$lc_max_filesize)
{
$lc_message= "File is too big, may not be bigger then 2MB";
}
else
{
if(move_uploaded_file($upfile, $fullloc))
{
chmod($fullloc, 0644);
}
else
{
die("Error moving \"$upfile\" to \"$fullloc\".");
}

$test =filesize($fullloc);
}


list($original_width, $original_height, $type, $attr) = getimagesize($fullloc);
list($l_new_width,$l_new_heigth) = calculate_width_height($original_width, $original_height,$lc_width,$lc_height);

resize($fullloc, $root_dir.$uploaddir."/".$lc_width."_".$lc_height."_".$file_name, $l_new_width, $l_new_heigth);

$upload_info[] = $lc_message;
$upload_info[] = $original_width;
$upload_info[] = $original_height;
$upload_info[] = $l_new_width;
$upload_info[] = $l_new_heigth;

return $upload_info;

}



?>
<body>
<?php
$root_dir =$HTTP_SERVER_VARS["DOCUMENT_ROOT"];

if ($_POST["resize_submit"] == "resize")
{

$lc_height =$_POST["height"];
$lc_width =$_POST["width"];

$day = getdate();
$minutes = $day['minutes'];
$temp=gettimeofday();
$sec=(int)$temp["sec"];

if (!empty($_FILES["userfile"]["name"]))
{
$fileext = substr($_FILES["userfile"]["name"], -4);

$ffile_name="1_".$minutes."_".$sec.$fileext;

$tfile_name_tmp=$_FILES["userfile"]["tmp_name"];

list($lc_upload_message,$original_width,$original_height,$l_new_width,$l_new_height) = upload_file($root_dir,$ffile_name,$tfile_name_tmp,$lc_width,$lc_height);

if ($lc_upload_message!="")
{
echo "error_message".$lc_upload_message;
}
}

?>
<form ENCTYPE="multipart/form-data" action="resize_image.php" method="post">
<table>
<tr><td>height :</td><td><input type="TEXT" name="height" size="10" value="<?php echo $lc_height ; ?>"></td></tr>
<tr><td>width :</td><td><input type="TEXT" name="width" size="10" value="<?php echo $lc_width ; ?>"></td></tr>
<tr><td>image to upload :</td><td><INPUT NAME="userfile" TYPE="file"></td></tr>
<tr><td><input type="submit" name="resize_submit" value="resize"></td>
</form>
</table>

<table>
<tr>
<td><IMG border=0 src="images/<?php echo $ffile_name ; ?>" ></td>
<td valign=bottom><IMG border=0 src="uploads/<?php echo $lc_width.'_'.$lc_height.'_'.$ffile_name ; ?>" ></td>
</tr>
<tr>
<td><?php echo "org_height:".$original_height." org_width:".$original_width; ?></td>
<td><?php echo "new_height_".$l_new_height." new_width_".$l_new_width; ?></td>
</tr>
</table>

<?php


}
else
{

?>
<form ENCTYPE="multipart/form-data" action="resize_image.php" method="post">
<table>
<tr><td>height :</td><td><input type="TEXT" name="height" size="10" ></td></tr>
<tr><td>width :</td><td><input type="TEXT" name="width" size="10" ></td></tr>
<tr><td>image to upload :</td><td><INPUT NAME="userfile" TYPE="file"></td></tr>
<tr><td><input type="submit" name="resize_submit" value="resize"></td>
</form>
</table>
<?php
}
?>
</body>
</html>
<?php
ob_end_flush();
?>

djr33
08-05-2010, 01:18 AM
There are a few ways to access php "ini" settings. One is in the .ini file, another is through ini_set() (http://php.net/manual/en/function.ini-set.php) and the third is to approach it like you have been using specific functions to change settings. Those go in order of "localness" so that a setting in php.ini is general and something in the script is more local and less powerful, but also should override any more general setting.

Another way that might work is setting an "ini" value in the .htaccess file. I don't have a link for this at the moment, but you can probably find something by searching.


What error message do you see, or do you just see a blank page? If you enable all errors (ironically this also requires php.ini, but you can use the other methods above too), then you should be able to figure it out.

Some hosts may simply block longer processing times. That is, some configurations will not allow you to override the global setting locally so without access to php.ini, you can't do anything.


The other issue with this may not be the time required for processing but instead the filesize since that is also something you can define in php.ini, so you may need to change that value as well.

It's hard to give more specific information since this is probably something you can't even see (since it's happening on the server).

And of course be careful this isn't against their TOS, though it probably isn't, at least if you're not over-using this feature.


I suggest, since this is likely a problem with the host, searching for similar questions related to yahoo hosting. Someone else has probably had this same problem and may have found a solution.