Visualizzazione dei risultati da 1 a 2 su 2

Discussione: Problema GD And BMP

  1. #1
    Utente di HTML.it L'avatar di kakashi
    Registrato dal
    Feb 2005
    Messaggi
    357

    Problema GD And BMP

    Ciao ragazzi ho un problema con le librerie GD.
    Ho una classe Php che mi permette di fare i resize immagini.
    Ho aggiunto una funzione per convertire le Bmp in Jpg, ma lo script mi va in errore e mi fa il resize sempre una volta invece di farli tutti insieme
    Codice PHP:
    class Resize_Image {

    var 
    $image_to_resize;
    var 
    $new_width;
    var 
    $new_height;
    var 
    $ratio;
    var 
    $new_image_name;
    var 
    $save_folder;

    function 
    resize()
    {
    if(!
    file_exists($this->image_to_resize))
    {
      exit(
    "File ".$this->image_to_resize." does not exist.");
    }

    $info GetImageSize($this->image_to_resize);

    if(empty(
    $info))
    {
      exit(
    "The file ".$this->image_to_resize." doesn't seem to be an image.");
    }

    $width $info[0];
    $height $info[1];
    $mime $info['mime'];

    /* Keep Aspect Ratio? */

    if($this->ratio)
    {
        if(
    $width $this->new_width || $height $this->new_height){
            if(
    $width $height){
                
    $factor = (float)$this->new_width / (float)$width;
                
    $this->new_height $factor $height;
            }
            else
            {
                
    $factor = (float)$this->new_height / (float)$height;
                
    $this->new_width $factor $width;
            }
        }
        else
        {
            
    $this->new_width $width ;
            
    $this->new_height $height;
        }
    }

    // What sort of image?
    $type substr(strrchr($mime'/'), 1);

    switch (
    $type)
    {
    case 
    'jpeg':
    case 
    'jpg' :
    case 
    'JPG' :
        
    $image_create_func 'ImageCreateFromJPEG';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    case 
    'png':
        
    $image_create_func 'ImageCreateFromPNG';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    case 
    'bmp':
        
    $image_create_func 'ImageCreateFromBMP';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    case 
    'gif':
        
    $image_create_func 'ImageCreateFromGIF';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    case 
    'vnd.wap.wbmp':
        
    $image_create_func 'ImageCreateFromWBMP';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    case 
    'xbm':
        
    $image_create_func 'ImageCreateFromXBM';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    default:
        
    $image_create_func 'ImageCreateFromJPEG';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
    }

    function 
    imagecreatefrombmp($p_sFile)
        {
            
    //    Load the image into a string
            
    $file    =    fopen($p_sFile,"rb");
            
    $read    =    fread($file,10);
            while(!
    feof($file)&&($read<>""))
                
    $read    .=    fread($file,1024);
           
            
    $temp    =    unpack("H*",$read);
            
    $hex    =    $temp[1];
            
    $header    =    substr($hex,0,108);
           
            
    //    Process the header
            //    Structure: [url]http://www.fastgraph.com/help/bmp_header_format.html[/url]
            
    if (substr($header,0,4)=="424d")
            {
                
    //    Cut it in parts of 2 bytes
                
    $header_parts    =    str_split($header,2);
               
                
    //    Get the width        4 bytes
                
    $width            =    hexdec($header_parts[19].$header_parts[18]);
               
                
    //    Get the height        4 bytes
                
    $height            =    hexdec($header_parts[23].$header_parts[22]);
               
                
    //    Unset the header params
                
    unset($header_parts);
            }
           
            
    //    Define starting X and Y
            
    $x                =    0;
            
    $y                =    1;
           
            
    //    Create newimage
            
    $image            =    imagecreatetruecolor($width,$height);
           
            
    //    Grab the body from the image
            
    $body            =    substr($hex,108);

            
    //    Calculate if padding at the end-line is needed
            //    Divided by two to keep overview.
            //    1 byte = 2 HEX-chars
            
    $body_size        =    (strlen($body)/2);
            
    $header_size    =    ($width*$height);

            
    //    Use end-line padding? Only when needed
            
    $usePadding        =    ($body_size>($header_size*3)+4);
           
            
    //    Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
            //    Calculate the next DWORD-position in the body
            
    for ($i=0;$i<$body_size;$i+=3)
            {
                
    //    Calculate line-ending and padding
                
    if ($x>=$width)
                {
                    
    //    If padding needed, ignore image-padding
                    //    Shift i to the ending of the current 32-bit-block
                    
    if ($usePadding)
                        
    $i    +=    $width%4;
                   
                    
    //    Reset horizontal position
                    
    $x    =    0;
                   
                    
    //    Raise the height-position (bottom-up)
                    
    $y++;
                   
                    
    //    Reached the image-height? Break the for-loop
                    
    if ($y>$height)
                        break;
                }
               
                
    //    Calculation of the RGB-pixel (defined as BGR in image-data)
                //    Define $i_pos as absolute position in the body
                
    $i_pos    =    $i*2;
                
    $r        =    hexdec($body[$i_pos+4].$body[$i_pos+5]);
                
    $g        =    hexdec($body[$i_pos+2].$body[$i_pos+3]);
                
    $b        =    hexdec($body[$i_pos].$body[$i_pos+1]);
               
                
    //    Calculate and draw the pixel
                
    $color    =    imagecolorallocate($image,$r,$g,$b);
                
    imagesetpixel($image,$x,$height-$y,$color);
               
                
    //    Raise the horizontal position
                
    $x++;
            }
           
            
    //    Unset the body / free the memory
            
    unset($body);
           
            
    //    Return image-object
            
    return $image;
        } 
        if(
    $type != "bmp"){
            
    // New Image
            
    $image_c ImageCreateTrueColor($this->new_width$this->new_height);    
            
    $new_image $image_create_func($this->image_to_resize);
            
    ImageCopyResampled($image_c$new_image0000$this->new_width$this->new_height$width$height);
        }

                if(
    $this->save_folder)
                {
                   if(
    $this->new_image_name)
                   {
                   
    $new_name $this->new_image_name.'.'.$new_image_ext;
                   }
                   else
                   {
                   
    $new_name $this->new_thumb_name(basename($this->image_to_resize)).'_resized.'.$new_image_ext;
                   }

                
    $save_path $this->save_folder.$new_name;
                }
                else
                {
                
    /* Show the image without saving it to a folder */
                   
    header("Content-Type: ".$mime);

                   
    $image_save_func($image_c,60);

                   
    $save_path '';
                }
            if(
    $type != "bmp"){
                
    $process $image_save_func($image_c$save_path,60);
            }
                return array(
    'result' => $process'new_file_path' => $save_path);

            }

        function 
    new_thumb_name($filename)
        {
            
    $string trim($filename);
            
    $string strtolower($string);
            
    $string trim(ereg_replace("[^ A-Za-z0-9_]"" "$string));
            
    $string ereg_replace("[ tnr]+""_"$string);
            
    $string str_replace(" "'_'$string);
            
    $string ereg_replace("[ _]+""_"$string);

            return 
    $string;
        } 
    L'errore che mi da è questo Fatal error: Cannot redeclare imagecreatefrombmp() (previously declared in C:\AppServ\www\libs\thumbnail.inc.php:767) in C:\AppServ\www\libs\thumbnail.inc.php on line 767

    sapete aiutarmi??

  2. #2
    Utente di HTML.it L'avatar di kakashi
    Registrato dal
    Feb 2005
    Messaggi
    357
    ho risolto cosi, Grazie cmq XD
    Codice PHP:
    class Resize_Image {

    var 
    $image_to_resize;
    var 
    $new_width;
    var 
    $new_height;
    var 
    $ratio;
    var 
    $new_image_name;
    var 
    $save_folder;

    function 
    resize()
    {
    if(!
    file_exists($this->image_to_resize))
    {
      exit(
    "File ".$this->image_to_resize." does not exist.");
    }

    $info GetImageSize($this->image_to_resize);

    if(empty(
    $info))
    {
      exit(
    "The file ".$this->image_to_resize." doesn't seem to be an image.");
    }

    $width $info[0];
    $height $info[1];
    $mime $info['mime'];

    /* Keep Aspect Ratio? */

    if($this->ratio)
    {
        if(
    $width $this->new_width || $height $this->new_height){
            if(
    $width $height){
                
    $factor = (float)$this->new_width / (float)$width;
                
    $this->new_height $factor $height;
            }
            else
            {
                
    $factor = (float)$this->new_height / (float)$height;
                
    $this->new_width $factor $width;
            }
        }
        else
        {
            
    $this->new_width $width ;
            
    $this->new_height $height;
        }
    }

    // What sort of image?
    $type substr(strrchr($mime'/'), 1);

    function 
    ConvertBMP2GD($src$dest false) {
        if(!(
    $src_f fopen($src"rb"))) {
            return 
    false;
        }
        if(!(
    $dest_f fopen($dest"wb"))) {
            return 
    false;
        }
        
    $header unpack("vtype/Vsize/v2reserved/Voffset"fread($src_f,14));
        
    $info unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant",fread($src_f40));

        
    extract($info);
        
    extract($header);

        if(
    $type != 0x4D42) { // signature "BM"
            
    return false;
        }

        
    $palette_size $offset 54;
        
    $ncolor $palette_size 4;
        
    $gd_header "";
        
    // true-color vs. palette
        
    $gd_header .= ($palette_size == 0) ? "\xFF\xFE" "\xFF\xFF";
        
    $gd_header .= pack("n2"$width$height);
        
    $gd_header .= ($palette_size == 0) ? "\x01" "\x00";
        if(
    $palette_size) {
            
    $gd_header .= pack("n"$ncolor);
        }
        
    // no transparency
        
    $gd_header .= "\xFF\xFF\xFF\xFF";

        
    fwrite($dest_f$gd_header);

        if(
    $palette_size) {
            
    $palette fread($src_f$palette_size);
            
    $gd_palette "";
            
    $j 0;
            while(
    $j $palette_size) {
                
    $b $palette{$j++};
                
    $g $palette{$j++};
                
    $r $palette{$j++};
                
    $a $palette{$j++};
                
    $gd_palette .= "$r$g$b$a";
            }
        
    $gd_palette .= str_repeat("\x00\x00\x00\x00"256 $ncolor);
        
    fwrite($dest_f$gd_palette);
        }

        
    $scan_line_size = (($bits $width) + 7) >> 3;
        
    $scan_line_align = ($scan_line_size 0x03) ? - ($scan_line_size &
        
    0x03) : 0;

        for(
    $i 0$l $height 1$i $height$i++, $l--) {
            
    // BMP stores scan lines starting from bottom
            
    fseek($src_f$offset + (($scan_line_size $scan_line_align) *
            
    $l));
            
    $scan_line fread($src_f$scan_line_size);
            if(
    $bits == 24) {
                
    $gd_scan_line "";
                
    $j 0;
                while(
    $j $scan_line_size) {
                    
    $b $scan_line{$j++};
                    
    $g $scan_line{$j++};
                    
    $r $scan_line{$j++};
                    
    $gd_scan_line .= "\x00$r$g$b";
                }
            }
            else if(
    $bits == 8) {
                
    $gd_scan_line $scan_line;
            }
            else if(
    $bits == 4) {
                
    $gd_scan_line "";
                
    $j 0;
                while(
    $j $scan_line_size) {
                    
    $byte ord($scan_line{$j++});
                    
    $p1 chr($byte >> 4);
                    
    $p2 chr($byte 0x0F);
                    
    $gd_scan_line .= "$p1$p2";
                } 
                
    $gd_scan_line substr($gd_scan_line0$width);
            }
            else if(
    $bits == 1)
            {
                
    $gd_scan_line "";
                
    $j 0;
                while(
    $j $scan_line_size) {
                
    $byte ord($scan_line{$j++});
                
    $p1 chr((int) (($byte 0x80) != 0));
                
    $p2 chr((int) (($byte 0x40) != 0));
                
    $p3 chr((int) (($byte 0x20) != 0));
                
    $p4 chr((int) (($byte 0x10) != 0));
                
    $p5 chr((int) (($byte 0x08) != 0));
                
    $p6 chr((int) (($byte 0x04) != 0));
                
    $p7 chr((int) (($byte 0x02) != 0));
                
    $p8 chr((int) (($byte 0x01) != 0));
                
    $gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
            } 
                
    $gd_scan_line substr($gd_scan_line0$width);
            }

            
    fwrite($dest_f$gd_scan_line);
        }
        
    fclose($src_f);
        
    fclose($dest_f);
        return 
    true;
    }

    function 
    imagecreatefrombmp($filename) {
        
    $tmp_name tempnam("/tmp""GD");
        if(
    ConvertBMP2GD($filename$tmp_name))
        {
            
    $img imagecreatefromgd($tmp_name);
            
    unlink($tmp_name);
            return 
    $img;
        } 
        return 
    false;


    switch (
    $type)
    {
    case 
    'jpeg':
    case 
    'jpg' :
    case 
    'JPG' :
        
    $image_create_func 'ImageCreateFromJPEG';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    case 
    'png':
        
    $image_create_func 'ImageCreateFromPNG';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    case 
    'bmp':
        
    //$image_create_func = 'imagecreatefrombmp';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    case 
    'gif':
        
    $image_create_func 'ImageCreateFromGIF';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    case 
    'vnd.wap.wbmp':
        
    $image_create_func 'ImageCreateFromWBMP';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    case 
    'xbm':
        
    $image_create_func 'ImageCreateFromXBM';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
        break;

    default:
        
    $image_create_func 'ImageCreateFromJPEG';
        
    $image_save_func 'ImageJPEG';
        
    $new_image_ext 'jpg';
    }

            
    // New Image
            
    $image_c ImageCreateTrueColor($this->new_width$this->new_height);    
            if(
    $type != "bmp"){
                
    $new_image $image_create_func($this->image_to_resize);}
            else{
                
    $new_image imagecreatefrombmp($this->image_to_resize);}
            
    ImageCopyResampled($image_c$new_image0000$this->new_width$this->new_height$width$height);

                if(
    $this->save_folder)
                {
                   if(
    $this->new_image_name)
                   {
                        
    $new_name $this->new_image_name.'.'.$new_image_ext;
                   }
                   else
                   {
                        
    $new_name $this->new_thumb_name(basename($this->image_to_resize)).'.'.$new_image_ext;
                   }

                        
    $save_path $this->save_folder.$new_name;
                }
                else
                {
                    
    /* Show the image without saving it to a folder */
                   
    header("Content-Type: ".$mime);
                   
    $image_save_func($image_c,60);
                   
    $save_path '';
                }
                
    $process $image_save_func($image_c$save_path,60);
                return array(
    'result' => $process'new_file_path' => $save_path);

        function 
    new_thumb_name($filename)
        {
            
    $string trim($filename);
            
    $string strtolower($string);
            
    $string trim(ereg_replace("[^ A-Za-z0-9_]"" "$string));
            
    $string ereg_replace("[ tnr]+""_"$string);
            
    $string str_replace(" "'_'$string);
            
    $string ereg_replace("[ _]+""_"$string);

            return 
    $string;
        }
    }


Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.