Jump to content

[SOLVED] Need help with image script and random number file name


vetman

Recommended Posts

I have this script to upload images into a database and view them. My problem is the thumbnail images don't use the original file name along with the thumb_ designation. It save the original filename with the full size image, but generates a random number to add to the thumbnail image designation. It works fine except I would like to use the original image name along with the thumb_ designation.

Any help would be appreciated.

Thanks in advance.

 

<?php
//Include connection files.
require_once("db_connection.php");
?>

<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<label for="title" id="title">Title</label><br />
<input type="text" name="title"/><br />
<label for="imageupload" id="imageupload">Browse Image</label><br />
<input type="file" name="imagefile"/><br />
<input type="submit" name="submit" value="Submit" /><br />
</form>

<?php
//If the form has been submitted
if(isset($_POST['submit'])){

//Validate the fields
if(strlen($_POST['title']) < 2 || empty($_FILES["imagefile"]) || $_FILES["imagefile"]["size"] > 1048576){
echo "You must enter a title longer than two characters and select an image to upload the image should be no larger than 1MB";
}else{

//Assign the title to a variable
$imgTitle = $_POST['title'];

//Assign the paths needed to variables for use in the script
$filePathFull = "assets/imgf/".$_FILES["imagefile"]["name"];
$filePathThumb = "assets/imgt/";

//Assign the files TMP name and TYPE to variables for use in the script
$tmpName = $_FILES["imagefile"]["tmp_name"];
$imageType = $_FILES["imagefile"]["type"];


//Use a switch statement to check the extension of the file type. If file type is not valid echo an error
switch ($imageType) {
case $imageType == "image/gif":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/jpeg":
move_uploaded_file($tmpName,$filePathFull); break;
case $imageType == "image/pjpeg":
move_uploaded_file($tmpName,$filePathFull); break;
case $imageType == "image/png":
move_uploaded_file($tmpName,$filePathFull); break;
case $imageType == "image/x-png":
move_uploaded_file($tmpName,$filePathFull); break;
default:
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}

// Get information about the image
list($srcWidth, $srcHeight, $type) = getimagesize($filePathFull);

//Original image ratio
$origRatio = $srcWidth/$srcHeight;

//Create the correct file type based on imagetype
switch($type) {
case IMAGETYPE_JPEG:
$startImage = imagecreatefromjpeg($filePathFull);
break;
case IMAGETYPE_PNG:
$startImage = imagecreatefrompng($filePathFull);
break;
case IMAGETYPE_GIF:
$startImage = imagecreatefromgif($filePathFull);
break;
default:
return false;
}

//Set target height and width.
$width = 200;
$height = 200;

//Calculate the height and width to keep the correct image ratio
if ($width/$height > $origRatio) {
$width = $height*$origRatio;
}else{
$height = $width/$origRatio;
}

//Create the thumbnail with true colours
$thumbImage = imagecreatetruecolor($width, $height);

//Generate the resized image
imagecopyresampled($thumbImage, $startImage, 0,0,0,0, $width, $height, $srcWidth, $srcHeight);

//Generate a random number to append the filename.
$ran = "thumb_".rand () ;
$thumb2 = $ran.".jpg";

//Create the path to store the thumbnail in.
$thumb_Add_thumb = $filePathThumb;
$thumb_Add_thumb .= $thumb2;

//Create the new image and put it into the thumbnails folder.
imagejpeg($thumbImage, "" .$filePathThumb. "$thumb2");

//Create sql query and insert the values into the database
$sql = "INSERT INTO imageUpload (title, thumbpath) VALUES ('$imgTitle', '$thumb_Add_thumb')";
$query = mysql_query($sql) or die(mysql_error());
}}
?>

Link to comment
Share on other sites

You can get rid of the random part and then replace this.

 

//Create the new image and put it into the thumbnails folder.

imagejpeg($thumbImage, "" .$filePathThumb. "$thumb2");

 

with

 

//Create the new image and put it into the thumbnails folder.

imagejpeg($thumbImage, "" .$filePathThumb. "$_FILES["imagefile"]["name"]);

 

essentially replacing $thumb2 which is your random thumb name with the actual name of the file.

Link to comment
Share on other sites

Thanks for the fast reply, I input your code, got this error message:

 

Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/r/i/g/rigtway/html/tuts/imageupload.php on line 103

Link to comment
Share on other sites

I'm not sure what happened, but it doesn't write what it should to the database.

It puts assets/imgt/ in the thumbpath but that's it, it doesn't fill in the fullpath file name either. It does put in the title in the right column. ???

Link to comment
Share on other sites

Sorry hard to troubleshoot without playing with the code in front of me..

 

Try echoing out variables to see what the results are yielding.

 

For example prior to the imagejpeg statement echo out $_FILES[imagefile][name] to see what value it holds

 

Do the same for $filePathThumb

 

If these both hold valid values then create a new var such as

 

$newthumb = $filePathThumb.$_FILES[imagefile][name];

 

then in the imagejpeg statement use

 

imagejpeg($thumbImage,$newthumb);

Link to comment
Share on other sites

switch ($imageType) {
case $imageType == "image/gif":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/jpeg":
move_uploaded_file($tmpName,$filePathFull); break;
case $imageType == "image/pjpeg":
move_uploaded_file($tmpName,$filePathFull); break;
case $imageType == "image/png":
move_uploaded_file($tmpName,$filePathFull); break;
case $imageType == "image/x-png":
move_uploaded_file($tmpName,$filePathFull); break;
default:
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}

 

 

switch ($imageType) {
case "image/gif":
move_uploaded_file($tmpName,$filePathFull);
break;
case "image/jpeg":
move_uploaded_file($tmpName,$filePathFull); break;
case "image/pjpeg":
move_uploaded_file($tmpName,$filePathFull); break;
case "image/png":
move_uploaded_file($tmpName,$filePathFull); break;
case "image/x-png":
move_uploaded_file($tmpName,$filePathFull); break;
default:
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}

 

Link to comment
Share on other sites

why not just put the filename on the end of hte thumbnail rather than a random number?!

 

i.e. get rid of

 

//Generate a random number to append the filename.

$ran = "thumb_".rand () ;

$thumb2 = $ran.".jpg";

 

and replace with

 

//append filename to thumb

$thumb2 = "thumb_".$_FILES["imagefile"]["name"];

 

 

...?

Link to comment
Share on other sites

you're not inserting the full path into the database...?

 

//Create sql query and insert the values into the database

$sql = "INSERT INTO imageUpload (title, thumbpath) VALUES ('$imgTitle', '$thumb_Add_thumb')";

 

you'd need to change to

 

//Create sql query and insert the values into the database

$sql = "INSERT INTO imageUpload (title, thumbpath, fullImageColumn) VALUES ('$imgTitle', '$thumb_Add_thumb', '$filePathFull')";

 

 

the fullImageColumn should be renamed to whatever the column is that stores your full image name...?

and $filePathFull contains the full file path to that image...

 

 

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.