Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with thumbnails please someone help me :(
Old 10-22-2008, 06:24 AM Help with thumbnails please someone help me :(
Novice Talker

Posts: 10
Name: kellyy
Trades: 0
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$imageTmp0000$new_width$new_height$width$height);

return 
$imageResized;

kelly3330 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-22-2008, 10:49 PM Re: Help with thumbnails please someone help me :(
RadGH's Avatar
Skilled Talker

Posts: 76
Name: Radley
Trades: 0
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
RadGH is offline
Reply With Quote
View Public Profile
 
Old 10-23-2008, 04:13 AM Re: Help with thumbnails please someone help me :(
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
exec("convert '$infile' -scale '200x200>' '$outfile'");
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 10-26-2008, 03:16 PM Re: Help with thumbnails please someone help me :(
Novice Talker

Posts: 10
Name: kellyy
Trades: 0
Quote:
Originally Posted by mtishetsky View Post
exec("convert '$infile' -scale '200x200>' '$outfile'");

Hello, if i added this just this it would work??
kelly3330 is offline
Reply With Quote
View Public Profile
 
Old 10-26-2008, 03:17 PM Re: Help with thumbnails please someone help me :(
Novice Talker

Posts: 10
Name: kellyy
Trades: 0
(ovbiously changing variables ofc)
kelly3330 is offline
Reply With Quote
View Public Profile
 
Old 10-26-2008, 03:40 PM Re: Help with thumbnails please someone help me :(
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
Quote:
Originally Posted by kelly3330 View Post
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"); ?>
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 10-26-2008, 05:19 PM Re: Help with thumbnails please someone help me :(
Novice Talker

Posts: 10
Name: kellyy
Trades: 0
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!
kelly3330 is offline
Reply With Quote
View Public Profile
 
Old 10-27-2008, 12:35 PM Re: Help with thumbnails please someone help me :(
Novice Talker

Posts: 10
Name: kellyy
Trades: 0
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$img0000$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..
kelly3330 is offline
Reply With Quote
View Public Profile
 
Old 10-27-2008, 12:39 PM Re: Help with thumbnails please someone help me :(
Novice Talker

Posts: 10
Name: kellyy
Trades: 0
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>
kelly3330 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help with thumbnails please someone help me :(
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29567 seconds with 12 queries