Image resize help (newb...)

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
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Image resize help (newb...)

Post by MiniMonty »

Hi all,

I'm using the script below to upload and rename images in numerical order
and it works fine. Now I need to add in a resize function to restrict the uploaded
images to a max of 847x543 pixels.

Can anyone suggest where in the script I should put the resize function and where
I might find a good one ? !!

Best wishes
Monty

Code: Select all

 
<?php
define ("MAX_SIZE","100");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['Submit']))
{
$image=$_FILES['image']['name'];
if ($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
$groovy = sizeof(glob("/Sites/com/shutterbugclub/www/images/uploads/*"));
$groovy = ++$groovy;
print $groovy;
$image_name=$groovy.'.'.$extension;
$newname="uploads/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully!</h1>";
}
// Now make an array of the contents of the directory "uploads"
$array = glob('uploads/*');
// write the .txt file with the new array
$myFile = "gallarray.txt";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = "arse=images/".implode(",images/",$array);
fwrite($fh, $stringData);
fclose($fh);
// APPEND the .txt fil with the total image number
$myFile = "gallarray.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = "&totalimgs=".$groovy;
fwrite($fh, $stringData);
fclose($fh);
?>
 
Post Reply