Avatar billede coolcom Nybegynder
06. juni 2008 - 01:11 Der er 10 kommentarer og
1 løsning

Problemer med GD lib

Hey.

Jeg er ved at kode et galleri, hvor jeg bruger GD lib.

Men mit problem er, at når jeg laver thumbnails, bliver de MEGET pixlede:

(testbillede)

http://www.starupdoggs.dk/upload/images/thumbs/tb_3.jpg

I forhold til originalen:

http://www.starupdoggs.dk/upload/images/3.jpg

Kan man løse dette på en eller anden måde, eller er det bare sådan de bliver?
Avatar billede Slater Ekspert
06. juni 2008 - 02:38 #1
Husker du at bruge imagecopyresampled i stedet for imagecopyresized?
Avatar billede jakobdo Ekspert
06. juni 2008 - 08:45 #2
Når du kalder imagejpeg(), hvad bruger du af kvalitet ?
Vis noget kode ja, så kan vi bedre hjælpe.
Avatar billede coderdk Praktikant
06. juni 2008 - 09:33 #3
Ja, vi kan ikke hjælpe dig med at finde fejlen, hvis du ikke viser hvordan du har gjort ;) Mit gæt er imagecopyresampled og/eller imagecreatetruecolor...
Avatar billede jakobdo Ekspert
06. juni 2008 - 09:36 #4
Så hælder jeg nok mest til: imagecreatetruecolor() :o)
Avatar billede coolcom Nybegynder
06. juni 2008 - 16:36 #5
Jeg har prøvet med imagecopyresampled, men det bliver næsten endnu værre.

Her er min kode:

        $counter = 0;
   
        // List of our known photo types
        $known_photo_types = array(
                            'image/pjpeg' => 'jpg',
                            'image/jpeg' => 'jpg',
                            'image/gif' => 'gif',
                            'image/bmp' => 'bmp',
                            'image/x-png' => 'png'
                        );
       
        // GD Function List
        $gd_function_suffix = array(
                            'image/pjpeg' => 'JPEG',
                            'image/jpeg' => 'JPEG',
                            'image/gif' => 'GIF',
                            'image/bmp' => 'WBMP',
                            'image/x-png' => 'PNG'
                        );
   
        // Fetch the photo array sent by preupload.php
        $photos_uploaded = $_FILES['photo_filename'];
   
        // Fetch the photo caption array
        $photo_caption         = $_POST['photo_caption'];
        $photo_category     = $_POST['category'];
        $number_of_fields    = $_POST['number_of_fields'];
       
        $path = realpath($_SERVER['DOCUMENT_ROOT']) . $settings['galleryImageDir'];
       
       
        if (empty($photo_category))
            {
                $form_error = $error->form_error($error = 'Du har ikke valgt en kategori');
                $this->upload();
                return;
            }
   
        while( $counter <= count($photos_uploaded) )
        {
            if($photos_uploaded['size'][$counter] > 0)
            {
                if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
                {
                    $this->output = $error->error('Ugyldig filtype i blandt de uploadede billeder');
                }
                else
                {
                    $connector->query('INSERT INTO cms_gallery_photos SET photo_filename="0", photo_caption="'.addslashes($photo_caption[$counter]).'",
                    photo_category="'.addslashes($_POST['category']).'"');
                   
                    $new_id = mysql_insert_id();
                    $filetype = $photos_uploaded['type'][$counter];
                    $extention = $known_photo_types[$filetype];
                    $filename = $new_id.".".$extention;
   
                    $connector->query('UPDATE cms_gallery_photos SET photo_filename="'.addslashes($filename).'" WHERE photo_id="'.addslashes($new_id).'"');
   
                    // Store the orignal file
                   
                   
                    move_uploaded_file($photos_uploaded['tmp_name'][$counter], $path . $filename);
   
                    // Let's get the Thumbnail size
                    $size = GetImageSize( $path . $filename );
                    if($size[0] > $size[1])
                    {
                        $thumbnail_width = 200;
                        $thumbnail_height = (int)(200 * $size[1] / $size[0]);
                    }
                    else
                    {
                        $thumbnail_width = (int)(200 * $size[0] / $size[1]);
                        $thumbnail_height = 200;
                    }
               
                    // Build Thumbnail with GD 1.x.x, you can use the other described methods too
                    $function_suffix = $gd_function_suffix[$filetype];
                    $function_to_read = "ImageCreateFrom".$function_suffix;
                    $function_to_write = "Image".$function_suffix;
   
                    // Read the source file
                    $source_handle = $function_to_read ( $path . $filename );
                   
                    if($source_handle)
                    {
                        // Let's create an blank image for the thumbnail
                            $destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height );
                   
                        // Now we resize it
                        ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );
                    }
   
                    // Let's save the thumbnail
                    $function_to_write( $destination_handle, $path."thumbs/tb_".$filename );
                    ImageDestroy($destination_handle );
                    //
   
                    $result_final .= "<img src='".$path."tb_".$filename."' /> File ".($counter+1)." Added<br />";
                    $this->output = $this->html->add_complete($result_final);
                }
            }
        $counter++;
        }
