Creating Image Thumnails...

images php coding issues or problems here.

Moderators: macek, egami, gesf

j0moran
New php-forum User
New php-forum User
Posts: 7
Joined: Mon May 17, 2004 9:13 am

Creating Image Thumnails...

Postby j0moran » Fri Jul 30, 2004 9:05 am

ok.. What i want to do is to be able to upload a regular image (any size) and have php create a thumbnail version of that image and upload the thumbnail and the original file to the server. Currently my code only uploads the original image. I can display the image as a thumbnail doing <img width height> but the file size is still big. Id like the thumbnail size to be around 5k so the page loads quicker, and then have a link on that thumbnail to the original file. ANy help would be AWESOME. Thanks. Heres my code

Code: Select all

echo "<form enctype=\"multipart/form-data\" name=addmatch method=POST action=addmatch.php><table><tr>";
   echo "<tr><td>Image: </td><td ><input name=\"tfile\" type=\"file\"></td></tr>";

   echo "<tr><td colspan=2><input type=submit value=Add name=Add></td></tr></table></form>";


Code: Select all

   #### Grabs the UPLOAD File Information ############
   $ttype=$_FILES["tfile"]["type"];
   $tsize=$_FILES["tfile"]["size"];
   $tname=$_FILES["tfile"]["name"];
   $ttemp=$_FILES['tfile']['tmp_name'];
   ########################################
    ######## UPLOADS The IMAGE FILE to the Web Server ############
   //Checks to see if the file was uploaded VIA POST method
    if (is_uploaded_file($ttemp)){
          //Copies the file to the server
           copy($ttemp, "./images/" . $tname) or failed();
          //Removes the link
           unlink($ttemp);
   }
    #############################################################


BTW id like to do this if at all possible without adding additional modules to my php server.

j0moran
New php-forum User
New php-forum User
Posts: 7
Joined: Mon May 17, 2004 9:13 am

Postby j0moran » Fri Jul 30, 2004 10:18 am

Ok i figured out a way to do it by adding a makeThumbnail function.. heres my code...

Code: Select all

function makeThumbnail($source, $destination, $width, $height)
{
   //load source image
   if(!($sourceImage =@imagecreatefromjpeg($source)))
   {
      $image = imagecreate($width, $height);
      $colorWhite = imagecolorallocate($image, 255, 255, 255);
      $colorBlack = imagecolorallocate($image, 0, 0, 0);
      imagefill($image, 0, 0, $colorWwite);
      imagestring($image, 1, 1, 10, "Failed!", $colorBlack);   
      imagepng($image, $desintation);
      return(FALSE);
   }   
   
   $destinationImage = imagecreatetruecolor($width, $height);
   
   imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $width, $height, imagesx($sourceImage), imagesy($sourceImage));
   imagejpeg($destinationImage, $destination);
   
}

makeThumbnail("./images/".$tname."", "./images/thumb_".$tname."", 110, 80);


Now.. This took the original file, scaled it down. The original file was 60kb now the thumbnail is 2kb.

User avatar
southriver
New php-forum User
New php-forum User
Posts: 51
Joined: Sun May 29, 2005 8:31 pm

Postby southriver » Tue Jun 14, 2005 3:07 am

Ok , this is useful with news page.
I have done.

thks
best regards.


Return to “PHP coding => Images”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron