Jump to content

thumbnail function


greengo

Recommended Posts

hi,

I'm trying to make an application that makes thumbnails but I can't make it work and i doesn't give me any error in return not even in the log file. Can anyone help me?

thank

 

<?php

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("$pathToImage/$fname");

    // continue only if this is a JPEG image

    if ($info['extension'] == 'jpg')

    {

      // load image and get image size

      $img = @imageCreateFromJPEG( "$pathToImages/$fname" )

      or die ("Cannot Initialize new GD image stream1");

      $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 )

      or die("Cannot Initialize new GD image stream2");

 

      // 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('photo','photo/thumbs',100);

 

 

 

 

 

function createGallery( $pathToImages, $pathToThumbs )

{

  //echo "Creating photo_fulvio.html <br />";

 

  $output = "<html>";

  $output .= "<head><title>PHOTO_FULVIO</title></head>";

  $output .= "<body>";

  $output .= "<table width='500' border='0' align='center'>";

  $output .= "<tr>";

  $output .= "<td><div align='center'>";

  $output .= "<h2><font color='#99CC00' face='Arial, Helvetica, sans-serif'><strong>FULVIO'S PHOTO</strong></font></h2>";

  $output .= "</div></td>";

  $output .= "</tr>";

  $output .= "</table>";

  $output .= "<table width='500' align='center' border='1'>";

  $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 align='center' align='center' valign='bottom'><a href='$pathToImages/$fname'>";

      $output .= "<img src='$pathToThumbs/$fname' border='0'><br><font color='black' face='Arial, Helvetica, sans-serif' size='2'>$fname</font>";

      $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( "photo_fulvio.php", "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("photo","photo/thumbs/");

//redirect to the created page

header( 'Location: http://localhost/greengo/fulvio/photo_fulvio.php') ;

?>

 

Link to comment
https://forums.phpfreaks.com/topic/77299-thumbnail-function/
Share on other sites

Here is a file I use to upload and convert jpg to thumbnails.

<?php
if ($submit){
/* Add backslashes before characters that need to be quoted in database queries etc.
   These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte). */
$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
//set the picture id a unix timestamp. I thought this will not have repeatations. You can keep anything what you like
// Add random number to time stamp to get ID Number
// Get file extention
$upload_dir="store/";   //Source File path
$thumbnail_path="store/Thumb/"; //Thumdnail File Path
$ext = substr($form_data_name,strlen($form_data_name)-3);
$ext = strtolower($ext);
// Verify file extention as supported formats
if ($ext == "jpg") {
if (move_uploaded_file($form_data,$upload_dir.$form_data_name))
        thumb_jpeg($form_data_name,$upload_dir,$thumbnail_path);
        $datatn = addslashes(fread(fopen($thumbnail_path.$form_data_name, "r"), filesize($thumbnail_path.$form_data_name)));
}
}
echo "<form method='post' action='$PHP_SELF'>";
echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
echo "<tr><td>File to upload:</td><td><input type='file' name='form_data' size='64'></td></tr>";
echo "</table>";
echo "<input type='submit' name='submit' value='submit'>";
echo "</form>";

function thumb_jpeg($image_name,$source_path,$destination_path){
//Create Thumbnail
    $new_width=150;  //Image width Change if needed
    $new_height=150;  //Image height Change if needed
    $destimg=ImageCreate($new_width,$new_height) or die("Problem In Creating image");
    $srcimg=ImageCreateFromjpeg($source_path.$image_name) or die("Problem In opening Source Image");
    ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die("Problem In resizing");
    Imagejpeg($destimg,$destination_path.$image_name) or die("Problem In saving");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/77299-thumbnail-function/#findComment-391372
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.