Avatar billede coderdk Praktikant
06. juni 2008 - 17:10 #6
Ændr:

$destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height );

til:

$destination_handle = imagecreatetruecolor( $thumbnail_width, $thumbnail_height );

og:

ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );

til

imagecopyresampled( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );
Avatar billede coolcom Nybegynder
06. juni 2008 - 17:37 #7
@coderdk:

Tak, så virker det:D

Smid et svar for point
Avatar billede coderdk Praktikant
06. juni 2008 - 18:52 #8
Aight :)
Avatar billede coolcom Nybegynder
07. juni 2008 - 13:23 #9
Jeg er lige stødt på endnu et problem.

Hvis jeg uploader billeder der er 1024 * 768 virker det fint, men hvis jeg så uploader et der er 1600 * 1200, får jeg en http fejl 500, om at der er en programmeringsfejl.

Hvad gør jeg ved det?
Avatar billede jakobdo Ekspert
07. juni 2008 - 14:40 #10
Det er med stor sandsynlighed din server som ikke kan håndtere så store mængder data, som et billede med den dimension frembringer.
Løsningen er enten at tjekke størrelsen med getimagesize(), lade vær med at uploade så store billeder eller få server admin til at give dig lov til at bruge mere hukommelse under eksekvering af et script.
Avatar billede coolcom Nybegynder
08. juni 2008 - 11:09 #11
Men jeg kan sagtens uploade 5 eller 10 af de mindre billeder af gangen, så det kan vel ikke være noget med hukommelsen?

Samtidig, står der jo også at det er en programmeringsfejl, når den ikke vil uploade.

Jeg har også udvidet min kode lidt:

