#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Red face Problems with thumbnails


    Hey. I just started to learn php and I have 2 problems with thumbnails that I've been trying to fix for days now. Hope you could help to solve at least one of them. This is very very important for me, so I would really appreciate if you helped me!!!! please

    1.I have made a form on my website where the user can upload image and add some extra information. So I made thumbnails for uploaded images that are stored in folder (they're stored in the database as well, but these thumbnails are linked with the folder not database) and when I click on the thumbnail I want it to show the big image and some data from database that was added when the user uploaded the image (like- name and date). Now when I click on a thumbnail it shows me all the data that's in the database- including other images as well. How to link a thumbnail that is made in folder with data from database? (thumbnails and original images have exact the same name).

    2. I used this script to create thumbnails, but it is only for .jpg images. What should I add to allow .png , .jpeg and .gif extensions as well?
    PHP Code:
    /** settings **/ 
    $images_dir 'images/'
    $thumbs_dir 'images_thumbs/'
    $thumbs_width 200
    $images_per_row 3






    /* function:  generates thumbnail */ 
    function make_thumb($src,$dest,$desired_width) { 
        
    /* read the source image */ 
        
    $source_image imagecreatefromjpeg($src); 
        
    $width imagesx($source_image); 
        
    $height imagesy($source_image); 
        
    /* find the "desired height" of this thumbnail, relative to the desired width  */ 
        
    $desired_height floor($height*($desired_width/$width)); 
        
    /* create a new, "virtual" image */ 
        
    $virtual_image imagecreatetruecolor($desired_width,$desired_height); 
        
    /* copy source image at a resized size */ 
        
    imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height); 
        
    /* create the physical thumbnail image to its destination */ 



    /* function:  returns files from dir */ 
    function get_files($images_dir,$exts = array('jpg')) { 
        
    $files = array(); 
        if(
    $handle opendir($images_dir)) { 
            while(
    false !== ($file readdir($handle))) { 
                
    $extension strtolower(get_file_extension($file)); 
                if(
    $extension && in_array($extension,$exts)) { 
                    
    $files[] = $file
                } 
            } 
            
    closedir($handle); 
        } 
        return 
    $files


    /* function:  returns a file's extension */ 
    function get_file_extension($file_name) { 
        return 
    substr(strrchr($file_name,'.'),1); 




    /** generate photo gallery **/ 
    $image_files get_files($images_dir); 
    if(
    count($image_files)) { 
        
    $index 0
        foreach(
    $image_files as $index=>$file) { 
            
    $index++; 
            
    $thumbnail_image $thumbs_dir.$file
            if(!
    file_exists($thumbnail_image)) { 
                
    $extension get_file_extension($thumbnail_image); 
                if(
    $extension) { 
                    
    make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width); 
                } 
            } 
            echo 
    '<a href="',$images_dir.$file,'"><img src="',$thumbnail_image,'" /></a>'
        } 

    else { 
        echo 
    '<p>There are no images in this gallery.</p>'

  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,996
    Rep Power
    9397
    1. It looks like that code is only about the gallery itself, not about what happens when you try to view an image. Is that what you mean? I would certainly expect it to show everything.
    It also looks like you're generating thumbnails when this page is displaying. Isn't it easier to do that when the images are being uploaded?

    2. If the image aren't too large and you have enough memory on the machine (and a high enough memory limit setting in PHP) then you can load pretty much any image with
    PHP Code:
    $source_image imagecreatefromstring(file_get_contents($src));
    if (!
    $source_image) {
        
    // error

    The thumbnails themselves can be in any type so making every one a JPEG shouldn't be a problem. Validate the images when they're uploaded.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    1. Yes ,the thumbnails are being generated not at the same time as images, but does that matter in this case? I have a form php file that redirects to addimage.php
    this is my addimage.php :
    PHP Code:
    // Image upload
    if(isset($_POST['submit'])) {
        
    $name $_POST['name'];
        
    $surname $_POST['surname'];
        
    $email $_POST['email'];
        
    $comment $_POST['comment'];
        
    $image$_FILES['image'];

        
    $errors = array();
        
    $allowed_ext = array ('jpg''jpeg''png''gif');

        
    $file_name $_FILES['image']['name'];
        
    $file_ext strtolower(end(explode('.'$file_name)));

        
    $file_size $_FILES['image']['size'];
        
    $file_tmp $_FILES['image']['tmp_name'];

        if (
    in_array($file_ext$allowed_ext) ===false) {
        
    $errors[] = 'Extension not allowed';
    }

    if(
    $file_size 2097152) {
        
    $errors[]= 'File size must be under 2mb';
    }


    if (empty(
    $errors)) {
        
    mysql_query("
            INSERT INTO competition
            (
                name,
                surname,
                email,
                image,
                comment,
                date
            )
            VALUES
            (
                '
    $name',
                '
    $surname',
                '
    $email',
                '
    $file_name',
                '
    $comment',
                NOW()
            )
        "
    ) or die(mysql_error());

        if (
    move_uploaded_file($file_tmp'images/'.$file_name));
            echo 
    'File uploaded';
            echo 
    '<p><img src="' 'images/' $file_name '" alt="Image" /></p>';
            echo 
    '<p><strong>Comment:</strong> ' $comment '<br />';
            echo 
    '<p><strong>Your name:</strong> ' $name ' ' $surname '<br />';
            echo 
    '<p><strong>E-mail:</strong> ' $email '<br />';
            echo 
    '<a href="thumbs.php">Go to the gallery</a>';
            
        } else {
            foreach(
    $errors as $error){
                echo 
    $error'<br/>';
            }
        }

    And as you see, this page just confirms the upload and leand the user to thumbs.php where I have this script that generates thumbnails.
    and actually I wanted to open a new page when the user clicks on the thumbnail , so I changed the link to go to a new page
    PHP Code:
    echo '<a href="gallery.php"><img src="',$thumbnail_image,'" /></a>'
    and here is the problem now. In the gallery.php I can take data from database, but I can't link it to the thumbnail, because thumbnails are in the folder, not database and are not linked to the database..
    I hope that you understood something. I am a beginner

IMN logo majestic logo threadwatch logo seochat tools logo