Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Plz Help: Image Resize, when inputing into DBA 1

Status
Not open for further replies.

i3iz

Technical User
Jan 3, 2005
30
0
0
US
I want to just put image locations into the Database. here are the fields of my table.

id
name
img_location
thumb_location
orientation

I know how to send those to such places, but i really dont get how to work with files really. I want to resize any file down to <250kb and also make a thumb of the image. How would i do that? right now, if the file is over 500KB i just reject it, but i am sure i could manipulate it down to 250k or atleast change the dimensions and Qual. any ideas? Someone please help me, i am puting this database together for my fiance's parents and am not charging them, but i want them to be able to interact with it via HTML, because they are not very tech savvy.



//HERE IS MY CODE
<?
// CONFIG
$serverpath = "/home/localhost/public_html/new/gallery"; // Path to where images should be uploaded to on the server.
$urltoimages = " // Web address to where the images are accessible from.
$maxsize = "500000"; // Example - 20000 is the same as 20kb
// CONFIG END

$mode = $_GET['mode'];
if ($mode == "") { $mode = "form"; }

if ($mode == "form") {
echo "<form enctype='multipart/form-data' method='post' action='?mode=upload'>\n";
echo "<input type='file' name='file'><br>";
echo "<input name='img_name' type='text' id='img_name' value='title your image'><br>";
echo "<input name='orientation' type='radio' value='ls'>Horizontal<br>";
echo "<input name='orientation' type='radio' value='pt'>Vertical<br>";
echo "<input type='submit' name='Submit' value='Go'>\n";
}


if ($mode == "upload") {

$file = $_FILES['file']['name'];
// If you add your own file types don't forget to add an uppercase version.
$allowedfiles[] = "gif";
$allowedfiles[] = "jpg";
$allowedfiles[] = "jpeg";
$allowedfiles[] = "png";
$allowedfiles[] = "GIF";
$allowedfiles[] = "JPG";
$allowedfiles[] = "JPEG";
$allowedfiles[] = "PNG";

if($_FILES['file']['size'] > $maxsize)
{
print "File size is too big - please reduce file size and try again.";
}
else {
$path = "$serverpath/$file";
$path2 = "$urltoimages/$file";
foreach($allowedfiles as $allowedfile) {

if ($done <> "yes") {
if (file_exists($path)) {
echo "A file with this name already exists - please rename the file and reupload.";
exit;
}
}

if (substr($file, -3) == $allowedfile) {
move_uploaded_file($_FILES['file']['tmp_name'], "$path");
$done = "yes";
// Insert database
require "header.php";
db_connect();

$form_name = urldecode($_POST['img_name']);
$form_orientation = urldecode($_POST['orientation']);
$form_img_location = urldecode($path2);

$sql="INSERT INTO gallery (name, orientation, img_location)
VALUES ('$form_name', '$form_orientation','$form_img_location')";
$result = mysql_query($sql)
or die ('I cannot query database: ' . mysql_error());
// end database code
echo "<p>Your image has been successfully uploaded to our server and can be accessed using the URL provided below.</p>";
echo "<p><A href='$urltoimages/$file' target='_blank'><strong>$urltoimages/$file</strong></a></p>";
echo "<p><img src='$urltoimages/$file' border='0'>";
}

}

if ($done <> "yes") { print "<p><b>Error:</b> Your image as not been uploaded because it is not a recognised image file. Please try again.</p>"; }
}
}

?>
 
difficult to tell what question you are asking. can you resize images - yes! use the php image manipulation add-on libraries.

can you store the locations of uploaded images in a database = yes! search for "file uploads" within the php.net online documentation.
 
here is my question...

how do i do this?

 can you resize images - yes! use the php image manipulation add-on libraries. 

I want to split the upload into 2 files, a thumbnail and a resize to be within a certain parameter. Seeing someone elses code doing this would help. I just cant figure out the php image manipulation libraries... I am still new.

--i3iz
Technical Newbie
 

If you read a bit about those functions, I think that you know how to do it.