$counter = 0;
   
        // List of our known photo types
        $known_photo_types = array(
                            'image/pjpeg' => 'jpg',
                            'image/jpeg' => 'jpg',
                            'image/gif' => 'gif',
                            'image/bmp' => 'bmp',
                            'image/x-png' => 'png'
                        );
       
        // GD Function List
        $gd_function_suffix = array(
                            'image/pjpeg' => 'JPEG',
                            'image/jpeg' => 'JPEG',
                            'image/gif' => 'GIF',
                            'image/bmp' => 'WBMP',
                            'image/x-png' => 'PNG'
                        );
   
        // Fetch the photo array sent by preupload.php
        $photos_uploaded = $_FILES['photo_filename'];
   
        // Fetch the photo caption array
        $photo_caption         = $_POST['photo_caption'];
        $photo_category     = $_POST['category'];
        $number_of_fields    = $_POST['number_of_fields'];
       
        $path = realpath($_SERVER['DOCUMENT_ROOT']) . $settings['galleryImageDir'];
       
       
        if (empty($photo_category))
            {
                $form_error = $error->form_error($error = 'Du har ikke valgt en kategori');
                $this->upload();
                return;
            }
   
        while( $counter <= count($photos_uploaded) )
        {
            if($photos_uploaded['size'][$counter] > 0)
            {
                if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
                {
                    $this->output = $error->error('Ugyldig filtype i blandt de uploadede billeder');
                }
                else
                {
                    $connector->query('INSERT INTO cms_gallery_photos SET photo_filename="0", photo_caption="'.addslashes($photo_caption[$counter]).'",
                    photo_category="'.addslashes($_POST['category']).'"');
                   
                    $new_id = mysql_insert_id();
                    $filetype = $photos_uploaded['type'][$counter];
                    $extention = $known_photo_types[$filetype];
                    $filename = $new_id.".".$extention;
   
                    $connector->query('UPDATE cms_gallery_photos SET photo_filename="'.addslashes($filename).'" WHERE photo_id="'.addslashes($new_id).'"');
   
                    // Store the orignal file
                    move_uploaded_file($photos_uploaded['tmp_name'][$counter], $path . $filename);
   
                    // Let's get the Thumbnail size
                    $size = GetImageSize( $path . $filename );
                    if($size[0] > $size[1])
                    {
                        $thumbnail_width = 150;
                        $thumbnail_height = (int)(150 * $size[1] / $size[0]);
                    }
                    else
                    {
                        $thumbnail_width = (int)(150 * $size[0] / $size[1]);
                        $thumbnail_height = 150;
                    }
               
                    // Build Thumbnail with GD 1.x.x, you can use the other described methods too
                    $function_suffix = $gd_function_suffix[$filetype];
                    $function_to_read = "ImageCreateFrom".$function_suffix;
                    $function_to_write = "Image".$function_suffix;
   
                    // Read the source file
                    $source_handle = $function_to_read ( $path . $filename );
                   
                    if($source_handle)
                    {
                        // Let's create an blank image for the thumbnail
                        $destination_handle = imagecreatetruecolor( $thumbnail_width, $thumbnail_height );
                   
                        // Now we resize it
                        imagecopyresampled( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );
                    }
   
                    // Let's save the thumbnail
                    $function_to_write( $destination_handle, $path."thumbs/tb_".$filename );
                    ImageDestroy($destination_handle );
                    //
                   
                    //Let's make the small images
                    $size = GetImageSize( $path . $filename );
                    if($size[0] > $size[1])
                    {
                        $small_width = 552;
                        $small_height = (int)(552 * $size[1] / $size[0]);
                    }
                    else
                    {
                        $small_width = (int)(552 * $size[0] / $size[1]);
                        $small_height = 552;
                    }
               
                    // Build small with GD 1.x.x, you can use the other described methods too
                    $function_suffix = $gd_function_suffix[$filetype];
                    $function_to_read = "ImageCreateFrom".$function_suffix;
                    $function_to_write = "Image".$function_suffix;
   
                    // Read the source file
                    $source_handle = $function_to_read ( $path . $filename );
                   
                    if($source_handle)
                    {
                        // Let's create an blank image for the small
                        $destination_handle = imagecreatetruecolor( $small_width, $small_height );
                   
                        // Now we resize it
                        imagecopyresampled( $destination_handle, $source_handle, 0, 0, 0, 0, $small_width, $small_height, $size[0], $size[1] );
                    }
   
                    // Let's save the small
                    $function_to_write( $destination_handle, $path."small/s_".$filename );
                    ImageDestroy($destination_handle );
                   
                   
                   
                    $result_final .= "<img src='".$path."tb_".$filename."' /> File ".($counter+1)." Added<br />";
                    $this->output = $this->html->add_complete($result_final);
                }
            }
        $counter++;
        }


Når jeg uploader et billed, vil jeg have den til at lave et thumbnail, et "small" billed på 552 px, samt at gemme det originale billed.

Når jeg så prøver at uploade et af de store, gemmer den det originale og et thumbnail. Men "small" fejler.
Jeg har prøvet at kigge på koden men jeg kan ikke lige finde fejlen, så jeg håber du/i kan.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester