SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem with bmp and png resizing php

    i got this script,in one of the very old artciles in sitepoint.
    everything is ok!except that PNG and BMP imges are not resized.
    giving this errors
    Code:
    Warning: imagecreatefromwbmp() [function.imagecreatefromwbmp]: 'thumb/tb_128448486.bmp' is not a valid WBMP file in C:\wamp\www\wallsnap.php on line 97
    
    Warning: imagewbmp(): supplied argument is not a valid Image resource in C:\wamp\www\wallsnap.php on line 105
    
    Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\wallsnap.php on line 106
    
    Warning: imagecreatefromwbmp() [function.imagecreatefromwbmp]: 'thumb/tb_128448486.bmp' is not a valid WBMP file in C:\wamp\www\wallsnap.php on line 139
    
    Warning: imagewbmp(): supplied argument is not a valid Image resource in C:\wamp\www\wallsnap.php on line 148
    
    Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\wallsnap.php on line 149
    Down is the Script check it for me please.
    Code:
    <?php session_start();
    $user=$_SESSION['user'];
    $id=$_SESSION['id'];
           //include("configa.inc.php");
           // include("design.inc.php");
    	    include("config.php");
    	
            
    
    		
    		
    		$image_dir='thumb';
    		$image_dir1='thumb';
            // initialization
    		
    		
            $result_final = "";
            $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 Suffix 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'];
            $photoFileName= $_FILES['photo_filename'];
            
            // Fetch the photo caption array
            $photo_caption = $_POST['photo_caption'];
            while( $counter <= count($photos_uploaded) )
            {
                    if($photos_uploaded['size'][$counter] > 0)
                    {
                            if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
                            {
                                    $result_final .= "Erro!the file is not a photo,or More than 2MB<br />";
                            }else{
    						
    						//create a random file name
                            $rand_name = md5(time());
                            $rand_name= rand(0,999999999);
    						$filetype = $photos_uploaded['type'][$counter];
                            $extention=$known_photo_types[$filetype];
                             $filename=$rand_name.".".$extention;
                            
                                    @mysql_query( "UPDATE login SET photo='$filename' WHERE user='$user'");
    								 @mysql_query( "UPDATE profile SET photos='$filename' WHERE users='$user'");
                                   // $new_id = mysql_insert_id();
                                   // $filetype = $photos_uploaded['type'][$counter];
                                    //$extention = $known_photo_types[$filetype];
                                    //$filename = $new_id.".".$extention;
    								
                                    //@mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );
                                    // Store the orignal file
                                    copy($photos_uploaded['tmp_name'][$counter], $image_dir."/tb_".$filename);
    // Let's get the original image size
                                    
    $size = GetImageSize($image_dir."/tb_".$filename );
    // First Create Thumbnail!
    // Thumbnail Settings
            $Config_tbwidth_wide = 100; // width of wide image
            $Config_tbheight_wide = 75; // height of wide image
            $Config_tbwidth_tall = 75; // width of tall image
            $Config_tbheight_tall = 100; // height of tall image
    // The Code
            if($size[0] > $size[1]){
                $thumbnail_width = $Config_tbwidth_wide;
                $thumbnail_height = (int)($Config_tbwidth_wide * $size[1] / $size[0]);
                if($thumbnail_height > $Config_tbheight_wide){
                    $thumbnail_height = $Config_tbheight_wide;
                    $thumbnail_width = (int)($Config_tbheight_wide * $size[0] / $size[1]);
                }
            }else{
                $thumbnail_width = (int)($Config_tbheight_tall * $size[0] / $size[1]);
                $thumbnail_height = $Config_tbheight_tall;
                if($thumbnail_width > $Config_tbwidth_tall){
                    $thumbnail_width = $Config_tbwidth_tall;
                    $thumbnail_height = (int)($Config_tbwidth_tall * $size[1] / $size[0]);
                }
            }
    
    // Build Thumbnail with GD 2.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 ( $image_dir."/tb_".$filename );
    if($source_handle){
    // Let's create a 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, $image_dir."/".$filename );
    ImageDestroy($destination_handle );
    // .................................................................................
    // This is the new code that checks for width and solves our problems
    // .................................................................................
    // Lets resize the image if its width is greater than 500 pixels
    // Resized image settings
    if ($size[0] > '80'){
            $Config_width_wide = 200; // width of wide image
            $Config_height_wide = 175; // height of wide image
            $Config_width_tall = 175; // width of tall image
            $Config_height_tall = 300; // height of tall image
    // The Code
            if($size[0] > $size[1]){
                $image_width = $Config_width_wide;
                $image_height = (int)($Config_width_wide * $size[1] / $size[0]);
                if($image_height > $Config_height_wide){
                    $image_height = $Config_height_wide;
                    $image_width = (int)($Config_height_wide * $size[0] / $size[1]);
                }
            }else{
                $image_width = (int)($Config_height_tall * $size[0] / $size[1]);
                $image_height = $Config_height_tall;
                if($image_width > $Config_width_tall){
                    $image_width = $Config_width_tall;
                    $image_height = (int)($Config_width_tall * $size[1] / $size[0]);
                }
            }
    
    // Build image with GD 2.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 ( $image_dir."/tb_".$filename );
    if($source_handle){
    // Let's create a blank image for the image
    $destination_handle = ImageCreateTrueColor ( $image_width, $image_height );
    // Now we resize it
    ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $image_width, $image_height, $size[0], $size[1] );
    }
    // Let's save the image
    
    $function_to_write( $destination_handle, $image_dir."/tb_".$filename );
    ImageDestroy($destination_handle );
    }
    
    $result_final .= "<img src='".$image_dir1. "/tb_".$filename."' /><br /><font color=green>Your  profile photo was changed</font><br /> <font size=5>To continue <a href=myprofile.php>click here</a></font><br />";
    $result_final.=mysql_query("INSERT INTO UPDATES SET photo='$filename',sender_id='$id'");
    }
    }
    $counter++;
    }
    // Print Result
    echo"$result_final ";
    Last edited by mrcniceguy; Aug 3, 2009 at 10:04. Reason: ..

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    bmp is not the same as wbmp see;
    http://us3.php.net/manual/en/functio...tefromwbmp.php

    Note:
    WBMP is a WAP graphic format optimized for mobile computing devices. The pictures in the WBMP format are saved in bit format. That is every pixel of an image is saved as 1 bit. Thus a 8 x 8 pixel image will require only 64 bits (8 bytes). The limitation of this format is that it can only save images in Black and White only (no support for color or gray scale).
    Imagemagick will work with bmp images and the code would be a lot simpler as well.
    Last edited by Rubble; Aug 3, 2009 at 10:33. Reason: Added note

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is pure-PHP code floating around to read BMP files into GD, although it's not particularly fast.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,815
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Since BMP files don't work on the web there is not really any point in using PHP to manipulate them.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thankx for ur reply Guys))
    i`m just wondering how i`m going to useImage Magic to work with the code i`m having)))

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    <?php session_start();
    $user=$_SESSION['user'];
    $id=$_SESSION['id'];
           
    //include("configa.inc.php");
           // include("design.inc.php");
            
    include("config.php");    
            
            
    $image_dir='thumb';
            
    $image_dir1='thumb';
            
    // initialization
                
            
    $result_final "";
            
    $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');
            
            
    // Fetch the photo array sent by preupload.php
            
    $photos_uploaded $_FILES['photo_filename'];
            
    $photoFileName$_FILES['photo_filename'];
            
            
    // Fetch the photo caption array
            
    $photo_caption $_POST['photo_caption'];
            while( 
    $counter <= count($photos_uploaded) )
            {
                    if(
    $photos_uploaded['size'][$counter] > 0)
                    {
                            if(!
    array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
                            {
                                    
    $result_final .= "Erro!the file is not a photo,or More than 2MB<br />";
                            }else{
                            
                            
    //create a random file name
                            
    $rand_name md5(time());
                            
    $rand_namerand(0,999999999);
                            
    $filetype $photos_uploaded['type'][$counter];
                            
    $extention=$known_photo_types[$filetype];
                             
    $filename=$rand_name.".".$extention;
                            
                                    @
    mysql_query"UPDATE login SET photo='$filename' WHERE user='$user'");
                                     @
    mysql_query"UPDATE profile SET photos='$filename' WHERE users='$user'");

     
    // The orignal file
    $original_image $photos_uploaded['tmp_name'][$counter];

    // First Create Thumbnail!
    // Thumbnail Settings
            
    $Config_tbwidth_wide 100// width of wide image
            
    $Config_tbheight_tall 100// height of tall image

    // Let's save the thumbnail
    $function_to_write $destination_handle.$image_dir."/".$filename ;
    exec("convert $original_image -resize {$Config_tbwidth_wide}x{$Config_tbheight_tall} -strip $function_to_write");

    // Resized image settings
            
    $Config_width_wide 200// width of wide image
            
    $Config_height_tall 300// height of tall image
    $function_to_write $destination_handle.$image_dir1."/".$filename ;
    exec("convert $original_image -resize {$Config_width_wide}x{$Config_height_tall} -strip $function_to_write");

    $result_final .= "<img src='".$image_dir1"/tb_".$filename."' /><br /><font color=green>Your  profile photo was changed</font><br /> <font size=5>To continue <a href=myprofile.php>click here</a></font><br />";
    $result_final.=mysql_query("INSERT INTO UPDATES SET photo='$filename',sender_id='$id'");
    }
    }
    $counter++;
    }
    // Print Result
    echo"$result_final ";
    Something like this would work but it is completely untested !!!!!!!!

    As felgall says unless the browser has a plug in most do not support bmp anyway.
    You could make the ImageMagick code change all the images to a jpg or png format if you wanted to.
    There are a lot of ImageMagick and php examples on my site.

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Rubblei tested the Code,it tells it says
    Your profile photo was changed.
    but n reality nothing happens.
    i hope u can check it.
    in ur website,i saw the examples.But I`m i never used imageMagick,i hope u Can do Something.

  8. #8
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Imagemagick may not be installed run this and what is the result:
    PHP Code:
    <?php
    echo "<pre>";
    system("type convert"); 
    system("convert -version");  
    echo 
    "</pre>";
    ?>
    I did say that the code was untested It was more to give you an idea how to do it.
    If ImagickMagick is installed I will take another look at it.

  9. #9
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code Gave me this message
    Code:
    Delphi Form Conversion Utility Version 7.0
    Copyright (c) 1995,2002 Borland Software Corporation
    I`m using Wampserver as my local Server))

  10. #10
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Thats strange ! I use XAMPP without a problem.

    As it is your local server you can install ImageMagick from here: http://www.imagemagick.org/script/bi...es.php#windows

    Are you then going to load your code up to a host ? If so you want to make sure they have it installed before getting to far into the code.

  11. #11
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i installed ImageMagic,and now it gave me this message
    Code:
    convert is /usr/bin/convert
    Version: ImageMagick 6.2.8 04/17/08 Q16 file:/usr/share/ImageMagick-6.2.8/doc/index.html
    Copyright: Copyright (C) 1999-2006 ImageMagick Studio LLC
    I tested the Code Again,but the same result,that it shows photo uploaded while nothing happens.

  12. #12
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Its working then; the version is a bit old as the current version is 6.5.something but the code will still work when its sorted. I will look into whats wrong.

  13. #13
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    See what this code does; I have added some echo's so you can check the values contain what they should.

    The message you recive at the end is assuming the images have been created it is not actualy checking to see they exist.

    You are saving both images to the same directory and so one will be overwriting the other when they are created:
    PHP Code:
    $image_dir='thumb';
    $image_dir1='thumb'
    PHP Code:
    <?php session_start();
     
    $user=$_SESSION['user'];
     
    $id=$_SESSION['id'];
     
    //include("configa.inc.php");
     // include("design.inc.php");
     
    include("config.php");
     
    $image_dir='thumb';
     
    $image_dir1='thumb';
     
    // initialization 
     
    $result_final "";
     
    $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');
     
    // Fetch the photo array sent by preupload.php
     
    $photos_uploaded $_FILES['photo_filename']; 

     
    // Fetch the photo caption array 
     
    $photo_caption $_POST['photo_caption'];
     
      
    // Check what is happening - remove from code when working
     
    echo "<br>photo_caption = ".$photo_caption;
     
     while( 
    $counter <= count($photos_uploaded) )
     {
     if(
    $photos_uploaded['size'][$counter] > 0)
     {
     if(!
    array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
     {
     
    $result_final .= "Erro!the file is not a photo,or More than 2MB<br />";
     }
     else{
     
    //create a random file name
     
    $rand_name md5(time());
     
    $rand_namerand(0,999999999);
     
    $filetype $photos_uploaded['type'][$counter];
     
    // Check what is happening - remove from code when working
     
    echo "<br>filetype = ".$filetype;
     
     
    $extention=$known_photo_types[$filetype];
     
    $filename=$rand_name.".".$extention;
     @
    mysql_query"UPDATE login SET photo='$filename' WHERE user='$user'");
     @
    mysql_query"UPDATE profile SET photos='$filename' WHERE users='$user'");
     
    // The orignal file
     
    $original_image $photos_uploaded['tmp_name'][$counter];
     
    // First Create Thumbnail!
     // Thumbnail Settings
     
    $Config_tbwidth_wide 100;// width of wide image
     
    $Config_tbheight_tall 100;// height of tall image
     // Let's save the thumbnail
     
    $function_to_write $image_dir."/".$filename ;
     
    exec("convert $original_image -resize {$Config_tbwidth_wide}x{$Config_tbheight_tall} -strip $function_to_write");
     
     
    // Check what is happening - remove from code when working
     
    echo "<br>Path to save thumbnail image = ".$function_to_write;
     echo 
    "<img src=\"$function_to_write\">";
     
     
    // Resized image settings
     
    $Config_width_wide 200// width of wide image
     
    $Config_height_tall 300// height of tall image
     
    $function_to_write1 $image_dir1."/".$filename ;
     
    exec("convert $original_image -resize {$Config_width_wide}x{$Config_height_tall} -strip $function_to_write1");
     
      
    // Check what is happening - remove from code when working
     
    echo "<br>Path to save thumbnail image = ".$function_to_write1;
     echo 
    "<img src=\"$function_to_write1\">";
     
     
    $result_final .= "<img src='".$image_dir1"/tb_".$filename."' /><br /><font color=green>Your  profile photo was changed</font><br /> <font size=5>To continue <a href=myprofile.php>click here</a></font><br />";
     
    mysql_query("INSERT INTO UPDATES SET photo='$filename',sender_id='$id'");
     }
     } 
    $counter++;
     } 
    // Print Result
     
    echo"$result_final ";  
     
    ?>

  14. #14
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Rubble You are the Man))the code is working.
    thankx very Much,i didn`t know even what is imageMagic.

  15. #15
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    I am glad it is now sorted; there is a lot you can do with Imagemagick - add borders, watermark, use different filters etc.
    http://www.imagemagick.org/script/examples.php

    One thing you should do if you are putting this code on a website is check the user input for "malicious user input" specialy when you are putting data into a database.

  16. #16
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how i`m i going to check ""malicious user input""??

  17. #17
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Check out google and this forum for sanitise user input.

    One basic way is to use htmlspecialchars ( ) http://uk2.php.net/manual/en/functio...ecialchars.php

    PHP Code:
    $photo_caption htmlspecialchars ($photo_caption); 
    Might be worth moving these lines after the image creation as if there is a problem with the image the code will fail.
    PHP Code:
    @mysql_query"UPDATE login SET photo='$filename' WHERE user='$user'"); @mysql_query"UPDATE profile SET photos='$filename' WHERE users='$user'"); 

  18. #18
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok!thankx man))i will tell you the feedback.


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
  •