Jump to content

Setting a max file size on upload?


Solarpitch

Recommended Posts

Hey Guys,

 

I am currently using the below script on my site. It works fine, the only propblem I have is that I havent set anything to catch a resize massive images. I want the script to catch images larger than 800px and resize them to something a little bit smaller.

 

Just wondering how I could add it to this script.

 

This script will take the current image and copy a thumbnail which will then be sent to the server. So how could I use it to catch large images and resize? I can use the resize() function and specify the height and width I want, I am just not sure how to do the rest.

 


$image1 = $_FILES['image1']['tmp_name'];

if($image1 != ""){$image1exists = 1; }
if($image1exists == 1){$image1file = $folder.$timest."AA".basename($_FILES['image1']['name']);}
if($image1exists == 1){$image1 = $folder.$timest."AA".basename($_FILES['image1']['name']);}

if($_FILES['image1']['tmp_name'] != ""){

			list($width, $height, $type, $attr) = getimagesize($_FILES['image1']['tmp_name']);

			move_uploaded_file($_FILES['image1']['tmp_name'], $image1file);

			$copy1 = $image1;

	        $destination_width    =    160;
   			$destination_height    =    110;

    			$imagecop1    =    new hft_image($copy1);
    			$sz=getimagesize($copy1);

    			$imagecop1->resize($destination_width,  $destination_height,  '-');
    			$image1thumb = $folder.$timest."AA"."THUMB.jpg";
    			$imagecop1->output_resized($image1thumb,  "JPEG");
}



Link to comment
Share on other sites

Here is some script that I wrote in the past for this very function.  You will have to take the time to go through it and change it to your variable names...

 

<?php

//check the size of the file if need be shrink it down
$size = getimagesize($_FILES['image']['tmp_name']); // Get the image dimensions and mime type
$width = $size[0]; // Width of the image
$height = $size[1]; // Width of the image

if(($width > $height) && ($width > 480))
{
$height = (($height/$width) * 480);
$width = 480;
$resize = "true";
}

else if (($height > $width) && ($height > 360)) 
{
$width = (($width/$height) * 360);
$height = 360;
$resize = "true";
}

if ($resize == "true")
{
thumb($uploadFile, $height, $width, $uploadFile);
}
?>

 

Then I had a function called "thumb" that you can either place on the same page or use in like an includes page....

 

<?php
function thumb($source, $height, $width, $new_source) 
{ 
/* Check for the image's exisitance */ 
if (!file_exists($source)) 
{ 
	echo 'File does not exist!'; 
}

else
{ 
	$size = getimagesize($source); // Get the image dimensions and mime type
	$w = $width;
	$h = $height;
	$quality = "100";
	$resize = imagecreatetruecolor($w, $h); // Create a blank image

	/* Check quality option. If quality is greater than 100, return error */ 
	if ($quality > 100) 
	{
		echo 'The maximum quality is 100. Quality changes only affect JPEG images.';
	}

	else
	{
		//header('Content-Type: '.$size['mime']); // Set the mime type for the image

		switch ($size['mime'])
		{
			case 'image/jpeg':
			$im = imagecreatefromjpeg($source);
			imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original JPEG
			imagejpeg($resize, $new_source, $quality); // Output the new JPEG
			break;

			case 'image/png':
			$im = imagecreatefrompng($source);
			imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original PNG
			imagepng($resize, $new_source, $quality); // Output the new PNG
			break;
		}

		imagedestroy($im); 
	} 
} 
}

?>

 

Hopefully that helps you out.

Link to comment
Share on other sites

Hey again guys,

 

I seem to be having trouble with memory when trying to run this piece of code. I have marked where the code fails and I seem to get the message

 

Allowed memory size of 33554432 bytes exhausted (tried to allocate 7584 bytes) in admin/thumbnail.inc.php on line 134

 

Is there anyway I can free up memory while running the script?

 


if($_FILES['image1']['tmp_name'] != ""){

			list($width, $height, $type, $attr) = getimagesize($_FILES['image1']['tmp_name']);
			move_uploaded_file($_FILES['image1']['tmp_name'], $image1file);

			$copy1 = $image1;

			if(($width > $height) && ($width > 800))
			{
				$height = (($height/$width) * 800);
				$width = 800;
				$resize = "true";
			}
			else if (($height > $width) && ($height > 800)) 
			{
				$width = (($width/$height) * 800);
				$height = 800;
				$resize = "true";
			}

			if ($resize == "true")
			{

				$destination_width1    =    760;
   			   	    $destination_height1   =    500;

				$newcopy    =    new hft_image($copy1);
				$size=getimagesize($image1);

				$newcopy->resize($destination_width1,  $destination_height1,  '-');

				$newsize = $folder.$timest."NEWSIZE"."THUMB.jpg";
				$newcopy->output_resized($newsize,  "JPEG");


			}




			$destination_width2    =    160;
   			    $destination_height2    =    110;
    			$imagecop1    =    new hft_image($copy1);
    			$sz=getimagesize($copy1);
    			$imagecop1->resize($destination_width2,  $destination_height2,  '-');
    			$image1thumb = $folder.$timest."AA"."THUMB.jpg";
    			$imagecop1->output_resized($image1thumb,  "JPEG");




	        $destination_width3    =    235;
   			$destination_height3    =    170;
    			$imagecop2    =    new hft_image($copy1); ###### CODE FAILS HERE ######
    			$sz=getimagesize($copy1);
    			$imagecop2->resize($destination_width3,  $destination_height3,  '-');
    			$mainthumb = $folder.$timest."MAIN"."THUMB.jpg";
    			$imagecop2->output_resized($mainthumb,  "JPEG");

    			
		}	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.