ps. read some of the examples too!
Remember to use imagecreatetruecolor(), or the images will look very bad!

I do this very simple:
On the upload page, I list all images uploaded by user.
I call my thumbs images TN_<whatever>, where <whatever> is the filename for the original images.

Like, if you where to upload cat.jpg, the thumb would be TN_cat.jpg

Then, when loading the upload index, I loop thru the files uploaded by user. I then do a:
if (!(is_file("TN_" . $row['fnam'])) {
if (is_file($row['fnam'])) {
createThumb($row['fnam']);
}
else {
echo "Original file missing!";
}
}
else {
echo "<img src=\"TN_$row['fnam']\" alt=\"{$row['title']}\" />";
}

ps. take this as psuedo-code!

Olav Alexander Mjelde
Admin & Webmaster
 
Sweet thanks...
so i have this code now... On my display page. Turns out i dont need a thumb, because i can let the server do the work...right?
This is a compramise between server storage and processor load.


Next question...What if i want the file to be maximum 150 in H or W, and the rest scaled to that? seems like i can do some type of math.

HOW DO I WRITE THIS IN THE CODE BELOW?
If H/w > 1
h= 150
w= (h/old H) * Old W
else if h/w < 1
w=150
h=(w/old W) * Old H
Else if h/w=1
w=150
h=150
Sorry i am a newbie...still learning I really appreciate your guys help...


HERE IS THE CODE I PLAN ON USING...
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreate($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>


i love tek-tips and phpbuddy!

--i3iz
Technical Newbie
 
<?php
// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 150;
$height = 150;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

if ($width && ($width_orig < $height_orig)) {
   $width = ($height / $height_orig) * $width_orig;
} else {
   $height = ($width / $width_orig) * $height_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>

ok so i have this... Got it thanks...

--i3iz
Technical Newbie
 
anyone know how to integrate my upload script to utilize the resized images?

--i3iz
Technical Newbie
 
still not sure what you mean! the resize script works, right?
is your query that you want to assign the filename dynamically from an uploaded image?

 
Do it like this:

Create a function for resizing images, let's say createThumb().

You then call createThumb(), if there is no thumb and there is an image.
Remember that you call the thumb TN_{$row['image']

createThumb("{$row['image']}", "TN_{$row['image']}", "150", "150");

Where the first parameter is the source and the second is the destination filename.

You can also combine your image-functions with a watermark script.

I made this script, so I can optimize the image-filenames, which I do via utilizing htaccess, so I first force an extensionless php file to be run as php, then I use "fake folders" and "fake filename" below that file.

I based my script on the searchfriendly urls tutorial on evolt.org

So, I then let the uploaded pictures be called timestamp() + random integer . jpg (forced). This is due to normal people not knowing which filenames are "good".

I guess I could do a replace and htmlentities, but now my users had uploaded ~530 pictures.
I did not want the work of renaming ~530 images to unique names, so google would index them.

I rather call them as this now:
<img src="php_file/b_filename-jpg/google_searchwords.jpg"...

IRL example:
if a file is called 12112.jpg, who the h*** searches google for 12112? They would rather search for what ever that image is.

so, on the upload page, I also let users input title and comments.

I then use:
$title = htmlentities(str_replace(" ", "_", $row['title']));
$comments= htmlentities(str_replace(" ", "_", $row['comments']));

echo "<img src=\"pictures/b_12112-jpg/{$title}\" alt=\"{$text}\" />";

ps. only take this as psuedocode!
If you are interested, I'm considering posting the script.
I think since I rewrote the evolt code, I owe it to the community, but if I am to release it, I have to clean up some commenting, comment some more, write some info, etc.

I guess I could just comment the new functions and reffer to the original script.

I cant wait untill google will index my 530 images.
Most likely, my PR (PageRank) will increase explosivly!

I just have to make some logick for the bilingual function for my custom CMS.

Good luck with your project!
I have to drive downtown now, to program on an art-center webpage, which is due opening tomorrow for public.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top