Okay. I have followed several tutorials now on this topic and it is not clicking as to how I would apply these tutorial scripts to my code. I have 2 separate issues to address but cannot seem to figure out either so any help or advice would be greatly appreciated. I am basically wanting a user to be able to upload images and have it resize them. I need to give them the option of uploading up to six images, with one image being 500 X 250 px and the rest being up to 500 X 500 px.
This is my current (one image) upload script:
[php]<?php
include ‘core/init.php’;
protect_page();
header(“Location: wod_admin.php”);
function GetFileName($f)
{
/*
- this function will get the name of a file
- for Instance if the file is image14.jpg
- only image14 will be returned.
BY wilson382
*/
$h=substr($f,strrpos($f,"/"),strrpos($f,"."));
$FileName= substr($h,1,strrpos($h,".")-1);
return $FileName;
}
if(isset($_POST[‘submit’]))
{
if(is_uploaded_file($_FILES[‘myfile’][‘tmp_name’]))
{
$name = $_FILES[‘myfile’][‘name’];
$tmp_name = $_FILES[‘myfile’][‘tmp_name’];
$location = “images/wod/$name”;
move_uploaded_file($tmp_name,$location);
}
$_month = mysql_real_escape_string($_POST['month']);
$_day = mysql_real_escape_string($_POST['day']);
$_year = mysql_real_escape_string($_POST['year']);
$_name = mysql_real_escape_string($_POST['name']);
$_w1 = mysql_real_escape_string($_POST['w1']);
$_w2 = mysql_real_escape_string($_POST['w2']);
$_w3 = mysql_real_escape_string($_POST['w3']);
$_w4 = mysql_real_escape_string($_POST['w4']);
$_w5 = mysql_real_escape_string($_POST['w5']);
$_w6 = mysql_real_escape_string($_POST['w6']);
$_comment = mysql_real_escape_string($_POST['comment']);
// select all rows that are not the top 4
$sql = mysql_query(“SELECT * FROM wod
WHERE id
NOT IN (SELECT * FROM (SELECT id
FROM wod
ORDER BY id
DESC LIMIT 4) AS ids_to_keep
)”);
while($row = mysql_fetch_assoc($sql)) {
$del = mysql_query(sprintf(“DELETE FROM wod
WHERE id
= %d”, (int) $row[‘id’])); // delete current row
unlink($row[‘imagelocation’]); // remove image from current row
}
$sql="INSERT INTO `wod` SET `month`='$_month',
`day`='$_day',
`year`='$_year',
`name`='$_name',
`w1`='$_w1',
`w2`='$_w2',
`w3`='$_w3',
`w4`='$_w4',
`w5`='$_w5',
`w6`='$_w6',
`comment`='$_comment',
`imagelocation`='$location'";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
mysql_close($con);
}
?>[/php]
ad this is the tutorial script:
[php]<?php
// File Variables
$name= $_FILES['image']['name'];
$temp= $_FILES['image']['tmp_name'];
$type= $_FILES['image']['type'];
$size= $_FILES['image']['size'];
$size2= getimagesize( $temp );
$width= $size2[0];
$height= $size2[1];
$upload= md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) );
// Restrictions for uploading
$maxwidth= 4000;
$maxheight= 4000;
$allowed= array( 'image/jpeg', 'image/png', 'image/gif' );
// Recognizing the extension
switch( $type ){
// Image/Jpeg
case 'image/jpeg':
$ext= '.jpg';
break;
// Image/png
case 'image/png':
$ext= '.png';
break;
// Image/gif
case 'image/gif':
$ext= '.gif';
break;
}
// Upload Variables
$path= 'uploads/' . $upload . $ext;
$thumb_path= 'uploads/thumbs/thumb_' . $upload . $ext;
// Check for the Image post.
if( $_POST ){
// Got into the POST check.
if( $_FILES ){
// Checking if the extension is allowed.
if( in_array( $type, $allowed ) ){
// Checking if the resolution is FULLHD or under this resolution.
if( $width <= $maxwidth && $height <= $maxheight ){
// Checking if the image is 5MB or less.
if( $size <= 5242880 ){
// Check the shape of the image.
if( $width == $height ){ $shape=1; }
if( $width < $height ){ $shape=2; }
if( $width > $height ){ $shape=3; }
// Ajusting the resize script on shape.
switch( $shape ){
// Code to resize a square image.
case 1:
$newwidth= 100;
$newheight= 100;
break;
// Code to resize a tall image.
case 2:
$newwidth= 100;
$ratio= $newwidth / $width;
$newheight= round( $height * $ratio );
break;
// Code to resize a wide image.
case 3:
$newheight= 100;
$ratio= $newheight / $height;
$newwidth= round( $width * $ratio );
break;
}
// Resizing according to extension.
switch( $type ){
// Image/Jpeg
case 'image/jpeg':
$img= imagecreatefromjpeg( $temp );
$thumb= imagecreatetruecolor( $newwidth, $newheight );
imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
imagejpeg( $thumb, $thumb_path );
break;
// Image/png
case 'image/png':
$img= imagecreatefrompng( $temp );
$thumb= imagecreatetruecolor( $newwidth, $newheight );
imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
imagepng( $thumb, $thumb_path );
break;
// Image/gif
case 'image/gif':
$img= imagecreatefromgif( $temp );
$thumb= imagecreatetruecolor( $newwidth, $newheight );
imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
imagegif( $thumb, $thumb_path );
break;
}
// Move the original file aswell.
move_uploaded_file( $temp, $path );
// Putting out the data.
header('Location: index.php');
}else{ die( "Your picture did not pass the size restrictions filter." ); }
}else{ die( "Your picture did not pass the resolution restrictions filter." ); }
}else{ die( "Your picture did not pass the extension restrictions filter." ); }
}
}
?>[/php]
I am failing to see how to apply the tutorial script to my current script. The current script takes the html upload form (named ‘myfile’) and sends its file folder path to a row named ‘imagelocation’ in a database.