Help with thumbnails please someone help me :(
10-22-2008, 06:24 AM
|
Help with thumbnails please someone help me :(
|
Posts: 10
Name: kellyy
|
I am quite new with php, the start page and add page is all what i have learned. How ever, i am trying to now make the images into thumbnails but i am having no luck.
I found this small script that makes the images into thumbnails but no matter what i do to intergrate it into the rest i cant get it to work. If someone could please help me i would much appreciate it as ive been working so hard and i really woulld like some help on how this works.
I have to pages, start and add. Add is where i am implementing the code for the thumbnails.
Start.php
<form enctype="multipart/form-data" action="add.php" method="POST">
Name: <input type="text" name="name"><br>
Description: <input type="text" name="description"><br>
Price: <input type="text" name="price"><br>
Photo: <input type="file" name="photo"> <br>
<input type="submit" value="add">
</form>
PHP Code:
// Connects to the Database mysql_connect("localhost", "root", "") or die(mysql_error()) ; mysql_select_db("cars") or die(mysql_error()) ; //Retrieves data from MySQL $data = mysql_query("SELECT * FROM cars") or die(mysql_error()); //Puts it into an array while($info = mysql_fetch_array( $data )) {
//Outputs the image and other data Echo "<img src=images/".$info['photo'] ."> <br>"; Echo "<b>Name:</b> ".$info['name'] . "<br> "; Echo "<b>Description:</b> ".$info['description'] . " <br>"; Echo "<b>Price:</b> ".$info['price'] . " <hr>";
Add.php
PHP Code:
//This is the directory where images will be saved $target = "images/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $name=$_POST['name']; $description=$_POST['description']; $price=$_POST['price']; $pic=($_FILES['photo']['name']); // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()) ; mysql_select_db("cars") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO cars VALUES ('$name', '$description', '$price', '$pic')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else {
//Gives and error if its not echo "Sorry, there was a problem uploading your file."; }
//THIS IS MY IMAGE THUMBNAIL FUNCTION - this is what im having problems with
PHP Code:
function resizeImage($pic,$toWidth,$toHeight){
// Get the original geometry and calculate scales list($width, $height) = getimagesize($pic); $xscale=$width/$toWidth; $yscale=$height/$toHeight; // Recalculate new size with default ratio if ($yscale>$xscale){ $new_width = round($width * (1/$yscale)); $new_height = round($height * (1/$yscale)); } else { $new_width = round($width * (1/$xscale)); $new_height = round($height * (1/$xscale)); }
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($pic);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
return $imageResized; }
|
|
|
|
10-22-2008, 10:49 PM
|
Re: Help with thumbnails please someone help me :(
|
Posts: 76
Name: Radley
|
I made an example that will output the image as a PHP file, perhaps this will help you.
You just plug in the path to your image in a get method as well as the width/height and viola, the image.php turns into your thumbnail.
I'm not sure how well this will work to upload into your database since that is local, and get methods are client-server, but it should give you enough to figure it out. The image function itself returns an image identifier, not a useable image (Unless that is the format that is stored in SQL db?)
See the HTML file here:
http://radgh.com/files/test/image_resize/
and download the zip file for the source.
http://radgh.com/files/test/image_resize/image.zip
|
|
|
|
10-23-2008, 04:13 AM
|
Re: Help with thumbnails please someone help me :(
|
Posts: 1,226
Name: Mike
Location: Mataro, Spain
|
exec("convert '$infile' -scale '200x200>' '$outfile'");
|
|
|
|
10-26-2008, 03:16 PM
|
Re: Help with thumbnails please someone help me :(
|
Posts: 10
Name: kellyy
|
Quote:
Originally Posted by mtishetsky
exec("convert '$infile' -scale '200x200>' '$outfile'");
|
Hello, if i added this just this it would work??
|
|
|
|
10-26-2008, 03:17 PM
|
Re: Help with thumbnails please someone help me :(
|
Posts: 10
Name: kellyy
|
(ovbiously changing variables ofc)
|
|
|
|
10-26-2008, 03:40 PM
|
Re: Help with thumbnails please someone help me :(
|
Posts: 219
Name: Rob
Location: UK
|
Quote:
Originally Posted by kelly3330
Hello, if i added this just this it would work??
|
You need Imagick installed on your hosting package. http://www.imagemagick.org/script/index.php.
Check its installed and if installed the path to Imagick by running :
PHP Code:
<?php system("convert -version"); system("type convert"); ?>
|
|
|
|
10-26-2008, 05:19 PM
|
Re: Help with thumbnails please someone help me :(
|
Posts: 10
Name: kellyy
|
Ok i ran the image magik test, i got a blank page, baring in mind i am only running on local atm wamp) while practicing, do i need to upload to host in order to get this working? Im pretty sure my host does support it, thanks for helping, i appreciate it!
|
|
|
|
10-27-2008, 12:35 PM
|
Re: Help with thumbnails please someone help me :(
|
Posts: 10
Name: kellyy
|
Right i wasnt getting very far with that so i am now working from a thumbnail learning script that i have come across, i just need help fitting it in with my orginal script
This is the thumbnail script
PHP Code:
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) { // open the directory $dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files: while (false !== ($fname = readdir( $dir ))) { // parse path for the extension $info = pathinfo($pathToImages . $fname); // continue only if this is a JPEG image if ( strtolower($info['extension']) == 'jpg' ) { echo "Creating thumbnail for {$fname} <br />";
// load image and get image size $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img );
// calculate thumbnail size $new_width = $thumbWidth; $new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" ); } } // close the directory closedir( $dir ); } // call createThumb function and pass to it as parameters the path // to the directory that contains images, the path to the directory // in which thumbnails will be placed and the thumbnail's width. // We are assuming that the path will be a relative path working // both in the filesystem, and through the web for links createThumbs("images/","images/thumbs/",100); ?>
function createGallery( $pathToImages, $pathToThumbs ) { echo "Creating gallery.html <br />";
$output = "<html>"; $output .= "<head><title>Thumbnails</title></head>"; $output .= "<body>"; $output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">"; $output .= "<tr>";
// open the directory $dir = opendir( $pathToThumbs );
$counter = 0; // loop through the directory while (false !== ($fname = readdir($dir))) { // strip the . and .. entries out if ($fname != '' && $fname != '') { $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">"; $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />"; $output .= "</a></td>";
$counter += 1; if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; } } } // close the directory closedir( $dir );
$output .= "</tr>"; $output .= "</table>"; $output .= "</body>"; $output .= "</html>";
// open the file $fhandle = fopen( "gallery.html", "w" ); // write the contents of the $output variable to the file fwrite( $fhandle, $output ); // close the file fclose( $fhandle ); } // call createGallery function and pass to it as parameters the path // to the directory that contains images and the path to the directory // in which thumbnails will be placed. We are assuming that // the path will be a relative path working // both in the filesystem, and through the web for links createGallery("images/","images/thumbs/"); ?>
and this is the code for my start page (start.php)
PHP Code:
<form enctype="multipart/form-data" action="add.php" method="POST"> Name: <input type="text" name="name"><br> Description: <input type="text" name="description"><br> Price: <input type="text" name="price"><br> Photo: <input type="file" name="photo"> <br> <input type="submit" value="add"> </form> <?php // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()) ; mysql_select_db("cars") or die(mysql_error()) ;
include("./class.image.php") ;
//Retrieves data from MySQL $data = mysql_query("SELECT * FROM cars") or die(mysql_error());
//Puts it into an array while($info = mysql_fetch_array( $data )) {
//Outputs the image and other data Echo "<img src=images/".$info['photo'] ."> <br>"; Echo "<b>Name:</b> ".$info['name'] . "<br> "; Echo "<b>Description:</b> ".$info['description'] . " <br>"; Echo "<b>Price:</b> ".$info['price'] . " <hr>";
}
and this is the script for my second page add.php
PHP Code:
<?php
//This is the directory where images will be saved $target = "http://www.webmaster-talk.com/images/"; $target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form $name=$_POST['name']; $description=$_POST['description']; $price=$_POST['price']; $pic=($_FILES['photo']['name']);
// Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()) ; mysql_select_db("cars") or die(mysql_error()) ;
//Writes the information to the database mysql_query("INSERT INTO cars VALUES ('$name', '$description', '$price', '$pic')") ;
//Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
//Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else {
//Gives and error if its not echo "Sorry, there was a problem uploading your file."; }
?>
As you can see, i just need help tieing the thumbnail script into my script properly, if someone could just help me out and tell me where i need to change variables and that on my thumbnail script and what to that would be brilliant as im confused in which i need to change and change too. For example does the {$pathtoimages} varriable on the thumbnail need to be changed to the actual path to images as well or just kept like that?
Thankyou very much, any help with this would truly be appreciated as i really am trying to learn
Last edited by kelly3330; 10-27-2008 at 12:36 PM..
|
|
|
|
10-27-2008, 12:39 PM
|
Re: Help with thumbnails please someone help me :(
|
Posts: 10
Name: kellyy
|
My problem really is tieing both scripts together- i mean i am confused about the thumbnail script and where i change things
For example, here, would i change the filename variable $fname, to $pic or $ target from my orginal script or would this stay the same?
PHP Code:
// loop through it, looking for any/all JPG files: while (false !== ($fname = readdir( $dir ))) { // parse path for the extension $info = pathinfo($pathToImages . $fname); // continue only if this is a JPEG image if ( strtolower($info['extension']) == 'jpg' )
</span></span>
|
|
|
|
|
« Reply to Help with thumbnails please someone help me :(
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|