SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Location
    East Coventry, PA
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image resizing issue - jpg vs JPG

    I am using this image resizing class by Bit Repository and I am having one particular problem with it. If the image I am resizing has an uppercase .JPG extension the resize fails, lowercase .jpg works perfect. I have posted the code below. Any help is greatly appreciated. Thanks.

    PHP Code:
    <?php
    /* 
    ------------------------------------------------------------------------------------
    Credits: Bit Repository

    Source URL: http://www.bitrepository.com/web-programming/php/resizing-an-image.html
    ------------------------------------------------------------------------------------
    */
    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)
    {
    $thumb = ($this->new_width $width && $this->new_height $height) ? true false// Thumbnail
    $bigger_image = ($this->new_width $width || $this->new_height $height) ? true false// Bigger Image

        
    if($thumb)
        {
            if(
    $this->new_width >= $this->new_height)
            {
            
    $x = ($width $this->new_width);

            
    $this->new_height = ($height $x);
            }
            else if(
    $this->new_height >= $this->new_width)
            {
            
    $x = ($height $this->new_height);

            
    $this->new_width = ($width $x);
            }
        }
        else if(
    $bigger_image)
        {
            if(
    $this->new_width >= $width)
            {
            
    $x = ($this->new_width $width);

            
    $this->new_height = ($height $x);
            }
            else if(
    $this->new_height >= $height)
            {
            
    $x = ($this->new_height $height);

            
    $this->new_width = ($width $x);
            }
        }
    }

    // What sort of image?

    $type substr(strrchr($mime'/'), 1);

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

    case 
    'png':
        
    $image_create_func 'ImageCreateFromPNG';
        
    $image_save_func 'ImagePNG';
        
    $new_image_ext 'png';
        break;

    case 
    'bmp':
        
    $image_create_func 'ImageCreateFromBMP';
        
    $image_save_func 'ImageBMP';
        
    $new_image_ext 'bmp';
        break;

    case 
    'gif':
        
    $image_create_func 'ImageCreateFromGIF';
        
    $image_save_func 'ImageGIF';
        
    $new_image_ext 'gif';
        break;

    case 
    'vnd.wap.wbmp':
        
    $image_create_func 'ImageCreateFromWBMP';
        
    $image_save_func 'ImageWBMP';
        
    $new_image_ext 'bmp';
        break;

    case 
    'xbm':
        
    $image_create_func 'ImageCreateFromXBM';
        
    $image_save_func 'ImageXBM';
        
    $new_image_ext 'xbm';
        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);

        
    $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);

               
    $save_path '';
            }

            
    $process $image_save_func($image_c$save_path);

            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("[ \t\n\r]+""_"$string);
        
    $string str_replace(" "'_'$string);
        
    $string ereg_replace("[ _]+""_"$string);

        return 
    $string;
        }
    }
    ?>

  2. #2
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See the part of the switch statement that reads:

    PHP Code:
    case 'jpeg'
           
    $image_create_func 'ImageCreateFromJPEG'
           
    $image_save_func 'ImageJPEG'
           
    $new_image_ext 'jpg'
           break; 
    ?

    The fastest solution would just to be to add a line beneath it:


    PHP Code:
    case 'jpg'
           
    $image_create_func 'ImageCreateFromJPEG'
           
    $image_save_func 'ImageJPEG'
           
    $new_image_ext 'jpg'
           break; 
    aaron-fisher.com - PHP articles and more

  3. #3
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,099
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If case is not important you could use strtolower on the filename.
    What I lack in acuracy I make up for in misteaks

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by lorenw View Post
    If case is not important you could use strtolower on the filename.
    The switch statement already does that.

    The problem is that it only specifies 'jpeg' in the case and not 'jpg'.

    The easiest way to fix it is to add the case 'jpg': immediately after the case 'jpeg': and before the code both cases are to run.
    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 Enthusiast
    Join Date
    Oct 2006
    Location
    East Coventry, PA
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, felgall! That did the trick.


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
  •