SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with creating a square thumbnail.

    I'm trying to create square thumbnails with php and GD, but
    haven't gotten it to work yet. I followed the "create php gallery in minutes" article here in sitepoint, and modified it from there.

    Here's the upload.php which takes the images information sent by the form.
    Everything else works, the image uploads and all, but the thumbnail doesn't get created. (parts of it are in finnish, sorry about that).

    PHP Code:
    <?php
        
    include("config.inc.php");



        
    // initialization

        
    $result_final "";

        
    $counter 0;



        
    // tunnetut kuvatyypit

        
    $known_photo_types = array( 

                            
    'image/pjpeg' => 'jpg',

                            
    'image/jpeg' => 'jpg',

                            
    'image/gif' => 'gif',

                            
    'image/bmp' => 'bmp',

                            
    'image/png' => 'png'

                        
    );

        

        
    // GD Function List

        
    $gd_function_suffix = array( 

                            
    'image/pjpeg' => 'JPEG',

                            
    'image/jpeg' => 'JPEG',

                            
    'image/gif' => 'GIF',

                            
    'image/bmp' => 'WBMP',

                            
    'image/png' => 'PNG'

                        
    );



        
    // Hae kuva array jonka preupload.php lähetti

        
    $photos_uploaded $_FILES['kuva_tiedostonimi'];



        
    // Hae kuvan kuvaus

        
        
    $photo_hahmot $_POST['kuva_hahmot'];

        
    $photo_date $_POST['kuva_date'];

        
    $photo_medium $_POST['kuva_medium'];

        
    $photo_caption $_POST['kuva_kuvaus'];

        


        while( 
    $counter <= count($photos_uploaded) )

        {

            if(
    $photos_uploaded['size'][$counter] > 0)

            {

                if(!
    array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))

                {

                    
    $result_final .= "File ".($counter+1)." is not a photo<br />";

                }

                else

                {

                    
    mysql_query"INSERT INTO galleria_kuvat(`kuva_tiedostonimi`, `kuva_hahmot`, `kuva_date`, `kuva_medium`, `kuva_kuvaus`, `kuva_kategoria`) VALUES('0', '$photo_hahmot[$counter]', '$photo_date[$counter]', '$photo_medium[$counter]', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" );

                    
    $new_id mysql_insert_id();

                    
    $filetype $photos_uploaded['type'][$counter];

                    
    $extention $known_photo_types[$filetype];

                    
    $filename $new_id.".".$extention;



                    
    mysql_query"UPDATE galleria_kuvat SET kuva_tiedostonimi='".addslashes($filename)."' WHERE kuva_id='".addslashes($new_id)."'" );



                    
    // Tallenna alkuperäinen tiedosto

                    
    copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename);


            
    $file $images_dir "/" $filename;
            
    $thumb $images_dir "/tb_" $filename;

            function 
    create_thumb_for($file$thumb$thumbsize=20$thumbheight=20) {
        
            if(!
    file_exists($file) or !isset($file)) return false;
                    
            
    $format substr ($file, -4);
            
            switch (
    $format)
            {
                case 
    '.jpg':
                    
    $src_img ImageCreateFromJpeg($file); break;
                case 
    '.gif':
                    
    $src_img ImageCreateFromGif($file); break;
                case 
    '.png':
                    
    $src_img ImageCreateFromPng($file); break;
                default:
                    return 
    false;    // Kuvatyyppiä ei tunnistettu
            
    }
            
            switch(
    $thumbheight)    
            {
                case 
    0:    // Säilyttää kuvan suhteet
                    
    if (ImageSX($src_img) <= ImageSY($src_img))
                    {
                        
    $ratio ImageSY($src_img) / ImageSX($src_img);
                        
    $new_w $thumbsize/$ratio;
                        
    $new_h $thumbsize;
                    }
                    else
                    {
                        
    $ratio ImageSX($src_img) / ImageSY($src_img);
                        
    $new_h $thumbsize/$ratio;
                        
    $new_w $thumbsize;
                    }
                    
    $dst_img ImageCreateTrueColor($new_w$new_h);
                    
    ImageCopyResampled(
                        
    $dst_img,
                        
    $src_img,
                        
    0000,
                        
    $new_w,
                        
    $new_h,
                        
    ImageSX($src_img),
                        
    ImageSY($src_img)
                    );
                    break;
                default:
                    if (
    ImageSX($src_img) >= ImageSY($src_img))
                    {
                        
    $ratio ImageSY($src_img) / ImageSX($src_img);
                        
    $new_w $thumbheight/$ratio;
                        
    $new_h $thumbheight;
                    }
                    else
                    {
                        
    $ratio ImageSX($src_img) / ImageSY($src_img);
                        
    $new_h $thumbsize/$ratio;
                        
    $new_w $thumbsize;
                    }
                    
    $dst_img ImageCreateTrueColor($thumbsize$thumbheight);
                    
    ImageCopyResampled(
                        
    $dst_img,
                        
    $src_img,
                        -
    0.09*$thumbsize, -0.16*$thumbheight00,    // Määrittelee leikkuukohdan, kertoimien suurentaminen vie leikkuukohtia oikealle ja alas
                        
    $new_w+(0.3*$thumbsize),
                        
    $new_h+(0.3*$thumbsize),
                        
    ImageSX($src_img),
                        
    ImageSY($src_img)
                    );
            }

            
            
    Imagejpeg($dst_img$thumb);

            
    imagedestroy ($dst_img);
            
            if (
    file_exists($thumb)) return true;
        }
                    
                    
                    
    $result_final .= "<img src='".$images_dir"/tb_".$filename."' /> File ".($counter+1)." Added<br />";

                }

            }

        
    $counter++;

        }



        
    // Print Result

    echo <<<__HTML_END



    <html>

    <head>

        <title>Kuvia lähetetty</title>

    </head>

    <body>

        
    $result_final


    <a href="viewgallery.php">Gallerian kategoriat</a></br>
    <a href="testi.htm">Etusivu</a></br>
    <a href="preupload.php?number_of_fields=1">Lähetä lisää kuvia</a>
    </body>

    </html>



    __HTML_END;

    ?>

    Here's the thumbnail creation part for clarity:
    PHP Code:
    $file $images_dir "/" $filename;
            
    $thumb $images_dir "/tb_" $filename;

            function 
    create_thumb_for($file$thumb$thumbsize=20$thumbheight=20) {
        
            if(!
    file_exists($file) or !isset($file)) return false;
                    
            
    $format substr ($file, -4);
            
            switch (
    $format)
            {
                case 
    '.jpg':
                    
    $src_img ImageCreateFromJpeg($file); break;
                case 
    '.gif':
                    
    $src_img ImageCreateFromGif($file); break;
                case 
    '.png':
                    
    $src_img ImageCreateFromPng($file); break;
                default:
                    return 
    false;    // Kuvatyyppiä ei tunnistettu
            
    }
            
            switch(
    $thumbheight)    
            {
                case 
    0:    // Säilyttää kuvan suhteet
                    
    if (ImageSX($src_img) <= ImageSY($src_img))
                    {
                        
    $ratio ImageSY($src_img) / ImageSX($src_img);
                        
    $new_w $thumbsize/$ratio;
                        
    $new_h $thumbsize;
                    }
                    else
                    {
                        
    $ratio ImageSX($src_img) / ImageSY($src_img);
                        
    $new_h $thumbsize/$ratio;
                        
    $new_w $thumbsize;
                    }
                    
    $dst_img ImageCreateTrueColor($new_w$new_h);
                    
    ImageCopyResampled(
                        
    $dst_img,
                        
    $src_img,
                        
    0000,
                        
    $new_w,
                        
    $new_h,
                        
    ImageSX($src_img),
                        
    ImageSY($src_img)
                    );
                    break;
                default:
                    if (
    ImageSX($src_img) >= ImageSY($src_img))
                    {
                        
    $ratio ImageSY($src_img) / ImageSX($src_img);
                        
    $new_w $thumbheight/$ratio;
                        
    $new_h $thumbheight;
                    }
                    else
                    {
                        
    $ratio ImageSX($src_img) / ImageSY($src_img);
                        
    $new_h $thumbsize/$ratio;
                        
    $new_w $thumbsize;
                    }
                    
    $dst_img ImageCreateTrueColor($thumbsize$thumbheight);
                    
    ImageCopyResampled(
                        
    $dst_img,
                        
    $src_img,
                        -
    0.09*$thumbsize, -0.16*$thumbheight00,    // Määrittelee leikkuukohdan, kertoimien suurentaminen vie leikkuukohtia oikealle ja alas
                        
    $new_w+(0.3*$thumbsize),
                        
    $new_h+(0.3*$thumbsize),
                        
    ImageSX($src_img),
                        
    ImageSY($src_img)
                    );
            }

            
            
    Imagejpeg($dst_img$thumb);

            
    imagedestroy ($dst_img);
            
            if (
    file_exists($thumb)) return true;
        } 
    All help appreciated

  2. #2
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Someone said that the function wasn't started, so I added a line to it.
    Like this.
    PHP Code:
    $file $images_dir "/" $filename;
    $thumb $images_dir "/tb_" $filename '.jpg'//HUOM, JPG!

    create_thumb_for($file$thumb);

    function 
    create_thumb_for($file$thumb$thumbsize=20$thumbheight=20) {

    if(!
    file_exists($file) or !isset($file)) return false

    But that gives this error:
    Code:
    Fatal error: Call to undefined function: create_thumb_for() in /home/int2000/9868/html/saka/testi/upload.php on line 114
    And the line is this, just what I added.
    PHP Code:
    create_thumb_for($file$thumb); 
    Did I add a wrong code? Or are the $file or $thumb defined wrong? x_o

    ...I'm kind of lost..
    All help appreciated.

  3. #3
    SitePoint Zealot shaman's Avatar
    Join Date
    Feb 2005
    Location
    W.R. Home
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You call to undefined function. You haven't had the function when you've tryed to call it.
    Try to define function first.
    She's my german fraulein

  4. #4
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I put the line 114 under the function create_thumb_for , because that defines the function right? ...or did I understand wrong? x_o I'm quite a newbie with php as you can see...

    Now there's no error, but no thumbnail either...this is getting quite frustrating.
    I've tried quite many different thumbnail creation script, and the only one I've gotten to work, doesn't create square thumbnails, without squashing them.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •